blob: fe2f53850d36f6b306b0d543fb8a26ed8777a79e [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
Johannes Berg8ca151b2013-01-24 14:25:36 +010010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
23 * USA
24 *
25 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020026 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010027 *
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 *
32 * BSD LICENSE
33 *
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +030034 * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
35 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010036 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 *
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in
46 * the documentation and/or other materials provided with the
47 * distribution.
48 * * Neither the name Intel Corporation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *
64 *****************************************************************************/
65#include <net/mac80211.h>
66
67#include "mvm.h"
68#include "sta.h"
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +030069#include "rs.h"
Johannes Berg8ca151b2013-01-24 14:25:36 +010070
Eliad Pellerb92e6612014-01-23 17:58:23 +020071static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
72 enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +010073{
74 int sta_id;
Eliad Pellerb92e6612014-01-23 17:58:23 +020075 u32 reserved_ids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +010076
Eliad Pellerb92e6612014-01-23 17:58:23 +020077 BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
Johannes Berg8ca151b2013-01-24 14:25:36 +010078 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
79
80 lockdep_assert_held(&mvm->mutex);
81
Eliad Pellerb92e6612014-01-23 17:58:23 +020082 /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
83 if (iftype != NL80211_IFTYPE_STATION)
84 reserved_ids = BIT(0);
85
Johannes Berg8ca151b2013-01-24 14:25:36 +010086 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
Eliad Pellerb92e6612014-01-23 17:58:23 +020087 for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++) {
88 if (BIT(sta_id) & reserved_ids)
89 continue;
90
Johannes Berg8ca151b2013-01-24 14:25:36 +010091 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
92 lockdep_is_held(&mvm->mutex)))
93 return sta_id;
Eliad Pellerb92e6612014-01-23 17:58:23 +020094 }
Johannes Berg8ca151b2013-01-24 14:25:36 +010095 return IWL_MVM_STATION_COUNT;
96}
97
Johannes Berg7a453972013-02-12 13:10:44 +010098/* send station add/update command to firmware */
99int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
100 bool update)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100101{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100102 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbach4b8265a2014-07-13 08:58:04 +0300103 struct iwl_mvm_add_sta_cmd add_sta_cmd = {
104 .sta_id = mvm_sta->sta_id,
105 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
106 .add_modify = update ? 1 : 0,
107 .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
108 STA_FLG_MIMO_EN_MSK),
109 };
Johannes Berg8ca151b2013-01-24 14:25:36 +0100110 int ret;
111 u32 status;
112 u32 agg_size = 0, mpdu_dens = 0;
113
Johannes Berg7a453972013-02-12 13:10:44 +0100114 if (!update) {
115 add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
116 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
117 }
Johannes Berg5bc5aaa2013-02-12 14:35:36 +0100118
119 switch (sta->bandwidth) {
120 case IEEE80211_STA_RX_BW_160:
121 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
122 /* fall through */
123 case IEEE80211_STA_RX_BW_80:
124 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
125 /* fall through */
126 case IEEE80211_STA_RX_BW_40:
127 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
128 /* fall through */
129 case IEEE80211_STA_RX_BW_20:
130 if (sta->ht_cap.ht_supported)
131 add_sta_cmd.station_flags |=
132 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
133 break;
134 }
135
136 switch (sta->rx_nss) {
137 case 1:
138 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
139 break;
140 case 2:
141 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
142 break;
143 case 3 ... 8:
144 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
145 break;
146 }
147
148 switch (sta->smps_mode) {
149 case IEEE80211_SMPS_AUTOMATIC:
150 case IEEE80211_SMPS_NUM_MODES:
151 WARN_ON(1);
152 break;
153 case IEEE80211_SMPS_STATIC:
154 /* override NSS */
155 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
156 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
157 break;
158 case IEEE80211_SMPS_DYNAMIC:
159 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
160 break;
161 case IEEE80211_SMPS_OFF:
162 /* nothing */
163 break;
164 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100165
166 if (sta->ht_cap.ht_supported) {
167 add_sta_cmd.station_flags_msk |=
168 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
169 STA_FLG_AGG_MPDU_DENS_MSK);
170
171 mpdu_dens = sta->ht_cap.ampdu_density;
172 }
173
174 if (sta->vht_cap.vht_supported) {
175 agg_size = sta->vht_cap.cap &
176 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
177 agg_size >>=
178 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
179 } else if (sta->ht_cap.ht_supported) {
180 agg_size = sta->ht_cap.ampdu_factor;
181 }
182
183 add_sta_cmd.station_flags |=
184 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
185 add_sta_cmd.station_flags |=
186 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
187
188 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300189 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(add_sta_cmd),
190 &add_sta_cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100191 if (ret)
192 return ret;
193
194 switch (status) {
195 case ADD_STA_SUCCESS:
196 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
197 break;
198 default:
199 ret = -EIO;
200 IWL_ERR(mvm, "ADD_STA failed\n");
201 break;
202 }
203
204 return ret;
205}
206
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300207static int iwl_mvm_tdls_sta_init(struct iwl_mvm *mvm,
208 struct ieee80211_sta *sta)
209{
210 unsigned long used_hw_queues;
211 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +0200212 unsigned int wdg_timeout =
213 iwl_mvm_get_wd_timeout(mvm, NULL, true, false);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300214 u32 ac;
215
216 lockdep_assert_held(&mvm->mutex);
217
218 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, NULL);
219
220 /* Find available queues, and allocate them to the ACs */
221 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
222 u8 queue = find_first_zero_bit(&used_hw_queues,
223 mvm->first_agg_queue);
224
225 if (queue >= mvm->first_agg_queue) {
226 IWL_ERR(mvm, "Failed to allocate STA queue\n");
227 return -EBUSY;
228 }
229
230 __set_bit(queue, &used_hw_queues);
231 mvmsta->hw_queue[ac] = queue;
232 }
233
234 /* Found a place for all queues - enable them */
235 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
236 iwl_mvm_enable_ac_txq(mvm, mvmsta->hw_queue[ac],
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200237 iwl_mvm_ac_to_tx_fifo[ac], wdg_timeout);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300238 mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
239 }
240
241 return 0;
242}
243
244static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
245 struct ieee80211_sta *sta)
246{
247 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
248 unsigned long sta_msk;
249 int i;
250
251 lockdep_assert_held(&mvm->mutex);
252
253 /* disable the TDLS STA-specific queues */
254 sta_msk = mvmsta->tfd_queue_msk;
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +0200255 for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
Emmanuel Grumbach0294d9e2015-01-05 16:52:55 +0200256 iwl_mvm_disable_txq(mvm, i, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300257}
258
Johannes Berg8ca151b2013-01-24 14:25:36 +0100259int iwl_mvm_add_sta(struct iwl_mvm *mvm,
260 struct ieee80211_vif *vif,
261 struct ieee80211_sta *sta)
262{
263 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100264 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100265 int i, ret, sta_id;
266
267 lockdep_assert_held(&mvm->mutex);
268
269 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
Eliad Pellerb92e6612014-01-23 17:58:23 +0200270 sta_id = iwl_mvm_find_free_sta_id(mvm,
271 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100272 else
273 sta_id = mvm_sta->sta_id;
274
Johannes Berg36f46312015-03-10 20:32:08 +0100275 if (sta_id == IWL_MVM_STATION_COUNT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100276 return -ENOSPC;
277
Gregory Greenman94939082015-08-24 14:38:35 +0300278 if (vif->type == NL80211_IFTYPE_AP) {
279 mvmvif->ap_assoc_sta_count++;
280 iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, FW_CTXT_ACTION_MODIFY);
281 }
282
Johannes Berg8ca151b2013-01-24 14:25:36 +0100283 spin_lock_init(&mvm_sta->lock);
284
285 mvm_sta->sta_id = sta_id;
286 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
287 mvmvif->color);
288 mvm_sta->vif = vif;
289 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300290 mvm_sta->tx_protection = 0;
291 mvm_sta->tt_tx_protection = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100292
293 /* HW restart, don't assume the memory has been zeroed */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300294 atomic_set(&mvm->pending_frames[sta_id], 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100295 mvm_sta->tid_disable_agg = 0;
296 mvm_sta->tfd_queue_msk = 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300297
298 /* allocate new queues for a TDLS station */
299 if (sta->tdls) {
300 ret = iwl_mvm_tdls_sta_init(mvm, sta);
301 if (ret)
302 return ret;
303 } else {
304 for (i = 0; i < IEEE80211_NUM_ACS; i++)
305 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
306 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
307 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100308
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200309 /* for HW restart - reset everything but the sequence number */
310 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
311 u16 seq = mvm_sta->tid_data[i].seq_number;
312 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
313 mvm_sta->tid_data[i].seq_number = seq;
314 }
Eyal Shapiraefed6642014-09-14 15:58:53 +0300315 mvm_sta->agg_tids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100316
Johannes Berg7a453972013-02-12 13:10:44 +0100317 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100318 if (ret)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300319 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100320
Johannes Berg9e848012014-08-04 14:33:42 +0200321 if (vif->type == NL80211_IFTYPE_STATION) {
322 if (!sta->tdls) {
323 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
324 mvmvif->ap_sta_id = sta_id;
325 } else {
326 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
327 }
328 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100329
330 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
331
332 return 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300333
334err:
335 iwl_mvm_tdls_sta_deinit(mvm, sta);
336 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100337}
338
Johannes Berg7a453972013-02-12 13:10:44 +0100339int iwl_mvm_update_sta(struct iwl_mvm *mvm,
340 struct ieee80211_vif *vif,
341 struct ieee80211_sta *sta)
342{
343 return iwl_mvm_sta_send_to_fw(mvm, sta, true);
344}
345
Johannes Berg8ca151b2013-01-24 14:25:36 +0100346int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
347 bool drain)
348{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300349 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100350 int ret;
351 u32 status;
352
353 lockdep_assert_held(&mvm->mutex);
354
355 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
356 cmd.sta_id = mvmsta->sta_id;
357 cmd.add_modify = STA_MODE_MODIFY;
358 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
359 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
360
361 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300362 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
363 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100364 if (ret)
365 return ret;
366
367 switch (status) {
368 case ADD_STA_SUCCESS:
369 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
370 mvmsta->sta_id);
371 break;
372 default:
373 ret = -EIO;
374 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
375 mvmsta->sta_id);
376 break;
377 }
378
379 return ret;
380}
381
382/*
383 * Remove a station from the FW table. Before sending the command to remove
384 * the station validate that the station is indeed known to the driver (sanity
385 * only).
386 */
387static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
388{
389 struct ieee80211_sta *sta;
390 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
391 .sta_id = sta_id,
392 };
393 int ret;
394
395 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
396 lockdep_is_held(&mvm->mutex));
397
398 /* Note: internal stations are marked as error values */
399 if (!sta) {
400 IWL_ERR(mvm, "Invalid station id\n");
401 return -EINVAL;
402 }
403
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300404 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100405 sizeof(rm_sta_cmd), &rm_sta_cmd);
406 if (ret) {
407 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
408 return ret;
409 }
410
411 return 0;
412}
413
414void iwl_mvm_sta_drained_wk(struct work_struct *wk)
415{
416 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
417 u8 sta_id;
418
419 /*
420 * The mutex is needed because of the SYNC cmd, but not only: if the
421 * work would run concurrently with iwl_mvm_rm_sta, it would run before
422 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
423 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
424 * that later.
425 */
426 mutex_lock(&mvm->mutex);
427
428 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
429 int ret;
430 struct ieee80211_sta *sta =
431 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
432 lockdep_is_held(&mvm->mutex));
433
Johannes Berg1ddbbb02013-12-04 22:39:17 +0100434 /*
435 * This station is in use or RCU-removed; the latter happens in
436 * managed mode, where mac80211 removes the station before we
437 * can remove it from firmware (we can only do that after the
438 * MAC is marked unassociated), and possibly while the deauth
439 * frame to disconnect from the AP is still queued. Then, the
440 * station pointer is -ENOENT when the last skb is reclaimed.
441 */
442 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100443 continue;
444
445 if (PTR_ERR(sta) == -EINVAL) {
446 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
447 sta_id);
448 continue;
449 }
450
451 if (!sta) {
452 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
453 sta_id);
454 continue;
455 }
456
457 WARN_ON(PTR_ERR(sta) != -EBUSY);
458 /* This station was removed and we waited until it got drained,
459 * we can now proceed and remove it.
460 */
461 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
462 if (ret) {
463 IWL_ERR(mvm,
464 "Couldn't remove sta %d after it was drained\n",
465 sta_id);
466 continue;
467 }
Monam Agarwalc531c772014-03-24 00:05:56 +0530468 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100469 clear_bit(sta_id, mvm->sta_drained);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300470
471 if (mvm->tfd_drained[sta_id]) {
472 unsigned long i, msk = mvm->tfd_drained[sta_id];
473
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +0200474 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
Emmanuel Grumbach0294d9e2015-01-05 16:52:55 +0200475 iwl_mvm_disable_txq(mvm, i, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300476
477 mvm->tfd_drained[sta_id] = 0;
478 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
479 sta_id, msk);
480 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100481 }
482
483 mutex_unlock(&mvm->mutex);
484}
485
486int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
487 struct ieee80211_vif *vif,
488 struct ieee80211_sta *sta)
489{
490 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100491 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100492 int ret;
493
494 lockdep_assert_held(&mvm->mutex);
495
496 if (vif->type == NL80211_IFTYPE_STATION &&
497 mvmvif->ap_sta_id == mvm_sta->sta_id) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +0200498 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
499 if (ret)
500 return ret;
Emmanuel Grumbach80d85652013-02-19 15:32:42 +0200501 /* flush its queues here since we are freeing mvm_sta */
502 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true);
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +0200503 if (ret)
504 return ret;
505 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
506 mvm_sta->tfd_queue_msk);
507 if (ret)
508 return ret;
509 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
Emmanuel Grumbach80d85652013-02-19 15:32:42 +0200510
Johannes Berg8ca151b2013-01-24 14:25:36 +0100511 /* if we are associated - we can't remove the AP STA now */
512 if (vif->bss_conf.assoc)
513 return ret;
514
515 /* unassoc - go ahead - remove the AP STA now */
516 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
Eliad Peller37577fe2013-12-05 17:19:39 +0200517
518 /* clear d0i3_ap_sta_id if no longer relevant */
519 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
520 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100521 }
522
523 /*
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300524 * This shouldn't happen - the TDLS channel switch should be canceled
525 * before the STA is removed.
526 */
527 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
528 mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
529 cancel_delayed_work(&mvm->tdls_cs.dwork);
530 }
531
532 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300533 * Make sure that the tx response code sees the station as -EBUSY and
534 * calls the drain worker.
535 */
536 spin_lock_bh(&mvm_sta->lock);
537 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +0100538 * There are frames pending on the AC queues for this station.
539 * We need to wait until all the frames are drained...
540 */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300541 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100542 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
543 ERR_PTR(-EBUSY));
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300544 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300545
546 /* disable TDLS sta queues on drain complete */
547 if (sta->tdls) {
548 mvm->tfd_drained[mvm_sta->sta_id] =
549 mvm_sta->tfd_queue_msk;
550 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
551 mvm_sta->sta_id);
552 }
553
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300554 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100555 } else {
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300556 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300557
558 if (sta->tdls)
559 iwl_mvm_tdls_sta_deinit(mvm, sta);
560
Johannes Berg8ca151b2013-01-24 14:25:36 +0100561 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
Monam Agarwalc531c772014-03-24 00:05:56 +0530562 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100563 }
564
565 return ret;
566}
567
568int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
569 struct ieee80211_vif *vif,
570 u8 sta_id)
571{
572 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
573
574 lockdep_assert_held(&mvm->mutex);
575
Monam Agarwalc531c772014-03-24 00:05:56 +0530576 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100577 return ret;
578}
579
Johannes Berg712b24a2014-08-04 14:14:14 +0200580static int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
581 struct iwl_mvm_int_sta *sta,
582 u32 qmask, enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100583{
584 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +0200585 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100586 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
587 return -ENOSPC;
588 }
589
590 sta->tfd_queue_msk = qmask;
591
592 /* put a non-NULL value so iterating over the stations won't stop */
593 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
594 return 0;
595}
596
Johannes Berg712b24a2014-08-04 14:14:14 +0200597static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
598 struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100599{
Monam Agarwalc531c772014-03-24 00:05:56 +0530600 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100601 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
602 sta->sta_id = IWL_MVM_STATION_COUNT;
603}
604
605static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
606 struct iwl_mvm_int_sta *sta,
607 const u8 *addr,
608 u16 mac_id, u16 color)
609{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300610 struct iwl_mvm_add_sta_cmd cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100611 int ret;
612 u32 status;
613
614 lockdep_assert_held(&mvm->mutex);
615
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300616 memset(&cmd, 0, sizeof(cmd));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100617 cmd.sta_id = sta->sta_id;
618 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
619 color));
620
621 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
622
623 if (addr)
624 memcpy(cmd.addr, addr, ETH_ALEN);
625
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300626 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
627 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100628 if (ret)
629 return ret;
630
631 switch (status) {
632 case ADD_STA_SUCCESS:
633 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
634 return 0;
635 default:
636 ret = -EIO;
637 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
638 status);
639 break;
640 }
641 return ret;
642}
643
644int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
645{
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200646 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
647 mvm->cfg->base_params->wd_timeout :
648 IWL_WATCHDOG_DISABLED;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100649 int ret;
650
651 lockdep_assert_held(&mvm->mutex);
652
Ariej Marjieh7da91b02014-07-07 12:09:40 +0300653 /* Map Aux queue to fifo - needs to happen before adding Aux station */
Avri Altman3edf8ff2014-07-30 11:41:01 +0300654 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue,
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200655 IWL_MVM_TX_FIFO_MCAST, wdg_timeout);
Ariej Marjieh7da91b02014-07-07 12:09:40 +0300656
657 /* Allocate aux station and assign to it the aux queue */
658 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
Eliad Pellerb92e6612014-01-23 17:58:23 +0200659 NL80211_IFTYPE_UNSPECIFIED);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100660 if (ret)
661 return ret;
662
663 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
664 MAC_INDEX_AUX, 0);
665
666 if (ret)
667 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
668 return ret;
669}
670
Johannes Berg712b24a2014-08-04 14:14:14 +0200671void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
672{
673 lockdep_assert_held(&mvm->mutex);
674
675 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
676}
677
Johannes Berg8ca151b2013-01-24 14:25:36 +0100678/*
679 * Send the add station command for the vif's broadcast station.
680 * Assumes that the station was already allocated.
681 *
682 * @mvm: the mvm component
683 * @vif: the interface to which the broadcast station is added
684 * @bsta: the broadcast station to add.
685 */
Johannes Berg013290a2014-08-04 13:38:48 +0200686int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100687{
688 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +0200689 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +0200690 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +0100691 const u8 *baddr = _baddr;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100692
693 lockdep_assert_held(&mvm->mutex);
694
Johannes Berg5023d962013-07-31 14:07:43 +0200695 if (vif->type == NL80211_IFTYPE_ADHOC)
696 baddr = vif->bss_conf.bssid;
697
Johannes Berg8ca151b2013-01-24 14:25:36 +0100698 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
699 return -ENOSPC;
700
701 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
702 mvmvif->id, mvmvif->color);
703}
704
705/* Send the FW a request to remove the station from it's internal data
706 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +0200707int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100708{
Johannes Berg013290a2014-08-04 13:38:48 +0200709 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100710 int ret;
711
712 lockdep_assert_held(&mvm->mutex);
713
Johannes Berg013290a2014-08-04 13:38:48 +0200714 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100715 if (ret)
716 IWL_WARN(mvm, "Failed sending remove station\n");
717 return ret;
718}
719
Johannes Berg013290a2014-08-04 13:38:48 +0200720int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
721{
722 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
723 u32 qmask;
724
725 lockdep_assert_held(&mvm->mutex);
726
Arik Nemtsovd92b732e2014-09-21 19:00:42 +0300727 qmask = iwl_mvm_mac_get_queues_mask(vif);
Johannes Berg013290a2014-08-04 13:38:48 +0200728
729 /*
730 * The firmware defines the TFD queue mask to only be relevant
731 * for *unicast* queues, so the multicast (CAB) queue shouldn't
732 * be included.
733 */
734 if (vif->type == NL80211_IFTYPE_AP)
735 qmask &= ~BIT(vif->cab_queue);
736
737 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
738 ieee80211_vif_type_p2p(vif));
739}
740
Johannes Berg8ca151b2013-01-24 14:25:36 +0100741/* Allocate a new station entry for the broadcast station to the given vif,
742 * and send it to the FW.
743 * Note that each P2P mac should have its own broadcast station.
744 *
745 * @mvm: the mvm component
746 * @vif: the interface to which the broadcast station is added
747 * @bsta: the broadcast station to add. */
Johannes Berg013290a2014-08-04 13:38:48 +0200748int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100749{
750 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +0200751 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100752 int ret;
753
754 lockdep_assert_held(&mvm->mutex);
755
Johannes Berg013290a2014-08-04 13:38:48 +0200756 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100757 if (ret)
758 return ret;
759
Johannes Berg013290a2014-08-04 13:38:48 +0200760 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100761
762 if (ret)
763 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +0200764
Johannes Berg8ca151b2013-01-24 14:25:36 +0100765 return ret;
766}
767
Johannes Berg013290a2014-08-04 13:38:48 +0200768void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
769{
770 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
771
772 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
773}
774
Johannes Berg8ca151b2013-01-24 14:25:36 +0100775/*
776 * Send the FW a request to remove the station from it's internal data
777 * structures, and in addition remove it from the local data structure.
778 */
Johannes Berg013290a2014-08-04 13:38:48 +0200779int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100780{
781 int ret;
782
783 lockdep_assert_held(&mvm->mutex);
784
Johannes Berg013290a2014-08-04 13:38:48 +0200785 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100786
Johannes Berg013290a2014-08-04 13:38:48 +0200787 iwl_mvm_dealloc_bcast_sta(mvm, vif);
788
Johannes Berg8ca151b2013-01-24 14:25:36 +0100789 return ret;
790}
791
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300792#define IWL_MAX_RX_BA_SESSIONS 16
793
Johannes Berg8ca151b2013-01-24 14:25:36 +0100794int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
795 int tid, u16 ssn, bool start)
796{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100797 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300798 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100799 int ret;
800 u32 status;
801
802 lockdep_assert_held(&mvm->mutex);
803
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300804 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
805 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
806 return -ENOSPC;
807 }
808
Johannes Berg8ca151b2013-01-24 14:25:36 +0100809 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
810 cmd.sta_id = mvm_sta->sta_id;
811 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +0300812 if (start) {
813 cmd.add_immediate_ba_tid = (u8) tid;
814 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
815 } else {
816 cmd.remove_immediate_ba_tid = (u8) tid;
817 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100818 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
819 STA_MODIFY_REMOVE_BA_TID;
820
821 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300822 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
823 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100824 if (ret)
825 return ret;
826
827 switch (status) {
828 case ADD_STA_SUCCESS:
829 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
830 start ? "start" : "stopp");
831 break;
832 case ADD_STA_IMMEDIATE_BA_FAILURE:
833 IWL_WARN(mvm, "RX BA Session refused by fw\n");
834 ret = -ENOSPC;
835 break;
836 default:
837 ret = -EIO;
838 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
839 start ? "start" : "stopp", status);
840 break;
841 }
842
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300843 if (!ret) {
844 if (start)
845 mvm->rx_ba_sessions++;
846 else if (mvm->rx_ba_sessions > 0)
847 /* check that restart flow didn't zero the counter */
848 mvm->rx_ba_sessions--;
849 }
850
Johannes Berg8ca151b2013-01-24 14:25:36 +0100851 return ret;
852}
853
854static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
855 int tid, u8 queue, bool start)
856{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100857 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300858 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100859 int ret;
860 u32 status;
861
862 lockdep_assert_held(&mvm->mutex);
863
864 if (start) {
865 mvm_sta->tfd_queue_msk |= BIT(queue);
866 mvm_sta->tid_disable_agg &= ~BIT(tid);
867 } else {
868 mvm_sta->tfd_queue_msk &= ~BIT(queue);
869 mvm_sta->tid_disable_agg |= BIT(tid);
870 }
871
872 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
873 cmd.sta_id = mvm_sta->sta_id;
874 cmd.add_modify = STA_MODE_MODIFY;
875 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
876 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
877 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
878
879 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300880 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
881 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100882 if (ret)
883 return ret;
884
885 switch (status) {
886 case ADD_STA_SUCCESS:
887 break;
888 default:
889 ret = -EIO;
890 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
891 start ? "start" : "stopp", status);
892 break;
893 }
894
895 return ret;
896}
897
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +0200898const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100899 IEEE80211_AC_BE,
900 IEEE80211_AC_BK,
901 IEEE80211_AC_BK,
902 IEEE80211_AC_BE,
903 IEEE80211_AC_VI,
904 IEEE80211_AC_VI,
905 IEEE80211_AC_VO,
906 IEEE80211_AC_VO,
907};
908
Johannes Berg3e56ead2013-02-15 22:23:18 +0100909static const u8 tid_to_ucode_ac[] = {
910 AC_BE,
911 AC_BK,
912 AC_BK,
913 AC_BE,
914 AC_VI,
915 AC_VI,
916 AC_VO,
917 AC_VO,
918};
919
Johannes Berg8ca151b2013-01-24 14:25:36 +0100920int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
921 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
922{
Johannes Berg5b577a92013-11-14 18:20:04 +0100923 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100924 struct iwl_mvm_tid_data *tid_data;
925 int txq_id;
926
927 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
928 return -EINVAL;
929
930 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
931 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
932 mvmsta->tid_data[tid].state);
933 return -ENXIO;
934 }
935
936 lockdep_assert_held(&mvm->mutex);
937
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200938 for (txq_id = mvm->first_agg_queue;
939 txq_id <= mvm->last_agg_queue; txq_id++)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100940 if (mvm->queue_to_mac80211[txq_id] ==
941 IWL_INVALID_MAC80211_QUEUE)
942 break;
943
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200944 if (txq_id > mvm->last_agg_queue) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100945 IWL_ERR(mvm, "Failed to allocate agg queue\n");
946 return -EIO;
947 }
948
Arik Nemtsovb2492502014-03-13 12:21:50 +0200949 spin_lock_bh(&mvmsta->lock);
950
951 /* possible race condition - we entered D0i3 while starting agg */
952 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
953 spin_unlock_bh(&mvmsta->lock);
954 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
955 return -EIO;
956 }
957
Johannes Berg8ca151b2013-01-24 14:25:36 +0100958 /* the new tx queue is still connected to the same mac80211 queue */
Johannes Berg3e56ead2013-02-15 22:23:18 +0100959 mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +0100960
Johannes Berg8ca151b2013-01-24 14:25:36 +0100961 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +0100962 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100963 tid_data->txq_id = txq_id;
964 *ssn = tid_data->ssn;
965
966 IWL_DEBUG_TX_QUEUES(mvm,
967 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
968 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
969 tid_data->next_reclaimed);
970
971 if (tid_data->ssn == tid_data->next_reclaimed) {
972 tid_data->state = IWL_AGG_STARTING;
973 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
974 } else {
975 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
976 }
977
978 spin_unlock_bh(&mvmsta->lock);
979
980 return 0;
981}
982
983int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
984 struct ieee80211_sta *sta, u16 tid, u8 buf_size)
985{
Johannes Berg5b577a92013-11-14 18:20:04 +0100986 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100987 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +0200988 unsigned int wdg_timeout =
989 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100990 int queue, fifo, ret;
991 u16 ssn;
992
Eyal Shapiraefed6642014-09-14 15:58:53 +0300993 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
994 != IWL_MAX_TID_COUNT);
995
Johannes Berg8ca151b2013-01-24 14:25:36 +0100996 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
997
998 spin_lock_bh(&mvmsta->lock);
999 ssn = tid_data->ssn;
1000 queue = tid_data->txq_id;
1001 tid_data->state = IWL_AGG_ON;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001002 mvmsta->agg_tids |= BIT(tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001003 tid_data->ssn = 0xffff;
1004 spin_unlock_bh(&mvmsta->lock);
1005
Johannes Berg3e56ead2013-02-15 22:23:18 +01001006 fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001007
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03001008 iwl_mvm_enable_agg_txq(mvm, queue, fifo, mvmsta->sta_id, tid,
1009 buf_size, ssn, wdg_timeout);
1010
Johannes Berg8ca151b2013-01-24 14:25:36 +01001011 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1012 if (ret)
1013 return -EIO;
1014
Johannes Berg8ca151b2013-01-24 14:25:36 +01001015 /*
1016 * Even though in theory the peer could have different
1017 * aggregation reorder buffer sizes for different sessions,
1018 * our ucode doesn't allow for that and has a global limit
1019 * for each station. Therefore, use the minimum of all the
1020 * aggregation sessions and our default value.
1021 */
1022 mvmsta->max_agg_bufsize =
1023 min(mvmsta->max_agg_bufsize, buf_size);
1024 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
1025
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03001026 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
1027 sta->addr, tid);
1028
Eyal Shapira9e680942013-11-09 00:16:16 +02001029 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001030}
1031
1032int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1033 struct ieee80211_sta *sta, u16 tid)
1034{
Johannes Berg5b577a92013-11-14 18:20:04 +01001035 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001036 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1037 u16 txq_id;
1038 int err;
1039
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +02001040
1041 /*
1042 * If mac80211 is cleaning its state, then say that we finished since
1043 * our state has been cleared anyway.
1044 */
1045 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1046 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1047 return 0;
1048 }
1049
Johannes Berg8ca151b2013-01-24 14:25:36 +01001050 spin_lock_bh(&mvmsta->lock);
1051
1052 txq_id = tid_data->txq_id;
1053
1054 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
1055 mvmsta->sta_id, tid, txq_id, tid_data->state);
1056
Eyal Shapiraefed6642014-09-14 15:58:53 +03001057 mvmsta->agg_tids &= ~BIT(tid);
1058
Johannes Berg8ca151b2013-01-24 14:25:36 +01001059 switch (tid_data->state) {
1060 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01001061 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001062
1063 IWL_DEBUG_TX_QUEUES(mvm,
1064 "ssn = %d, next_recl = %d\n",
1065 tid_data->ssn, tid_data->next_reclaimed);
1066
1067 /* There are still packets for this RA / TID in the HW */
1068 if (tid_data->ssn != tid_data->next_reclaimed) {
1069 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
1070 err = 0;
1071 break;
1072 }
1073
1074 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +02001075 tid_data->state = IWL_AGG_OFF;
1076 mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
1077 spin_unlock_bh(&mvmsta->lock);
1078
1079 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1080
1081 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1082
Emmanuel Grumbach0294d9e2015-01-05 16:52:55 +02001083 iwl_mvm_disable_txq(mvm, txq_id, 0);
Johannes Bergf7f89e72014-08-05 15:24:44 +02001084 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001085 case IWL_AGG_STARTING:
1086 case IWL_EMPTYING_HW_QUEUE_ADDBA:
1087 /*
1088 * The agg session has been stopped before it was set up. This
1089 * can happen when the AddBA timer times out for example.
1090 */
1091
1092 /* No barriers since we are under mutex */
1093 lockdep_assert_held(&mvm->mutex);
1094 mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
1095
1096 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1097 tid_data->state = IWL_AGG_OFF;
1098 err = 0;
1099 break;
1100 default:
1101 IWL_ERR(mvm,
1102 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
1103 mvmsta->sta_id, tid, tid_data->state);
1104 IWL_ERR(mvm,
1105 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
1106 err = -EINVAL;
1107 }
1108
1109 spin_unlock_bh(&mvmsta->lock);
1110
1111 return err;
1112}
1113
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001114int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1115 struct ieee80211_sta *sta, u16 tid)
1116{
Johannes Berg5b577a92013-11-14 18:20:04 +01001117 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001118 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1119 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02001120 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001121
1122 /*
1123 * First set the agg state to OFF to avoid calling
1124 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
1125 */
1126 spin_lock_bh(&mvmsta->lock);
1127 txq_id = tid_data->txq_id;
1128 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
1129 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001130 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001131 tid_data->state = IWL_AGG_OFF;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001132 mvmsta->agg_tids &= ~BIT(tid);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001133 spin_unlock_bh(&mvmsta->lock);
1134
Johannes Bergb6658ff2013-07-24 13:55:51 +02001135 if (old_state >= IWL_AGG_ON) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001136 iwl_mvm_drain_sta(mvm, mvmsta, true);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001137 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true))
1138 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001139 iwl_trans_wait_tx_queue_empty(mvm->trans,
1140 mvmsta->tfd_queue_msk);
1141 iwl_mvm_drain_sta(mvm, mvmsta, false);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001142
Johannes Bergf7f89e72014-08-05 15:24:44 +02001143 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1144
Emmanuel Grumbach0294d9e2015-01-05 16:52:55 +02001145 iwl_mvm_disable_txq(mvm, tid_data->txq_id, 0);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001146 }
1147
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001148 mvm->queue_to_mac80211[tid_data->txq_id] =
1149 IWL_INVALID_MAC80211_QUEUE;
1150
1151 return 0;
1152}
1153
Johannes Berg8ca151b2013-01-24 14:25:36 +01001154static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
1155{
Johannes Berg2dc2a152015-06-16 17:09:18 +02001156 int i, max = -1, max_offs = -1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001157
1158 lockdep_assert_held(&mvm->mutex);
1159
Johannes Berg2dc2a152015-06-16 17:09:18 +02001160 /* Pick the unused key offset with the highest 'deleted'
1161 * counter. Every time a key is deleted, all the counters
1162 * are incremented and the one that was just deleted is
1163 * reset to zero. Thus, the highest counter is the one
1164 * that was deleted longest ago. Pick that one.
1165 */
1166 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
1167 if (test_bit(i, mvm->fw_key_table))
1168 continue;
1169 if (mvm->fw_key_deleted[i] > max) {
1170 max = mvm->fw_key_deleted[i];
1171 max_offs = i;
1172 }
1173 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001174
Johannes Berg2dc2a152015-06-16 17:09:18 +02001175 if (max_offs < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001176 return STA_KEY_IDX_INVALID;
1177
Johannes Berg2dc2a152015-06-16 17:09:18 +02001178 __set_bit(max_offs, mvm->fw_key_table);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001179
Johannes Berg2dc2a152015-06-16 17:09:18 +02001180 return max_offs;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001181}
1182
1183static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
1184 struct ieee80211_sta *sta)
1185{
Johannes Berg5b530e92014-12-23 16:00:17 +01001186 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001187
1188 if (sta) {
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001189 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001190
1191 return mvm_sta->sta_id;
1192 }
1193
1194 /*
1195 * The device expects GTKs for station interfaces to be
1196 * installed as GTKs for the AP station. If we have no
1197 * station ID, then use AP's station ID.
1198 */
1199 if (vif->type == NL80211_IFTYPE_STATION &&
1200 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
1201 return mvmvif->ap_sta_id;
1202
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001203 return IWL_MVM_STATION_COUNT;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001204}
1205
1206static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
1207 struct iwl_mvm_sta *mvm_sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01001208 struct ieee80211_key_conf *keyconf, bool mcast,
Johannes Berg2f6319d2014-11-12 23:39:56 +01001209 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001210{
Max Stepanov5a258aa2013-04-07 09:11:21 +03001211 struct iwl_mvm_add_sta_key_cmd cmd = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001212 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01001213 int ret;
1214 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001215 u16 keyidx;
1216 int i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001217 u8 sta_id = mvm_sta->sta_id;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001218
1219 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1220 STA_KEY_FLG_KEYID_MSK;
1221 key_flags = cpu_to_le16(keyidx);
1222 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
1223
1224 switch (keyconf->cipher) {
1225 case WLAN_CIPHER_SUITE_TKIP:
1226 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001227 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001228 for (i = 0; i < 5; i++)
Max Stepanov5a258aa2013-04-07 09:11:21 +03001229 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1230 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001231 break;
1232 case WLAN_CIPHER_SUITE_CCMP:
1233 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001234 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001235 break;
Johannes Bergba3943b2014-11-12 23:54:48 +01001236 case WLAN_CIPHER_SUITE_WEP104:
1237 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
John W. Linvilleaa0cb082015-01-12 16:18:11 -05001238 /* fall through */
Johannes Bergba3943b2014-11-12 23:54:48 +01001239 case WLAN_CIPHER_SUITE_WEP40:
1240 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
1241 memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
1242 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001243 default:
Max Stepanove36e5432013-08-27 19:56:13 +03001244 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
1245 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001246 }
1247
Johannes Bergba3943b2014-11-12 23:54:48 +01001248 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001249 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1250
Max Stepanov5a258aa2013-04-07 09:11:21 +03001251 cmd.key_offset = keyconf->hw_key_idx;
1252 cmd.key_flags = key_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001253 cmd.sta_id = sta_id;
1254
1255 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001256 if (cmd_flags & CMD_ASYNC)
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001257 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
1258 sizeof(cmd), &cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001259 else
1260 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1261 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001262
1263 switch (status) {
1264 case ADD_STA_SUCCESS:
1265 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1266 break;
1267 default:
1268 ret = -EIO;
1269 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1270 break;
1271 }
1272
1273 return ret;
1274}
1275
1276static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1277 struct ieee80211_key_conf *keyconf,
1278 u8 sta_id, bool remove_key)
1279{
1280 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1281
1282 /* verify the key details match the required command's expectations */
1283 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1284 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1285 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1286 return -EINVAL;
1287
1288 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1289 igtk_cmd.sta_id = cpu_to_le32(sta_id);
1290
1291 if (remove_key) {
1292 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1293 } else {
1294 struct ieee80211_key_seq seq;
1295 const u8 *pn;
1296
1297 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001298 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1299 pn = seq.aes_cmac.pn;
1300 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1301 ((u64) pn[4] << 8) |
1302 ((u64) pn[3] << 16) |
1303 ((u64) pn[2] << 24) |
1304 ((u64) pn[1] << 32) |
1305 ((u64) pn[0] << 40));
1306 }
1307
1308 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1309 remove_key ? "removing" : "installing",
1310 igtk_cmd.sta_id);
1311
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001312 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001313 sizeof(igtk_cmd), &igtk_cmd);
1314}
1315
1316
1317static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1318 struct ieee80211_vif *vif,
1319 struct ieee80211_sta *sta)
1320{
Johannes Berg5b530e92014-12-23 16:00:17 +01001321 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001322
1323 if (sta)
1324 return sta->addr;
1325
1326 if (vif->type == NL80211_IFTYPE_STATION &&
1327 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1328 u8 sta_id = mvmvif->ap_sta_id;
1329 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1330 lockdep_is_held(&mvm->mutex));
1331 return sta->addr;
1332 }
1333
1334
1335 return NULL;
1336}
1337
Johannes Berg2f6319d2014-11-12 23:39:56 +01001338static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1339 struct ieee80211_vif *vif,
1340 struct ieee80211_sta *sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01001341 struct ieee80211_key_conf *keyconf,
1342 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001343{
Johannes Berg2f6319d2014-11-12 23:39:56 +01001344 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001345 int ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001346 const u8 *addr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001347 struct ieee80211_key_seq seq;
1348 u16 p1k[5];
1349
Johannes Berg8ca151b2013-01-24 14:25:36 +01001350 switch (keyconf->cipher) {
1351 case WLAN_CIPHER_SUITE_TKIP:
1352 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1353 /* get phase 1 key from mac80211 */
1354 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1355 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Bergba3943b2014-11-12 23:54:48 +01001356 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001357 seq.tkip.iv32, p1k, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001358 break;
1359 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergba3943b2014-11-12 23:54:48 +01001360 case WLAN_CIPHER_SUITE_WEP40:
1361 case WLAN_CIPHER_SUITE_WEP104:
1362 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001363 0, NULL, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001364 break;
1365 default:
Johannes Bergba3943b2014-11-12 23:54:48 +01001366 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Johannes Berg2f6319d2014-11-12 23:39:56 +01001367 0, NULL, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001368 }
1369
Johannes Berg8ca151b2013-01-24 14:25:36 +01001370 return ret;
1371}
1372
Johannes Berg2f6319d2014-11-12 23:39:56 +01001373static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
Johannes Bergba3943b2014-11-12 23:54:48 +01001374 struct ieee80211_key_conf *keyconf,
1375 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001376{
Max Stepanov5a258aa2013-04-07 09:11:21 +03001377 struct iwl_mvm_add_sta_key_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001378 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01001379 int ret;
1380 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001381
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02001382 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1383 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001384 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1385 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1386
Johannes Bergba3943b2014-11-12 23:54:48 +01001387 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001388 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1389
Max Stepanov5a258aa2013-04-07 09:11:21 +03001390 cmd.key_flags = key_flags;
1391 cmd.key_offset = keyconf->hw_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001392 cmd.sta_id = sta_id;
1393
Johannes Berg8ca151b2013-01-24 14:25:36 +01001394 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001395 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1396 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001397
1398 switch (status) {
1399 case ADD_STA_SUCCESS:
1400 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1401 break;
1402 default:
1403 ret = -EIO;
1404 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1405 break;
1406 }
1407
1408 return ret;
1409}
1410
Johannes Berg2f6319d2014-11-12 23:39:56 +01001411int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1412 struct ieee80211_vif *vif,
1413 struct ieee80211_sta *sta,
1414 struct ieee80211_key_conf *keyconf,
1415 bool have_key_offset)
1416{
Johannes Bergba3943b2014-11-12 23:54:48 +01001417 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg2f6319d2014-11-12 23:39:56 +01001418 u8 sta_id;
1419 int ret;
Matti Gottlieb11828db2015-06-01 15:15:11 +03001420 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
Johannes Berg2f6319d2014-11-12 23:39:56 +01001421
1422 lockdep_assert_held(&mvm->mutex);
1423
1424 /* Get the station id from the mvm local station table */
1425 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1426 if (sta_id == IWL_MVM_STATION_COUNT) {
1427 IWL_ERR(mvm, "Failed to find station id\n");
1428 return -EINVAL;
1429 }
1430
1431 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1432 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1433 goto end;
1434 }
1435
1436 /*
1437 * It is possible that the 'sta' parameter is NULL, and thus
1438 * there is a need to retrieve the sta from the local station table.
1439 */
1440 if (!sta) {
1441 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1442 lockdep_is_held(&mvm->mutex));
1443 if (IS_ERR_OR_NULL(sta)) {
1444 IWL_ERR(mvm, "Invalid station id\n");
1445 return -EINVAL;
1446 }
1447 }
1448
1449 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
1450 return -EINVAL;
1451
1452 if (!have_key_offset) {
1453 /*
1454 * The D3 firmware hardcodes the PTK offset to 0, so we have to
1455 * configure it there. As a result, this workaround exists to
1456 * let the caller set the key offset (hw_key_idx), see d3.c.
1457 */
1458 keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
1459 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1460 return -ENOSPC;
1461 }
1462
Johannes Bergba3943b2014-11-12 23:54:48 +01001463 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, mcast);
1464 if (ret) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01001465 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
Johannes Bergba3943b2014-11-12 23:54:48 +01001466 goto end;
1467 }
1468
1469 /*
1470 * For WEP, the same key is used for multicast and unicast. Upload it
1471 * again, using the same key offset, and now pointing the other one
1472 * to the same key slot (offset).
1473 * If this fails, remove the original as well.
1474 */
1475 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
1476 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
1477 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, !mcast);
1478 if (ret) {
1479 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1480 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
1481 }
1482 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01001483
1484end:
1485 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1486 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Matti Gottlieb11828db2015-06-01 15:15:11 +03001487 sta ? sta->addr : zero_addr, ret);
Johannes Berg2f6319d2014-11-12 23:39:56 +01001488 return ret;
1489}
1490
1491int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1492 struct ieee80211_vif *vif,
1493 struct ieee80211_sta *sta,
1494 struct ieee80211_key_conf *keyconf)
1495{
Johannes Bergba3943b2014-11-12 23:54:48 +01001496 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg2f6319d2014-11-12 23:39:56 +01001497 u8 sta_id;
Johannes Berg2dc2a152015-06-16 17:09:18 +02001498 int ret, i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001499
1500 lockdep_assert_held(&mvm->mutex);
1501
1502 /* Get the station id from the mvm local station table */
1503 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1504
1505 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1506 keyconf->keyidx, sta_id);
1507
1508 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1509 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1510
1511 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
1512 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1513 keyconf->hw_key_idx);
1514 return -ENOENT;
1515 }
1516
Johannes Berg2dc2a152015-06-16 17:09:18 +02001517 /* track which key was deleted last */
1518 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
1519 if (mvm->fw_key_deleted[i] < U8_MAX)
1520 mvm->fw_key_deleted[i]++;
1521 }
1522 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
1523
Johannes Berg2f6319d2014-11-12 23:39:56 +01001524 if (sta_id == IWL_MVM_STATION_COUNT) {
1525 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1526 return 0;
1527 }
1528
1529 /*
1530 * It is possible that the 'sta' parameter is NULL, and thus
1531 * there is a need to retrieve the sta from the local station table,
1532 * for example when a GTK is removed (where the sta_id will then be
1533 * the AP ID, and no station was passed by mac80211.)
1534 */
1535 if (!sta) {
1536 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1537 lockdep_is_held(&mvm->mutex));
1538 if (!sta) {
1539 IWL_ERR(mvm, "Invalid station id\n");
1540 return -EINVAL;
1541 }
1542 }
1543
1544 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
1545 return -EINVAL;
1546
Johannes Bergba3943b2014-11-12 23:54:48 +01001547 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
1548 if (ret)
1549 return ret;
1550
1551 /* delete WEP key twice to get rid of (now useless) offset */
1552 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
1553 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
1554 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
1555
1556 return ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001557}
1558
Johannes Berg8ca151b2013-01-24 14:25:36 +01001559void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1560 struct ieee80211_vif *vif,
1561 struct ieee80211_key_conf *keyconf,
1562 struct ieee80211_sta *sta, u32 iv32,
1563 u16 *phase1key)
1564{
Beni Levc3eb5362013-02-06 17:22:18 +02001565 struct iwl_mvm_sta *mvm_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001566 u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
Johannes Bergba3943b2014-11-12 23:54:48 +01001567 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001568
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001569 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001570 return;
1571
Beni Levc3eb5362013-02-06 17:22:18 +02001572 rcu_read_lock();
1573
1574 if (!sta) {
1575 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1576 if (WARN_ON(IS_ERR_OR_NULL(sta))) {
1577 rcu_read_unlock();
1578 return;
1579 }
1580 }
1581
Johannes Berg2f6319d2014-11-12 23:39:56 +01001582 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Bergba3943b2014-11-12 23:54:48 +01001583 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001584 iv32, phase1key, CMD_ASYNC);
Beni Levc3eb5362013-02-06 17:22:18 +02001585 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01001586}
1587
Johannes Berg9cc40712013-02-15 22:47:48 +01001588void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
1589 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001590{
Johannes Berg5b577a92013-11-14 18:20:04 +01001591 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001592 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001593 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01001594 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03001595 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01001596 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01001597 };
1598 int ret;
1599
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001600 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001601 if (ret)
1602 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1603}
1604
Johannes Berg9cc40712013-02-15 22:47:48 +01001605void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
1606 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001607 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01001608 u16 cnt, u16 tids, bool more_data,
1609 bool agg)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001610{
Johannes Berg5b577a92013-11-14 18:20:04 +01001611 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001612 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001613 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01001614 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001615 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1616 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01001617 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01001618 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01001619 int tid, ret;
1620 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001621
Johannes Berg3e56ead2013-02-15 22:23:18 +01001622 /* convert TIDs to ACs - we don't support TSPEC so that's OK
1623 * Note that this field is reserved and unused by firmware not
1624 * supporting GO uAPSD, so it's safe to always do this.
1625 */
1626 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
1627 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
1628
1629 /* If we're releasing frames from aggregation queues then check if the
1630 * all queues combined that we're releasing frames from have
1631 * - more frames than the service period, in which case more_data
1632 * needs to be set
1633 * - fewer than 'cnt' frames, in which case we need to adjust the
1634 * firmware command (but do that unconditionally)
1635 */
1636 if (agg) {
1637 int remaining = cnt;
1638
1639 spin_lock_bh(&mvmsta->lock);
1640 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
1641 struct iwl_mvm_tid_data *tid_data;
1642 u16 n_queued;
1643
1644 tid_data = &mvmsta->tid_data[tid];
1645 if (WARN(tid_data->state != IWL_AGG_ON &&
1646 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
1647 "TID %d state is %d\n",
1648 tid, tid_data->state)) {
1649 spin_unlock_bh(&mvmsta->lock);
1650 ieee80211_sta_eosp(sta);
1651 return;
1652 }
1653
1654 n_queued = iwl_mvm_tid_queued(tid_data);
1655 if (n_queued > remaining) {
1656 more_data = true;
1657 remaining = 0;
1658 break;
1659 }
1660 remaining -= n_queued;
1661 }
1662 spin_unlock_bh(&mvmsta->lock);
1663
1664 cmd.sleep_tx_count = cpu_to_le16(cnt - remaining);
1665 if (WARN_ON(cnt - remaining == 0)) {
1666 ieee80211_sta_eosp(sta);
1667 return;
1668 }
1669 }
1670
1671 /* Note: this is ignored by firmware not supporting GO uAPSD */
1672 if (more_data)
1673 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
1674
1675 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
1676 mvmsta->next_status_eosp = true;
1677 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
1678 } else {
1679 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
1680 }
1681
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001682 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001683 if (ret)
1684 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1685}
Johannes Berg3e56ead2013-02-15 22:23:18 +01001686
Johannes Berg04168412015-06-23 21:22:09 +02001687void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
1688 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg3e56ead2013-02-15 22:23:18 +01001689{
1690 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1691 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
1692 struct ieee80211_sta *sta;
1693 u32 sta_id = le32_to_cpu(notif->sta_id);
1694
1695 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +02001696 return;
Johannes Berg3e56ead2013-02-15 22:23:18 +01001697
1698 rcu_read_lock();
1699 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1700 if (!IS_ERR_OR_NULL(sta))
1701 ieee80211_sta_eosp(sta);
1702 rcu_read_unlock();
Johannes Berg3e56ead2013-02-15 22:23:18 +01001703}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03001704
1705void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
1706 struct iwl_mvm_sta *mvmsta, bool disable)
1707{
1708 struct iwl_mvm_add_sta_cmd cmd = {
1709 .add_modify = STA_MODE_MODIFY,
1710 .sta_id = mvmsta->sta_id,
1711 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
1712 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
1713 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1714 };
1715 int ret;
1716
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03001717 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1718 if (ret)
1719 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1720}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001721
1722void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
1723 struct ieee80211_sta *sta,
1724 bool disable)
1725{
1726 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1727
1728 spin_lock_bh(&mvm_sta->lock);
1729
1730 if (mvm_sta->disable_tx == disable) {
1731 spin_unlock_bh(&mvm_sta->lock);
1732 return;
1733 }
1734
1735 mvm_sta->disable_tx = disable;
1736
1737 /*
Sara Sharon0d365ae2015-03-31 12:24:05 +03001738 * Tell mac80211 to start/stop queuing tx for this station,
1739 * but don't stop queuing if there are still pending frames
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001740 * for this station.
1741 */
1742 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
1743 ieee80211_sta_block_awake(mvm->hw, sta, disable);
1744
1745 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
1746
1747 spin_unlock_bh(&mvm_sta->lock);
1748}
1749
1750void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
1751 struct iwl_mvm_vif *mvmvif,
1752 bool disable)
1753{
1754 struct ieee80211_sta *sta;
1755 struct iwl_mvm_sta *mvm_sta;
1756 int i;
1757
1758 lockdep_assert_held(&mvm->mutex);
1759
1760 /* Block/unblock all the stations of the given mvmvif */
1761 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
1762 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
1763 lockdep_is_held(&mvm->mutex));
1764 if (IS_ERR_OR_NULL(sta))
1765 continue;
1766
1767 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1768 if (mvm_sta->mac_id_n_color !=
1769 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
1770 continue;
1771
1772 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
1773 }
1774}
Luciano Coelhodc88b4b2014-11-10 11:10:14 +02001775
1776void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1777{
1778 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1779 struct iwl_mvm_sta *mvmsta;
1780
1781 rcu_read_lock();
1782
1783 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
1784
1785 if (!WARN_ON(!mvmsta))
1786 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
1787
1788 rcu_read_unlock();
1789}