blob: 4fb4c4e4923d9f7e37e242b2ebff4d15f0f7fd02 [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;
329
330 lockdep_assert_held(&mvm->mutex);
331
Liad Kaufmand2515a92016-03-23 16:31:08 +0200332 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300333
334 /*
335 * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
336 * exists
337 */
338 if (!ieee80211_is_data_qos(hdr->frame_control) ||
339 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
340 queue = iwl_mvm_find_free_queue(mvm, IWL_MVM_DQA_MIN_MGMT_QUEUE,
341 IWL_MVM_DQA_MAX_MGMT_QUEUE);
342 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
343 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
344 queue);
345
346 /* If no such queue is found, we'll use a DATA queue instead */
347 }
348
349 if (queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
350 queue = mvmsta->reserved_queue;
351 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
352 }
353
354 if (queue < 0)
355 queue = iwl_mvm_find_free_queue(mvm, IWL_MVM_DQA_MIN_DATA_QUEUE,
356 IWL_MVM_DQA_MAX_DATA_QUEUE);
357 if (queue >= 0)
358 mvm->queue_info[queue].setup_reserved = false;
359
Liad Kaufmand2515a92016-03-23 16:31:08 +0200360 spin_unlock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300361
362 /* TODO: support shared queues for same RA */
363 if (queue < 0)
364 return -ENOSPC;
365
366 /*
367 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
368 * but for configuring the SCD to send A-MPDUs we need to mark the queue
369 * as aggregatable.
370 * Mark all DATA queues as allowing to be aggregated at some point
371 */
Liad Kaufmand5216a22015-08-09 15:50:51 +0300372 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
373 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +0300374
375 IWL_DEBUG_TX_QUEUES(mvm, "Allocating queue #%d to sta %d on tid %d\n",
376 queue, mvmsta->sta_id, tid);
377
378 ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
379 iwl_mvm_enable_txq(mvm, queue, mac_queue, ssn, &cfg,
380 wdg_timeout);
381
382 spin_lock_bh(&mvmsta->lock);
383 mvmsta->tid_data[tid].txq_id = queue;
384 mvmsta->tfd_queue_msk |= BIT(queue);
385
386 if (mvmsta->reserved_queue == queue)
387 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
388 spin_unlock_bh(&mvmsta->lock);
389
390 return iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
391}
392
393static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
394{
395 if (tid == IWL_MAX_TID_COUNT)
396 return IEEE80211_AC_VO; /* MGMT */
397
398 return tid_to_mac80211_ac[tid];
399}
400
401static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
402 struct ieee80211_sta *sta, int tid)
403{
404 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
405 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
406 struct sk_buff *skb;
407 struct ieee80211_hdr *hdr;
408 struct sk_buff_head deferred_tx;
409 u8 mac_queue;
410 bool no_queue = false; /* Marks if there is a problem with the queue */
411 u8 ac;
412
413 lockdep_assert_held(&mvm->mutex);
414
415 skb = skb_peek(&tid_data->deferred_tx_frames);
416 if (!skb)
417 return;
418 hdr = (void *)skb->data;
419
420 ac = iwl_mvm_tid_to_ac_queue(tid);
421 mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
422
423 if (tid_data->txq_id == IEEE80211_INVAL_HW_QUEUE &&
424 iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
425 IWL_ERR(mvm,
426 "Can't alloc TXQ for sta %d tid %d - dropping frame\n",
427 mvmsta->sta_id, tid);
428
429 /*
430 * Mark queue as problematic so later the deferred traffic is
431 * freed, as we can do nothing with it
432 */
433 no_queue = true;
434 }
435
436 __skb_queue_head_init(&deferred_tx);
437
Liad Kaufmand2515a92016-03-23 16:31:08 +0200438 /* Disable bottom-halves when entering TX path */
439 local_bh_disable();
Liad Kaufman24afba72015-07-28 18:56:08 +0300440 spin_lock(&mvmsta->lock);
441 skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
442 spin_unlock(&mvmsta->lock);
443
Liad Kaufman24afba72015-07-28 18:56:08 +0300444 while ((skb = __skb_dequeue(&deferred_tx)))
445 if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
446 ieee80211_free_txskb(mvm->hw, skb);
447 local_bh_enable();
448
449 /* Wake queue */
450 iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
451}
452
453void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
454{
455 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
456 add_stream_wk);
457 struct ieee80211_sta *sta;
458 struct iwl_mvm_sta *mvmsta;
459 unsigned long deferred_tid_traffic;
460 int sta_id, tid;
461
462 mutex_lock(&mvm->mutex);
463
464 /* Go over all stations with deferred traffic */
465 for_each_set_bit(sta_id, mvm->sta_deferred_frames,
466 IWL_MVM_STATION_COUNT) {
467 clear_bit(sta_id, mvm->sta_deferred_frames);
468 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
469 lockdep_is_held(&mvm->mutex));
470 if (IS_ERR_OR_NULL(sta))
471 continue;
472
473 mvmsta = iwl_mvm_sta_from_mac80211(sta);
474 deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
475
476 for_each_set_bit(tid, &deferred_tid_traffic,
477 IWL_MAX_TID_COUNT + 1)
478 iwl_mvm_tx_deferred_stream(mvm, sta, tid);
479 }
480
481 mutex_unlock(&mvm->mutex);
482}
483
484static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
Liad Kaufmand5216a22015-08-09 15:50:51 +0300485 struct ieee80211_sta *sta,
486 enum nl80211_iftype vif_type)
Liad Kaufman24afba72015-07-28 18:56:08 +0300487{
488 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
489 int queue;
490
491 spin_lock_bh(&mvm->queue_info_lock);
492
493 /* Make sure we have free resources for this STA */
Liad Kaufmand5216a22015-08-09 15:50:51 +0300494 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
495 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
496 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].setup_reserved)
497 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
498 else
499 queue = iwl_mvm_find_free_queue(mvm, IWL_MVM_DQA_MIN_DATA_QUEUE,
500 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +0300501 if (queue < 0) {
502 spin_unlock_bh(&mvm->queue_info_lock);
503 IWL_ERR(mvm, "No available queues for new station\n");
504 return -ENOSPC;
505 }
506 mvm->queue_info[queue].setup_reserved = true;
507
508 spin_unlock_bh(&mvm->queue_info_lock);
509
510 mvmsta->reserved_queue = queue;
511
512 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
513 queue, mvmsta->sta_id);
514
515 return 0;
516}
517
Johannes Berg8ca151b2013-01-24 14:25:36 +0100518int iwl_mvm_add_sta(struct iwl_mvm *mvm,
519 struct ieee80211_vif *vif,
520 struct ieee80211_sta *sta)
521{
522 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100523 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharona571f5f2015-12-07 12:50:58 +0200524 struct iwl_mvm_rxq_dup_data *dup_data;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100525 int i, ret, sta_id;
526
527 lockdep_assert_held(&mvm->mutex);
528
529 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
Eliad Pellerb92e6612014-01-23 17:58:23 +0200530 sta_id = iwl_mvm_find_free_sta_id(mvm,
531 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100532 else
533 sta_id = mvm_sta->sta_id;
534
Johannes Berg36f46312015-03-10 20:32:08 +0100535 if (sta_id == IWL_MVM_STATION_COUNT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100536 return -ENOSPC;
537
538 spin_lock_init(&mvm_sta->lock);
539
540 mvm_sta->sta_id = sta_id;
541 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
542 mvmvif->color);
543 mvm_sta->vif = vif;
544 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300545 mvm_sta->tx_protection = 0;
546 mvm_sta->tt_tx_protection = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100547
548 /* HW restart, don't assume the memory has been zeroed */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300549 atomic_set(&mvm->pending_frames[sta_id], 0);
Liad Kaufman69191af2015-09-01 18:50:22 +0300550 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100551 mvm_sta->tfd_queue_msk = 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300552
553 /* allocate new queues for a TDLS station */
554 if (sta->tdls) {
555 ret = iwl_mvm_tdls_sta_init(mvm, sta);
556 if (ret)
557 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300558 } else if (!iwl_mvm_is_dqa_supported(mvm)) {
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300559 for (i = 0; i < IEEE80211_NUM_ACS; i++)
560 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
561 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
562 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100563
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200564 /* for HW restart - reset everything but the sequence number */
Liad Kaufman24afba72015-07-28 18:56:08 +0300565 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200566 u16 seq = mvm_sta->tid_data[i].seq_number;
567 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
568 mvm_sta->tid_data[i].seq_number = seq;
Liad Kaufman24afba72015-07-28 18:56:08 +0300569
570 if (!iwl_mvm_is_dqa_supported(mvm))
571 continue;
572
573 /*
574 * Mark all queues for this STA as unallocated and defer TX
575 * frames until the queue is allocated
576 */
577 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
578 skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200579 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300580 mvm_sta->deferred_traffic_tid_map = 0;
Eyal Shapiraefed6642014-09-14 15:58:53 +0300581 mvm_sta->agg_tids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100582
Sara Sharona571f5f2015-12-07 12:50:58 +0200583 if (iwl_mvm_has_new_rx_api(mvm) &&
584 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
585 dup_data = kcalloc(mvm->trans->num_rx_queues,
586 sizeof(*dup_data),
587 GFP_KERNEL);
588 if (!dup_data)
589 return -ENOMEM;
590 mvm_sta->dup_data = dup_data;
591 }
592
Liad Kaufman24afba72015-07-28 18:56:08 +0300593 if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufmand5216a22015-08-09 15:50:51 +0300594 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
595 ieee80211_vif_type_p2p(vif));
Liad Kaufman24afba72015-07-28 18:56:08 +0300596 if (ret)
597 goto err;
598 }
599
600 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100601 if (ret)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300602 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100603
Johannes Berg9e848012014-08-04 14:33:42 +0200604 if (vif->type == NL80211_IFTYPE_STATION) {
605 if (!sta->tdls) {
606 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
607 mvmvif->ap_sta_id = sta_id;
608 } else {
609 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
610 }
611 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100612
613 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
614
615 return 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300616
617err:
618 iwl_mvm_tdls_sta_deinit(mvm, sta);
619 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100620}
621
Johannes Berg7a453972013-02-12 13:10:44 +0100622int iwl_mvm_update_sta(struct iwl_mvm *mvm,
623 struct ieee80211_vif *vif,
624 struct ieee80211_sta *sta)
625{
Liad Kaufman24afba72015-07-28 18:56:08 +0300626 return iwl_mvm_sta_send_to_fw(mvm, sta, true, 0);
Johannes Berg7a453972013-02-12 13:10:44 +0100627}
628
Johannes Berg8ca151b2013-01-24 14:25:36 +0100629int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
630 bool drain)
631{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300632 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100633 int ret;
634 u32 status;
635
636 lockdep_assert_held(&mvm->mutex);
637
638 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
639 cmd.sta_id = mvmsta->sta_id;
640 cmd.add_modify = STA_MODE_MODIFY;
641 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
642 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
643
644 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +0200645 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
646 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300647 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100648 if (ret)
649 return ret;
650
Sara Sharon837c4da2016-01-07 16:50:45 +0200651 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100652 case ADD_STA_SUCCESS:
653 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
654 mvmsta->sta_id);
655 break;
656 default:
657 ret = -EIO;
658 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
659 mvmsta->sta_id);
660 break;
661 }
662
663 return ret;
664}
665
666/*
667 * Remove a station from the FW table. Before sending the command to remove
668 * the station validate that the station is indeed known to the driver (sanity
669 * only).
670 */
671static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
672{
673 struct ieee80211_sta *sta;
674 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
675 .sta_id = sta_id,
676 };
677 int ret;
678
679 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
680 lockdep_is_held(&mvm->mutex));
681
682 /* Note: internal stations are marked as error values */
683 if (!sta) {
684 IWL_ERR(mvm, "Invalid station id\n");
685 return -EINVAL;
686 }
687
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300688 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100689 sizeof(rm_sta_cmd), &rm_sta_cmd);
690 if (ret) {
691 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
692 return ret;
693 }
694
695 return 0;
696}
697
698void iwl_mvm_sta_drained_wk(struct work_struct *wk)
699{
700 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
701 u8 sta_id;
702
703 /*
704 * The mutex is needed because of the SYNC cmd, but not only: if the
705 * work would run concurrently with iwl_mvm_rm_sta, it would run before
706 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
707 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
708 * that later.
709 */
710 mutex_lock(&mvm->mutex);
711
712 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
713 int ret;
714 struct ieee80211_sta *sta =
715 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
716 lockdep_is_held(&mvm->mutex));
717
Johannes Berg1ddbbb02013-12-04 22:39:17 +0100718 /*
719 * This station is in use or RCU-removed; the latter happens in
720 * managed mode, where mac80211 removes the station before we
721 * can remove it from firmware (we can only do that after the
722 * MAC is marked unassociated), and possibly while the deauth
723 * frame to disconnect from the AP is still queued. Then, the
724 * station pointer is -ENOENT when the last skb is reclaimed.
725 */
726 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100727 continue;
728
729 if (PTR_ERR(sta) == -EINVAL) {
730 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
731 sta_id);
732 continue;
733 }
734
735 if (!sta) {
736 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
737 sta_id);
738 continue;
739 }
740
741 WARN_ON(PTR_ERR(sta) != -EBUSY);
742 /* This station was removed and we waited until it got drained,
743 * we can now proceed and remove it.
744 */
745 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
746 if (ret) {
747 IWL_ERR(mvm,
748 "Couldn't remove sta %d after it was drained\n",
749 sta_id);
750 continue;
751 }
Monam Agarwalc531c772014-03-24 00:05:56 +0530752 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100753 clear_bit(sta_id, mvm->sta_drained);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300754
755 if (mvm->tfd_drained[sta_id]) {
756 unsigned long i, msk = mvm->tfd_drained[sta_id];
757
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +0200758 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +0300759 iwl_mvm_disable_txq(mvm, i, i,
760 IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300761
762 mvm->tfd_drained[sta_id] = 0;
763 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
764 sta_id, msk);
765 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100766 }
767
768 mutex_unlock(&mvm->mutex);
769}
770
Liad Kaufman24afba72015-07-28 18:56:08 +0300771static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
772 struct ieee80211_vif *vif,
773 struct iwl_mvm_sta *mvm_sta)
774{
775 int ac;
776 int i;
777
778 lockdep_assert_held(&mvm->mutex);
779
780 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
781 if (mvm_sta->tid_data[i].txq_id == IEEE80211_INVAL_HW_QUEUE)
782 continue;
783
784 ac = iwl_mvm_tid_to_ac_queue(i);
785 iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
786 vif->hw_queue[ac], i, 0);
787 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
788 }
789}
790
Johannes Berg8ca151b2013-01-24 14:25:36 +0100791int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
792 struct ieee80211_vif *vif,
793 struct ieee80211_sta *sta)
794{
795 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100796 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100797 int ret;
798
799 lockdep_assert_held(&mvm->mutex);
800
Sara Sharona571f5f2015-12-07 12:50:58 +0200801 if (iwl_mvm_has_new_rx_api(mvm))
802 kfree(mvm_sta->dup_data);
803
Johannes Berg8ca151b2013-01-24 14:25:36 +0100804 if (vif->type == NL80211_IFTYPE_STATION &&
805 mvmvif->ap_sta_id == mvm_sta->sta_id) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +0200806 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
807 if (ret)
808 return ret;
Emmanuel Grumbach80d85652013-02-19 15:32:42 +0200809 /* flush its queues here since we are freeing mvm_sta */
Luca Coelho5888a402015-10-06 09:54:57 +0300810 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +0200811 if (ret)
812 return ret;
813 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
814 mvm_sta->tfd_queue_msk);
815 if (ret)
816 return ret;
817 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
Emmanuel Grumbach80d85652013-02-19 15:32:42 +0200818
Liad Kaufman24afba72015-07-28 18:56:08 +0300819 /* If DQA is supported - the queues can be disabled now */
820 if (iwl_mvm_is_dqa_supported(mvm))
821 iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
822
Johannes Berg8ca151b2013-01-24 14:25:36 +0100823 /* if we are associated - we can't remove the AP STA now */
824 if (vif->bss_conf.assoc)
825 return ret;
826
827 /* unassoc - go ahead - remove the AP STA now */
828 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
Eliad Peller37577fe2013-12-05 17:19:39 +0200829
830 /* clear d0i3_ap_sta_id if no longer relevant */
831 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
832 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100833 }
834
835 /*
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300836 * This shouldn't happen - the TDLS channel switch should be canceled
837 * before the STA is removed.
838 */
839 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
840 mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
841 cancel_delayed_work(&mvm->tdls_cs.dwork);
842 }
843
844 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300845 * Make sure that the tx response code sees the station as -EBUSY and
846 * calls the drain worker.
847 */
848 spin_lock_bh(&mvm_sta->lock);
849 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +0100850 * There are frames pending on the AC queues for this station.
851 * We need to wait until all the frames are drained...
852 */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300853 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100854 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
855 ERR_PTR(-EBUSY));
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300856 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300857
858 /* disable TDLS sta queues on drain complete */
859 if (sta->tdls) {
860 mvm->tfd_drained[mvm_sta->sta_id] =
861 mvm_sta->tfd_queue_msk;
862 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
863 mvm_sta->sta_id);
864 }
865
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300866 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100867 } else {
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300868 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300869
870 if (sta->tdls)
871 iwl_mvm_tdls_sta_deinit(mvm, sta);
872
Johannes Berg8ca151b2013-01-24 14:25:36 +0100873 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
Monam Agarwalc531c772014-03-24 00:05:56 +0530874 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100875 }
876
877 return ret;
878}
879
880int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
881 struct ieee80211_vif *vif,
882 u8 sta_id)
883{
884 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
885
886 lockdep_assert_held(&mvm->mutex);
887
Monam Agarwalc531c772014-03-24 00:05:56 +0530888 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100889 return ret;
890}
891
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200892int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
893 struct iwl_mvm_int_sta *sta,
894 u32 qmask, enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100895{
896 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +0200897 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100898 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
899 return -ENOSPC;
900 }
901
902 sta->tfd_queue_msk = qmask;
903
904 /* put a non-NULL value so iterating over the stations won't stop */
905 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
906 return 0;
907}
908
Johannes Berg712b24a2014-08-04 14:14:14 +0200909static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
910 struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100911{
Monam Agarwalc531c772014-03-24 00:05:56 +0530912 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100913 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
914 sta->sta_id = IWL_MVM_STATION_COUNT;
915}
916
917static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
918 struct iwl_mvm_int_sta *sta,
919 const u8 *addr,
920 u16 mac_id, u16 color)
921{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300922 struct iwl_mvm_add_sta_cmd cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100923 int ret;
924 u32 status;
925
926 lockdep_assert_held(&mvm->mutex);
927
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300928 memset(&cmd, 0, sizeof(cmd));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100929 cmd.sta_id = sta->sta_id;
930 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
931 color));
932
933 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
Liad Kaufmancf0cda12015-09-24 10:44:12 +0200934 cmd.tid_disable_tx = cpu_to_le16(0xffff);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100935
936 if (addr)
937 memcpy(cmd.addr, addr, ETH_ALEN);
938
Sara Sharon854c5702016-01-26 13:17:47 +0200939 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
940 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300941 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100942 if (ret)
943 return ret;
944
Sara Sharon837c4da2016-01-07 16:50:45 +0200945 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100946 case ADD_STA_SUCCESS:
947 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
948 return 0;
949 default:
950 ret = -EIO;
951 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
952 status);
953 break;
954 }
955 return ret;
956}
957
958int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
959{
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200960 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
961 mvm->cfg->base_params->wd_timeout :
962 IWL_WATCHDOG_DISABLED;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100963 int ret;
964
965 lockdep_assert_held(&mvm->mutex);
966
Ariej Marjieh7da91b02014-07-07 12:09:40 +0300967 /* Map Aux queue to fifo - needs to happen before adding Aux station */
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300968 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300969 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
Ariej Marjieh7da91b02014-07-07 12:09:40 +0300970
971 /* Allocate aux station and assign to it the aux queue */
972 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
Eliad Pellerb92e6612014-01-23 17:58:23 +0200973 NL80211_IFTYPE_UNSPECIFIED);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100974 if (ret)
975 return ret;
976
977 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
978 MAC_INDEX_AUX, 0);
979
980 if (ret)
981 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
982 return ret;
983}
984
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200985int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
986{
987 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
988
989 lockdep_assert_held(&mvm->mutex);
990 return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
991 mvmvif->id, 0);
992}
993
994int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
995{
996 int ret;
997
998 lockdep_assert_held(&mvm->mutex);
999
1000 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
1001 if (ret)
1002 IWL_WARN(mvm, "Failed sending remove station\n");
1003
1004 return ret;
1005}
1006
1007void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
1008{
1009 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
1010}
1011
Johannes Berg712b24a2014-08-04 14:14:14 +02001012void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1013{
1014 lockdep_assert_held(&mvm->mutex);
1015
1016 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1017}
1018
Johannes Berg8ca151b2013-01-24 14:25:36 +01001019/*
1020 * Send the add station command for the vif's broadcast station.
1021 * Assumes that the station was already allocated.
1022 *
1023 * @mvm: the mvm component
1024 * @vif: the interface to which the broadcast station is added
1025 * @bsta: the broadcast station to add.
1026 */
Johannes Berg013290a2014-08-04 13:38:48 +02001027int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001028{
1029 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001030 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +02001031 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +01001032 const u8 *baddr = _baddr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001033
1034 lockdep_assert_held(&mvm->mutex);
1035
Liad Kaufmande24f632015-08-04 15:19:18 +03001036 if (iwl_mvm_is_dqa_supported(mvm)) {
1037 struct iwl_trans_txq_scd_cfg cfg = {
1038 .fifo = IWL_MVM_TX_FIFO_VO,
1039 .sta_id = mvmvif->bcast_sta.sta_id,
1040 .tid = IWL_MAX_TID_COUNT,
1041 .aggregate = false,
1042 .frame_limit = IWL_FRAME_LIMIT,
1043 };
1044 unsigned int wdg_timeout =
1045 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1046 int queue;
1047
1048 if ((vif->type == NL80211_IFTYPE_AP) &&
1049 (mvmvif->bcast_sta.tfd_queue_msk &
1050 BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE)))
1051 queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
Liad Kaufman4c965132015-08-09 19:26:56 +03001052 else if ((vif->type == NL80211_IFTYPE_P2P_DEVICE) &&
1053 (mvmvif->bcast_sta.tfd_queue_msk &
1054 BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE)))
1055 queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
Liad Kaufmande24f632015-08-04 15:19:18 +03001056 else if (WARN(1, "Missed required TXQ for adding bcast STA\n"))
1057 return -EINVAL;
1058
1059 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0, &cfg,
1060 wdg_timeout);
1061 }
1062
Johannes Berg5023d962013-07-31 14:07:43 +02001063 if (vif->type == NL80211_IFTYPE_ADHOC)
1064 baddr = vif->bss_conf.bssid;
1065
Johannes Berg8ca151b2013-01-24 14:25:36 +01001066 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
1067 return -ENOSPC;
1068
1069 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
1070 mvmvif->id, mvmvif->color);
1071}
1072
1073/* Send the FW a request to remove the station from it's internal data
1074 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +02001075int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001076{
Johannes Berg013290a2014-08-04 13:38:48 +02001077 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001078 int ret;
1079
1080 lockdep_assert_held(&mvm->mutex);
1081
Johannes Berg013290a2014-08-04 13:38:48 +02001082 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001083 if (ret)
1084 IWL_WARN(mvm, "Failed sending remove station\n");
1085 return ret;
1086}
1087
Johannes Berg013290a2014-08-04 13:38:48 +02001088int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1089{
1090 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Liad Kaufmande24f632015-08-04 15:19:18 +03001091 u32 qmask = 0;
Johannes Berg013290a2014-08-04 13:38:48 +02001092
1093 lockdep_assert_held(&mvm->mutex);
1094
Liad Kaufmande24f632015-08-04 15:19:18 +03001095 if (!iwl_mvm_is_dqa_supported(mvm))
1096 qmask = iwl_mvm_mac_get_queues_mask(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001097
Liad Kaufmande24f632015-08-04 15:19:18 +03001098 if (vif->type == NL80211_IFTYPE_AP) {
1099 /*
1100 * The firmware defines the TFD queue mask to only be relevant
1101 * for *unicast* queues, so the multicast (CAB) queue shouldn't
1102 * be included.
1103 */
Johannes Berg013290a2014-08-04 13:38:48 +02001104 qmask &= ~BIT(vif->cab_queue);
1105
Liad Kaufmande24f632015-08-04 15:19:18 +03001106 if (iwl_mvm_is_dqa_supported(mvm))
1107 qmask |= BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE);
Liad Kaufman4c965132015-08-09 19:26:56 +03001108 } else if (iwl_mvm_is_dqa_supported(mvm) &&
1109 vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1110 qmask |= BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE);
Liad Kaufmande24f632015-08-04 15:19:18 +03001111 }
1112
Johannes Berg013290a2014-08-04 13:38:48 +02001113 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
1114 ieee80211_vif_type_p2p(vif));
1115}
1116
Johannes Berg8ca151b2013-01-24 14:25:36 +01001117/* Allocate a new station entry for the broadcast station to the given vif,
1118 * and send it to the FW.
1119 * Note that each P2P mac should have its own broadcast station.
1120 *
1121 * @mvm: the mvm component
1122 * @vif: the interface to which the broadcast station is added
1123 * @bsta: the broadcast station to add. */
Johannes Berg013290a2014-08-04 13:38:48 +02001124int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001125{
1126 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001127 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001128 int ret;
1129
1130 lockdep_assert_held(&mvm->mutex);
1131
Johannes Berg013290a2014-08-04 13:38:48 +02001132 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001133 if (ret)
1134 return ret;
1135
Johannes Berg013290a2014-08-04 13:38:48 +02001136 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001137
1138 if (ret)
1139 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +02001140
Johannes Berg8ca151b2013-01-24 14:25:36 +01001141 return ret;
1142}
1143
Johannes Berg013290a2014-08-04 13:38:48 +02001144void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1145{
1146 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1147
1148 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
1149}
1150
Johannes Berg8ca151b2013-01-24 14:25:36 +01001151/*
1152 * Send the FW a request to remove the station from it's internal data
1153 * structures, and in addition remove it from the local data structure.
1154 */
Johannes Berg013290a2014-08-04 13:38:48 +02001155int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001156{
1157 int ret;
1158
1159 lockdep_assert_held(&mvm->mutex);
1160
Johannes Berg013290a2014-08-04 13:38:48 +02001161 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001162
Johannes Berg013290a2014-08-04 13:38:48 +02001163 iwl_mvm_dealloc_bcast_sta(mvm, vif);
1164
Johannes Berg8ca151b2013-01-24 14:25:36 +01001165 return ret;
1166}
1167
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03001168#define IWL_MAX_RX_BA_SESSIONS 16
1169
Sara Sharon10b2b202016-03-20 16:23:41 +02001170static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm)
1171{
1172 struct iwl_mvm_internal_rxq_notif data = {
1173 .type = IWL_MVM_RXQ_EMPTY,
1174 .sync = 1,
1175 };
1176
1177 iwl_mvm_sync_rx_queues_internal(mvm, &data, sizeof(data));
1178}
1179
Johannes Berg8ca151b2013-01-24 14:25:36 +01001180int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Sara Sharon10b2b202016-03-20 16:23:41 +02001181 int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001182{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001183 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001184 struct iwl_mvm_add_sta_cmd cmd = {};
Sara Sharon10b2b202016-03-20 16:23:41 +02001185 struct iwl_mvm_baid_data *baid_data = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001186 int ret;
1187 u32 status;
1188
1189 lockdep_assert_held(&mvm->mutex);
1190
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03001191 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
1192 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
1193 return -ENOSPC;
1194 }
1195
Sara Sharon10b2b202016-03-20 16:23:41 +02001196 if (iwl_mvm_has_new_rx_api(mvm) && start) {
1197 /*
1198 * Allocate here so if allocation fails we can bail out early
1199 * before starting the BA session in the firmware
1200 */
1201 baid_data = kzalloc(sizeof(*baid_data), GFP_KERNEL);
1202 if (!baid_data)
1203 return -ENOMEM;
1204 }
1205
Johannes Berg8ca151b2013-01-24 14:25:36 +01001206 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1207 cmd.sta_id = mvm_sta->sta_id;
1208 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03001209 if (start) {
1210 cmd.add_immediate_ba_tid = (u8) tid;
1211 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
Sara Sharon854c5702016-01-26 13:17:47 +02001212 cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03001213 } else {
1214 cmd.remove_immediate_ba_tid = (u8) tid;
1215 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001216 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
1217 STA_MODIFY_REMOVE_BA_TID;
1218
1219 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001220 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1221 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001222 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001223 if (ret)
Sara Sharon10b2b202016-03-20 16:23:41 +02001224 goto out_free;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001225
Sara Sharon837c4da2016-01-07 16:50:45 +02001226 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001227 case ADD_STA_SUCCESS:
1228 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
1229 start ? "start" : "stopp");
1230 break;
1231 case ADD_STA_IMMEDIATE_BA_FAILURE:
1232 IWL_WARN(mvm, "RX BA Session refused by fw\n");
1233 ret = -ENOSPC;
1234 break;
1235 default:
1236 ret = -EIO;
1237 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
1238 start ? "start" : "stopp", status);
1239 break;
1240 }
1241
Sara Sharon10b2b202016-03-20 16:23:41 +02001242 if (ret)
1243 goto out_free;
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03001244
Sara Sharon10b2b202016-03-20 16:23:41 +02001245 if (start) {
1246 u8 baid;
1247
1248 mvm->rx_ba_sessions++;
1249
1250 if (!iwl_mvm_has_new_rx_api(mvm))
1251 return 0;
1252
1253 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
1254 ret = -EINVAL;
1255 goto out_free;
1256 }
1257 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
1258 IWL_ADD_STA_BAID_SHIFT);
1259 baid_data->baid = baid;
1260 baid_data->timeout = timeout;
1261 baid_data->last_rx = jiffies;
1262 init_timer(&baid_data->session_timer);
1263 baid_data->session_timer.function =
1264 iwl_mvm_rx_agg_session_expired;
1265 baid_data->session_timer.data =
1266 (unsigned long)&mvm->baid_map[baid];
1267 baid_data->mvm = mvm;
1268 baid_data->tid = tid;
1269 baid_data->sta_id = mvm_sta->sta_id;
1270
1271 mvm_sta->tid_to_baid[tid] = baid;
1272 if (timeout)
1273 mod_timer(&baid_data->session_timer,
1274 TU_TO_EXP_TIME(timeout * 2));
1275
1276 /*
1277 * protect the BA data with RCU to cover a case where our
1278 * internal RX sync mechanism will timeout (not that it's
1279 * supposed to happen) and we will free the session data while
1280 * RX is being processed in parallel
1281 */
1282 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
1283 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
1284 } else if (mvm->rx_ba_sessions > 0) {
1285 u8 baid = mvm_sta->tid_to_baid[tid];
1286
1287 /* check that restart flow didn't zero the counter */
1288 mvm->rx_ba_sessions--;
1289 if (!iwl_mvm_has_new_rx_api(mvm))
1290 return 0;
1291
1292 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
1293 return -EINVAL;
1294
1295 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
1296 if (WARN_ON(!baid_data))
1297 return -EINVAL;
1298
1299 /* synchronize all rx queues so we can safely delete */
1300 iwl_mvm_sync_rxq_del_ba(mvm);
1301 del_timer_sync(&baid_data->session_timer);
1302
1303 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
1304 kfree_rcu(baid_data, rcu_head);
1305 }
1306 return 0;
1307
1308out_free:
1309 kfree(baid_data);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001310 return ret;
1311}
1312
1313static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1314 int tid, u8 queue, bool start)
1315{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001316 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001317 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001318 int ret;
1319 u32 status;
1320
1321 lockdep_assert_held(&mvm->mutex);
1322
1323 if (start) {
1324 mvm_sta->tfd_queue_msk |= BIT(queue);
1325 mvm_sta->tid_disable_agg &= ~BIT(tid);
1326 } else {
1327 mvm_sta->tfd_queue_msk &= ~BIT(queue);
1328 mvm_sta->tid_disable_agg |= BIT(tid);
1329 }
1330
1331 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1332 cmd.sta_id = mvm_sta->sta_id;
1333 cmd.add_modify = STA_MODE_MODIFY;
1334 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
1335 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
1336 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
1337
1338 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001339 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1340 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001341 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001342 if (ret)
1343 return ret;
1344
Sara Sharon837c4da2016-01-07 16:50:45 +02001345 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001346 case ADD_STA_SUCCESS:
1347 break;
1348 default:
1349 ret = -EIO;
1350 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
1351 start ? "start" : "stopp", status);
1352 break;
1353 }
1354
1355 return ret;
1356}
1357
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +02001358const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001359 IEEE80211_AC_BE,
1360 IEEE80211_AC_BK,
1361 IEEE80211_AC_BK,
1362 IEEE80211_AC_BE,
1363 IEEE80211_AC_VI,
1364 IEEE80211_AC_VI,
1365 IEEE80211_AC_VO,
1366 IEEE80211_AC_VO,
1367};
1368
Johannes Berg3e56ead2013-02-15 22:23:18 +01001369static const u8 tid_to_ucode_ac[] = {
1370 AC_BE,
1371 AC_BK,
1372 AC_BK,
1373 AC_BE,
1374 AC_VI,
1375 AC_VI,
1376 AC_VO,
1377 AC_VO,
1378};
1379
Johannes Berg8ca151b2013-01-24 14:25:36 +01001380int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1381 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1382{
Johannes Berg5b577a92013-11-14 18:20:04 +01001383 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001384 struct iwl_mvm_tid_data *tid_data;
1385 int txq_id;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001386 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001387
1388 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
1389 return -EINVAL;
1390
1391 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
1392 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
1393 mvmsta->tid_data[tid].state);
1394 return -ENXIO;
1395 }
1396
1397 lockdep_assert_held(&mvm->mutex);
1398
Arik Nemtsovb2492502014-03-13 12:21:50 +02001399 spin_lock_bh(&mvmsta->lock);
1400
1401 /* possible race condition - we entered D0i3 while starting agg */
1402 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
1403 spin_unlock_bh(&mvmsta->lock);
1404 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
1405 return -EIO;
1406 }
1407
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001408 spin_lock_bh(&mvm->queue_info_lock);
1409
1410 txq_id = iwl_mvm_find_free_queue(mvm, mvm->first_agg_queue,
1411 mvm->last_agg_queue);
1412 if (txq_id < 0) {
1413 ret = txq_id;
1414 spin_unlock_bh(&mvm->queue_info_lock);
1415 IWL_ERR(mvm, "Failed to allocate agg queue\n");
1416 goto release_locks;
1417 }
1418 mvm->queue_info[txq_id].setup_reserved = true;
1419 spin_unlock_bh(&mvm->queue_info_lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001420
Johannes Berg8ca151b2013-01-24 14:25:36 +01001421 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +01001422 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001423 tid_data->txq_id = txq_id;
1424 *ssn = tid_data->ssn;
1425
1426 IWL_DEBUG_TX_QUEUES(mvm,
1427 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
1428 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
1429 tid_data->next_reclaimed);
1430
1431 if (tid_data->ssn == tid_data->next_reclaimed) {
1432 tid_data->state = IWL_AGG_STARTING;
1433 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1434 } else {
1435 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
1436 }
1437
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001438 ret = 0;
1439
1440release_locks:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001441 spin_unlock_bh(&mvmsta->lock);
1442
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001443 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001444}
1445
1446int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02001447 struct ieee80211_sta *sta, u16 tid, u8 buf_size,
1448 bool amsdu)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001449{
Johannes Berg5b577a92013-11-14 18:20:04 +01001450 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001451 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02001452 unsigned int wdg_timeout =
1453 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02001454 int queue, ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001455 u16 ssn;
1456
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02001457 struct iwl_trans_txq_scd_cfg cfg = {
1458 .sta_id = mvmsta->sta_id,
1459 .tid = tid,
1460 .frame_limit = buf_size,
1461 .aggregate = true,
1462 };
1463
Eyal Shapiraefed6642014-09-14 15:58:53 +03001464 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
1465 != IWL_MAX_TID_COUNT);
1466
Johannes Berg8ca151b2013-01-24 14:25:36 +01001467 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
1468
1469 spin_lock_bh(&mvmsta->lock);
1470 ssn = tid_data->ssn;
1471 queue = tid_data->txq_id;
1472 tid_data->state = IWL_AGG_ON;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001473 mvmsta->agg_tids |= BIT(tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001474 tid_data->ssn = 0xffff;
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02001475 tid_data->amsdu_in_ampdu_allowed = amsdu;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001476 spin_unlock_bh(&mvmsta->lock);
1477
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02001478 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001479
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02001480 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[tid_to_mac80211_ac[tid]],
1481 ssn, &cfg, wdg_timeout);
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03001482
Johannes Berg8ca151b2013-01-24 14:25:36 +01001483 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1484 if (ret)
1485 return -EIO;
1486
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001487 /* No need to mark as reserved */
1488 spin_lock_bh(&mvm->queue_info_lock);
1489 mvm->queue_info[queue].setup_reserved = false;
1490 spin_unlock_bh(&mvm->queue_info_lock);
1491
Johannes Berg8ca151b2013-01-24 14:25:36 +01001492 /*
1493 * Even though in theory the peer could have different
1494 * aggregation reorder buffer sizes for different sessions,
1495 * our ucode doesn't allow for that and has a global limit
1496 * for each station. Therefore, use the minimum of all the
1497 * aggregation sessions and our default value.
1498 */
1499 mvmsta->max_agg_bufsize =
1500 min(mvmsta->max_agg_bufsize, buf_size);
1501 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
1502
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03001503 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
1504 sta->addr, tid);
1505
Eyal Shapira9e680942013-11-09 00:16:16 +02001506 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001507}
1508
1509int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1510 struct ieee80211_sta *sta, u16 tid)
1511{
Johannes Berg5b577a92013-11-14 18:20:04 +01001512 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001513 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1514 u16 txq_id;
1515 int err;
1516
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +02001517
1518 /*
1519 * If mac80211 is cleaning its state, then say that we finished since
1520 * our state has been cleared anyway.
1521 */
1522 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1523 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1524 return 0;
1525 }
1526
Johannes Berg8ca151b2013-01-24 14:25:36 +01001527 spin_lock_bh(&mvmsta->lock);
1528
1529 txq_id = tid_data->txq_id;
1530
1531 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
1532 mvmsta->sta_id, tid, txq_id, tid_data->state);
1533
Eyal Shapiraefed6642014-09-14 15:58:53 +03001534 mvmsta->agg_tids &= ~BIT(tid);
1535
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001536 /* No need to mark as reserved anymore */
1537 spin_lock_bh(&mvm->queue_info_lock);
1538 mvm->queue_info[txq_id].setup_reserved = false;
1539 spin_unlock_bh(&mvm->queue_info_lock);
1540
Johannes Berg8ca151b2013-01-24 14:25:36 +01001541 switch (tid_data->state) {
1542 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01001543 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001544
1545 IWL_DEBUG_TX_QUEUES(mvm,
1546 "ssn = %d, next_recl = %d\n",
1547 tid_data->ssn, tid_data->next_reclaimed);
1548
1549 /* There are still packets for this RA / TID in the HW */
1550 if (tid_data->ssn != tid_data->next_reclaimed) {
1551 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
1552 err = 0;
1553 break;
1554 }
1555
1556 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +02001557 tid_data->state = IWL_AGG_OFF;
Johannes Bergf7f89e72014-08-05 15:24:44 +02001558 spin_unlock_bh(&mvmsta->lock);
1559
1560 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1561
1562 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1563
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001564 iwl_mvm_disable_txq(mvm, txq_id,
1565 vif->hw_queue[tid_to_mac80211_ac[tid]], tid,
1566 0);
Johannes Bergf7f89e72014-08-05 15:24:44 +02001567 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001568 case IWL_AGG_STARTING:
1569 case IWL_EMPTYING_HW_QUEUE_ADDBA:
1570 /*
1571 * The agg session has been stopped before it was set up. This
1572 * can happen when the AddBA timer times out for example.
1573 */
1574
1575 /* No barriers since we are under mutex */
1576 lockdep_assert_held(&mvm->mutex);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001577
1578 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1579 tid_data->state = IWL_AGG_OFF;
1580 err = 0;
1581 break;
1582 default:
1583 IWL_ERR(mvm,
1584 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
1585 mvmsta->sta_id, tid, tid_data->state);
1586 IWL_ERR(mvm,
1587 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
1588 err = -EINVAL;
1589 }
1590
1591 spin_unlock_bh(&mvmsta->lock);
1592
1593 return err;
1594}
1595
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001596int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1597 struct ieee80211_sta *sta, u16 tid)
1598{
Johannes Berg5b577a92013-11-14 18:20:04 +01001599 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001600 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1601 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02001602 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001603
1604 /*
1605 * First set the agg state to OFF to avoid calling
1606 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
1607 */
1608 spin_lock_bh(&mvmsta->lock);
1609 txq_id = tid_data->txq_id;
1610 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
1611 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001612 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001613 tid_data->state = IWL_AGG_OFF;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001614 mvmsta->agg_tids &= ~BIT(tid);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001615 spin_unlock_bh(&mvmsta->lock);
1616
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001617 /* No need to mark as reserved */
1618 spin_lock_bh(&mvm->queue_info_lock);
1619 mvm->queue_info[txq_id].setup_reserved = false;
1620 spin_unlock_bh(&mvm->queue_info_lock);
1621
Johannes Bergb6658ff2013-07-24 13:55:51 +02001622 if (old_state >= IWL_AGG_ON) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001623 iwl_mvm_drain_sta(mvm, mvmsta, true);
Luca Coelho5888a402015-10-06 09:54:57 +03001624 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
Johannes Bergb6658ff2013-07-24 13:55:51 +02001625 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001626 iwl_trans_wait_tx_queue_empty(mvm->trans,
1627 mvmsta->tfd_queue_msk);
1628 iwl_mvm_drain_sta(mvm, mvmsta, false);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001629
Johannes Bergf7f89e72014-08-05 15:24:44 +02001630 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1631
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001632 iwl_mvm_disable_txq(mvm, tid_data->txq_id,
1633 vif->hw_queue[tid_to_mac80211_ac[tid]], tid,
1634 0);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001635 }
1636
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001637 return 0;
1638}
1639
Johannes Berg8ca151b2013-01-24 14:25:36 +01001640static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
1641{
Johannes Berg2dc2a152015-06-16 17:09:18 +02001642 int i, max = -1, max_offs = -1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001643
1644 lockdep_assert_held(&mvm->mutex);
1645
Johannes Berg2dc2a152015-06-16 17:09:18 +02001646 /* Pick the unused key offset with the highest 'deleted'
1647 * counter. Every time a key is deleted, all the counters
1648 * are incremented and the one that was just deleted is
1649 * reset to zero. Thus, the highest counter is the one
1650 * that was deleted longest ago. Pick that one.
1651 */
1652 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
1653 if (test_bit(i, mvm->fw_key_table))
1654 continue;
1655 if (mvm->fw_key_deleted[i] > max) {
1656 max = mvm->fw_key_deleted[i];
1657 max_offs = i;
1658 }
1659 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001660
Johannes Berg2dc2a152015-06-16 17:09:18 +02001661 if (max_offs < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001662 return STA_KEY_IDX_INVALID;
1663
Johannes Berg2dc2a152015-06-16 17:09:18 +02001664 return max_offs;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001665}
1666
Johannes Berg5f7a1842015-12-11 09:36:10 +01001667static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
1668 struct ieee80211_vif *vif,
1669 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001670{
Johannes Berg5b530e92014-12-23 16:00:17 +01001671 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001672
Johannes Berg5f7a1842015-12-11 09:36:10 +01001673 if (sta)
1674 return iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001675
1676 /*
1677 * The device expects GTKs for station interfaces to be
1678 * installed as GTKs for the AP station. If we have no
1679 * station ID, then use AP's station ID.
1680 */
1681 if (vif->type == NL80211_IFTYPE_STATION &&
Avri Altman9513c5e2015-10-19 16:29:11 +02001682 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1683 u8 sta_id = mvmvif->ap_sta_id;
1684
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02001685 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
1686 lockdep_is_held(&mvm->mutex));
Avri Altman9513c5e2015-10-19 16:29:11 +02001687 /*
1688 * It is possible that the 'sta' parameter is NULL,
1689 * for example when a GTK is removed - the sta_id will then
1690 * be the AP ID, and no station was passed by mac80211.
1691 */
1692 if (IS_ERR_OR_NULL(sta))
Johannes Berg5f7a1842015-12-11 09:36:10 +01001693 return NULL;
Avri Altman9513c5e2015-10-19 16:29:11 +02001694
Johannes Berg5f7a1842015-12-11 09:36:10 +01001695 return iwl_mvm_sta_from_mac80211(sta);
Avri Altman9513c5e2015-10-19 16:29:11 +02001696 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001697
Johannes Berg5f7a1842015-12-11 09:36:10 +01001698 return NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001699}
1700
1701static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
1702 struct iwl_mvm_sta *mvm_sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01001703 struct ieee80211_key_conf *keyconf, bool mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001704 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
1705 u8 key_offset)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001706{
Max Stepanov5a258aa2013-04-07 09:11:21 +03001707 struct iwl_mvm_add_sta_key_cmd cmd = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001708 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01001709 int ret;
1710 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001711 u16 keyidx;
1712 int i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001713 u8 sta_id = mvm_sta->sta_id;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001714
1715 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1716 STA_KEY_FLG_KEYID_MSK;
1717 key_flags = cpu_to_le16(keyidx);
1718 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
1719
1720 switch (keyconf->cipher) {
1721 case WLAN_CIPHER_SUITE_TKIP:
1722 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001723 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001724 for (i = 0; i < 5; i++)
Max Stepanov5a258aa2013-04-07 09:11:21 +03001725 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1726 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001727 break;
1728 case WLAN_CIPHER_SUITE_CCMP:
1729 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001730 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001731 break;
Johannes Bergba3943b2014-11-12 23:54:48 +01001732 case WLAN_CIPHER_SUITE_WEP104:
1733 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
John W. Linvilleaa0cb082015-01-12 16:18:11 -05001734 /* fall through */
Johannes Bergba3943b2014-11-12 23:54:48 +01001735 case WLAN_CIPHER_SUITE_WEP40:
1736 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
1737 memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
1738 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001739 default:
Max Stepanove36e5432013-08-27 19:56:13 +03001740 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
1741 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001742 }
1743
Johannes Bergba3943b2014-11-12 23:54:48 +01001744 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001745 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1746
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001747 cmd.key_offset = key_offset;
Max Stepanov5a258aa2013-04-07 09:11:21 +03001748 cmd.key_flags = key_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001749 cmd.sta_id = sta_id;
1750
1751 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001752 if (cmd_flags & CMD_ASYNC)
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001753 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
1754 sizeof(cmd), &cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001755 else
1756 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1757 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001758
1759 switch (status) {
1760 case ADD_STA_SUCCESS:
1761 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1762 break;
1763 default:
1764 ret = -EIO;
1765 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1766 break;
1767 }
1768
1769 return ret;
1770}
1771
1772static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1773 struct ieee80211_key_conf *keyconf,
1774 u8 sta_id, bool remove_key)
1775{
1776 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1777
1778 /* verify the key details match the required command's expectations */
1779 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1780 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1781 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1782 return -EINVAL;
1783
1784 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1785 igtk_cmd.sta_id = cpu_to_le32(sta_id);
1786
1787 if (remove_key) {
1788 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1789 } else {
1790 struct ieee80211_key_seq seq;
1791 const u8 *pn;
1792
1793 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001794 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1795 pn = seq.aes_cmac.pn;
1796 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1797 ((u64) pn[4] << 8) |
1798 ((u64) pn[3] << 16) |
1799 ((u64) pn[2] << 24) |
1800 ((u64) pn[1] << 32) |
1801 ((u64) pn[0] << 40));
1802 }
1803
1804 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1805 remove_key ? "removing" : "installing",
1806 igtk_cmd.sta_id);
1807
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001808 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001809 sizeof(igtk_cmd), &igtk_cmd);
1810}
1811
1812
1813static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1814 struct ieee80211_vif *vif,
1815 struct ieee80211_sta *sta)
1816{
Johannes Berg5b530e92014-12-23 16:00:17 +01001817 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001818
1819 if (sta)
1820 return sta->addr;
1821
1822 if (vif->type == NL80211_IFTYPE_STATION &&
1823 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1824 u8 sta_id = mvmvif->ap_sta_id;
1825 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1826 lockdep_is_held(&mvm->mutex));
1827 return sta->addr;
1828 }
1829
1830
1831 return NULL;
1832}
1833
Johannes Berg2f6319d2014-11-12 23:39:56 +01001834static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1835 struct ieee80211_vif *vif,
1836 struct ieee80211_sta *sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01001837 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001838 u8 key_offset,
Johannes Bergba3943b2014-11-12 23:54:48 +01001839 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001840{
Johannes Berg2f6319d2014-11-12 23:39:56 +01001841 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001842 int ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001843 const u8 *addr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001844 struct ieee80211_key_seq seq;
1845 u16 p1k[5];
1846
Johannes Berg8ca151b2013-01-24 14:25:36 +01001847 switch (keyconf->cipher) {
1848 case WLAN_CIPHER_SUITE_TKIP:
1849 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1850 /* get phase 1 key from mac80211 */
1851 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1852 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Bergba3943b2014-11-12 23:54:48 +01001853 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001854 seq.tkip.iv32, p1k, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001855 break;
1856 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergba3943b2014-11-12 23:54:48 +01001857 case WLAN_CIPHER_SUITE_WEP40:
1858 case WLAN_CIPHER_SUITE_WEP104:
1859 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001860 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001861 break;
1862 default:
Johannes Bergba3943b2014-11-12 23:54:48 +01001863 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001864 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001865 }
1866
Johannes Berg8ca151b2013-01-24 14:25:36 +01001867 return ret;
1868}
1869
Johannes Berg2f6319d2014-11-12 23:39:56 +01001870static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
Johannes Bergba3943b2014-11-12 23:54:48 +01001871 struct ieee80211_key_conf *keyconf,
1872 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001873{
Max Stepanov5a258aa2013-04-07 09:11:21 +03001874 struct iwl_mvm_add_sta_key_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001875 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01001876 int ret;
1877 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001878
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02001879 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1880 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001881 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1882 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1883
Johannes Bergba3943b2014-11-12 23:54:48 +01001884 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001885 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1886
Max Stepanov5a258aa2013-04-07 09:11:21 +03001887 cmd.key_flags = key_flags;
1888 cmd.key_offset = keyconf->hw_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001889 cmd.sta_id = sta_id;
1890
Johannes Berg8ca151b2013-01-24 14:25:36 +01001891 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001892 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1893 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001894
1895 switch (status) {
1896 case ADD_STA_SUCCESS:
1897 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1898 break;
1899 default:
1900 ret = -EIO;
1901 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1902 break;
1903 }
1904
1905 return ret;
1906}
1907
Johannes Berg2f6319d2014-11-12 23:39:56 +01001908int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1909 struct ieee80211_vif *vif,
1910 struct ieee80211_sta *sta,
1911 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001912 u8 key_offset)
Johannes Berg2f6319d2014-11-12 23:39:56 +01001913{
Johannes Bergba3943b2014-11-12 23:54:48 +01001914 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01001915 struct iwl_mvm_sta *mvm_sta;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001916 u8 sta_id;
1917 int ret;
Matti Gottlieb11828db2015-06-01 15:15:11 +03001918 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
Johannes Berg2f6319d2014-11-12 23:39:56 +01001919
1920 lockdep_assert_held(&mvm->mutex);
1921
1922 /* Get the station id from the mvm local station table */
Johannes Berg5f7a1842015-12-11 09:36:10 +01001923 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
1924 if (!mvm_sta) {
1925 IWL_ERR(mvm, "Failed to find station\n");
Johannes Berg2f6319d2014-11-12 23:39:56 +01001926 return -EINVAL;
1927 }
Johannes Berg5f7a1842015-12-11 09:36:10 +01001928 sta_id = mvm_sta->sta_id;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001929
1930 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1931 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1932 goto end;
1933 }
1934
1935 /*
1936 * It is possible that the 'sta' parameter is NULL, and thus
1937 * there is a need to retrieve the sta from the local station table.
1938 */
1939 if (!sta) {
1940 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1941 lockdep_is_held(&mvm->mutex));
1942 if (IS_ERR_OR_NULL(sta)) {
1943 IWL_ERR(mvm, "Invalid station id\n");
1944 return -EINVAL;
1945 }
1946 }
1947
1948 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
1949 return -EINVAL;
1950
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001951 /* If the key_offset is not pre-assigned, we need to find a
1952 * new offset to use. In normal cases, the offset is not
1953 * pre-assigned, but during HW_RESTART we want to reuse the
1954 * same indices, so we pass them when this function is called.
1955 *
1956 * In D3 entry, we need to hardcoded the indices (because the
1957 * firmware hardcodes the PTK offset to 0). In this case, we
1958 * need to make sure we don't overwrite the hw_key_idx in the
1959 * keyconf structure, because otherwise we cannot configure
1960 * the original ones back when resuming.
1961 */
1962 if (key_offset == STA_KEY_IDX_INVALID) {
1963 key_offset = iwl_mvm_set_fw_key_idx(mvm);
1964 if (key_offset == STA_KEY_IDX_INVALID)
Johannes Berg2f6319d2014-11-12 23:39:56 +01001965 return -ENOSPC;
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001966 keyconf->hw_key_idx = key_offset;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001967 }
1968
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001969 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02001970 if (ret)
Johannes Bergba3943b2014-11-12 23:54:48 +01001971 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01001972
1973 /*
1974 * For WEP, the same key is used for multicast and unicast. Upload it
1975 * again, using the same key offset, and now pointing the other one
1976 * to the same key slot (offset).
1977 * If this fails, remove the original as well.
1978 */
1979 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
1980 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001981 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
1982 key_offset, !mcast);
Johannes Bergba3943b2014-11-12 23:54:48 +01001983 if (ret) {
Johannes Bergba3943b2014-11-12 23:54:48 +01001984 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02001985 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01001986 }
1987 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01001988
Luca Coelho9c3deeb2015-11-11 01:06:17 +02001989 __set_bit(key_offset, mvm->fw_key_table);
1990
Johannes Berg2f6319d2014-11-12 23:39:56 +01001991end:
1992 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1993 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Matti Gottlieb11828db2015-06-01 15:15:11 +03001994 sta ? sta->addr : zero_addr, ret);
Johannes Berg2f6319d2014-11-12 23:39:56 +01001995 return ret;
1996}
1997
1998int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1999 struct ieee80211_vif *vif,
2000 struct ieee80211_sta *sta,
2001 struct ieee80211_key_conf *keyconf)
2002{
Johannes Bergba3943b2014-11-12 23:54:48 +01002003 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01002004 struct iwl_mvm_sta *mvm_sta;
2005 u8 sta_id = IWL_MVM_STATION_COUNT;
Johannes Berg2dc2a152015-06-16 17:09:18 +02002006 int ret, i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002007
2008 lockdep_assert_held(&mvm->mutex);
2009
Johannes Berg5f7a1842015-12-11 09:36:10 +01002010 /* Get the station from the mvm local station table */
2011 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
Johannes Berg2f6319d2014-11-12 23:39:56 +01002012
2013 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
2014 keyconf->keyidx, sta_id);
2015
2016 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
2017 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
2018
2019 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
2020 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
2021 keyconf->hw_key_idx);
2022 return -ENOENT;
2023 }
2024
Johannes Berg2dc2a152015-06-16 17:09:18 +02002025 /* track which key was deleted last */
2026 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2027 if (mvm->fw_key_deleted[i] < U8_MAX)
2028 mvm->fw_key_deleted[i]++;
2029 }
2030 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
2031
Johannes Berg5f7a1842015-12-11 09:36:10 +01002032 if (!mvm_sta) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01002033 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
2034 return 0;
2035 }
2036
Johannes Berg5f7a1842015-12-11 09:36:10 +01002037 sta_id = mvm_sta->sta_id;
2038
Johannes Bergba3943b2014-11-12 23:54:48 +01002039 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
2040 if (ret)
2041 return ret;
2042
2043 /* delete WEP key twice to get rid of (now useless) offset */
2044 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2045 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
2046 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
2047
2048 return ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002049}
2050
Johannes Berg8ca151b2013-01-24 14:25:36 +01002051void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
2052 struct ieee80211_vif *vif,
2053 struct ieee80211_key_conf *keyconf,
2054 struct ieee80211_sta *sta, u32 iv32,
2055 u16 *phase1key)
2056{
Beni Levc3eb5362013-02-06 17:22:18 +02002057 struct iwl_mvm_sta *mvm_sta;
Johannes Bergba3943b2014-11-12 23:54:48 +01002058 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002059
Beni Levc3eb5362013-02-06 17:22:18 +02002060 rcu_read_lock();
2061
Johannes Berg5f7a1842015-12-11 09:36:10 +01002062 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
2063 if (WARN_ON_ONCE(!mvm_sta))
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02002064 goto unlock;
Johannes Bergba3943b2014-11-12 23:54:48 +01002065 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002066 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02002067
2068 unlock:
Beni Levc3eb5362013-02-06 17:22:18 +02002069 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01002070}
2071
Johannes Berg9cc40712013-02-15 22:47:48 +01002072void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
2073 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002074{
Johannes Berg5b577a92013-11-14 18:20:04 +01002075 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002076 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002077 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01002078 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03002079 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01002080 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01002081 };
2082 int ret;
2083
Sara Sharon854c5702016-01-26 13:17:47 +02002084 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
2085 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002086 if (ret)
2087 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2088}
2089
Johannes Berg9cc40712013-02-15 22:47:48 +01002090void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
2091 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002092 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01002093 u16 cnt, u16 tids, bool more_data,
2094 bool agg)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002095{
Johannes Berg5b577a92013-11-14 18:20:04 +01002096 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002097 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002098 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01002099 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002100 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
2101 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01002102 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01002103 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01002104 int tid, ret;
2105 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002106
Johannes Berg3e56ead2013-02-15 22:23:18 +01002107 /* convert TIDs to ACs - we don't support TSPEC so that's OK
2108 * Note that this field is reserved and unused by firmware not
2109 * supporting GO uAPSD, so it's safe to always do this.
2110 */
2111 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
2112 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
2113
2114 /* If we're releasing frames from aggregation queues then check if the
2115 * all queues combined that we're releasing frames from have
2116 * - more frames than the service period, in which case more_data
2117 * needs to be set
2118 * - fewer than 'cnt' frames, in which case we need to adjust the
2119 * firmware command (but do that unconditionally)
2120 */
2121 if (agg) {
2122 int remaining = cnt;
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02002123 int sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01002124
2125 spin_lock_bh(&mvmsta->lock);
2126 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
2127 struct iwl_mvm_tid_data *tid_data;
2128 u16 n_queued;
2129
2130 tid_data = &mvmsta->tid_data[tid];
2131 if (WARN(tid_data->state != IWL_AGG_ON &&
2132 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
2133 "TID %d state is %d\n",
2134 tid, tid_data->state)) {
2135 spin_unlock_bh(&mvmsta->lock);
2136 ieee80211_sta_eosp(sta);
2137 return;
2138 }
2139
2140 n_queued = iwl_mvm_tid_queued(tid_data);
2141 if (n_queued > remaining) {
2142 more_data = true;
2143 remaining = 0;
2144 break;
2145 }
2146 remaining -= n_queued;
2147 }
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02002148 sleep_tx_count = cnt - remaining;
2149 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
2150 mvmsta->sleep_tx_count = sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01002151 spin_unlock_bh(&mvmsta->lock);
2152
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02002153 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
Johannes Berg3e56ead2013-02-15 22:23:18 +01002154 if (WARN_ON(cnt - remaining == 0)) {
2155 ieee80211_sta_eosp(sta);
2156 return;
2157 }
2158 }
2159
2160 /* Note: this is ignored by firmware not supporting GO uAPSD */
2161 if (more_data)
2162 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
2163
2164 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
2165 mvmsta->next_status_eosp = true;
2166 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
2167 } else {
2168 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
2169 }
2170
Emmanuel Grumbach156f92f2015-11-24 14:55:18 +02002171 /* block the Tx queues until the FW updated the sleep Tx count */
2172 iwl_trans_block_txq_ptrs(mvm->trans, true);
2173
2174 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
2175 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
Sara Sharon854c5702016-01-26 13:17:47 +02002176 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002177 if (ret)
2178 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2179}
Johannes Berg3e56ead2013-02-15 22:23:18 +01002180
Johannes Berg04168412015-06-23 21:22:09 +02002181void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
2182 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg3e56ead2013-02-15 22:23:18 +01002183{
2184 struct iwl_rx_packet *pkt = rxb_addr(rxb);
2185 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
2186 struct ieee80211_sta *sta;
2187 u32 sta_id = le32_to_cpu(notif->sta_id);
2188
2189 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +02002190 return;
Johannes Berg3e56ead2013-02-15 22:23:18 +01002191
2192 rcu_read_lock();
2193 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
2194 if (!IS_ERR_OR_NULL(sta))
2195 ieee80211_sta_eosp(sta);
2196 rcu_read_unlock();
Johannes Berg3e56ead2013-02-15 22:23:18 +01002197}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03002198
2199void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
2200 struct iwl_mvm_sta *mvmsta, bool disable)
2201{
2202 struct iwl_mvm_add_sta_cmd cmd = {
2203 .add_modify = STA_MODE_MODIFY,
2204 .sta_id = mvmsta->sta_id,
2205 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
2206 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
2207 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
2208 };
2209 int ret;
2210
Sara Sharon854c5702016-01-26 13:17:47 +02002211 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
2212 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03002213 if (ret)
2214 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2215}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03002216
2217void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
2218 struct ieee80211_sta *sta,
2219 bool disable)
2220{
2221 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2222
2223 spin_lock_bh(&mvm_sta->lock);
2224
2225 if (mvm_sta->disable_tx == disable) {
2226 spin_unlock_bh(&mvm_sta->lock);
2227 return;
2228 }
2229
2230 mvm_sta->disable_tx = disable;
2231
2232 /*
Sara Sharon0d365ae2015-03-31 12:24:05 +03002233 * Tell mac80211 to start/stop queuing tx for this station,
2234 * but don't stop queuing if there are still pending frames
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03002235 * for this station.
2236 */
2237 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
2238 ieee80211_sta_block_awake(mvm->hw, sta, disable);
2239
2240 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
2241
2242 spin_unlock_bh(&mvm_sta->lock);
2243}
2244
2245void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
2246 struct iwl_mvm_vif *mvmvif,
2247 bool disable)
2248{
2249 struct ieee80211_sta *sta;
2250 struct iwl_mvm_sta *mvm_sta;
2251 int i;
2252
2253 lockdep_assert_held(&mvm->mutex);
2254
2255 /* Block/unblock all the stations of the given mvmvif */
2256 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
2257 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
2258 lockdep_is_held(&mvm->mutex));
2259 if (IS_ERR_OR_NULL(sta))
2260 continue;
2261
2262 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2263 if (mvm_sta->mac_id_n_color !=
2264 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
2265 continue;
2266
2267 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
2268 }
2269}
Luciano Coelhodc88b4b2014-11-10 11:10:14 +02002270
2271void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2272{
2273 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2274 struct iwl_mvm_sta *mvmsta;
2275
2276 rcu_read_lock();
2277
2278 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
2279
2280 if (!WARN_ON(!mvmsta))
2281 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
2282
2283 rcu_read_unlock();
2284}