blob: df216cd0c98f4659d1c00d7d07c8adb31c8244c6 [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
278 spin_lock_init(&mvm_sta->lock);
279
280 mvm_sta->sta_id = sta_id;
281 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
282 mvmvif->color);
283 mvm_sta->vif = vif;
284 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300285 mvm_sta->tx_protection = 0;
286 mvm_sta->tt_tx_protection = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100287
288 /* HW restart, don't assume the memory has been zeroed */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300289 atomic_set(&mvm->pending_frames[sta_id], 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100290 mvm_sta->tid_disable_agg = 0;
291 mvm_sta->tfd_queue_msk = 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300292
293 /* allocate new queues for a TDLS station */
294 if (sta->tdls) {
295 ret = iwl_mvm_tdls_sta_init(mvm, sta);
296 if (ret)
297 return ret;
298 } else {
299 for (i = 0; i < IEEE80211_NUM_ACS; i++)
300 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
301 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
302 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100303
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200304 /* for HW restart - reset everything but the sequence number */
305 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
306 u16 seq = mvm_sta->tid_data[i].seq_number;
307 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
308 mvm_sta->tid_data[i].seq_number = seq;
309 }
Eyal Shapiraefed6642014-09-14 15:58:53 +0300310 mvm_sta->agg_tids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100311
Johannes Berg7a453972013-02-12 13:10:44 +0100312 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100313 if (ret)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300314 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100315
Johannes Berg9e848012014-08-04 14:33:42 +0200316 if (vif->type == NL80211_IFTYPE_STATION) {
317 if (!sta->tdls) {
318 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
319 mvmvif->ap_sta_id = sta_id;
320 } else {
321 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
322 }
323 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100324
325 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
326
327 return 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300328
329err:
330 iwl_mvm_tdls_sta_deinit(mvm, sta);
331 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100332}
333
Johannes Berg7a453972013-02-12 13:10:44 +0100334int iwl_mvm_update_sta(struct iwl_mvm *mvm,
335 struct ieee80211_vif *vif,
336 struct ieee80211_sta *sta)
337{
338 return iwl_mvm_sta_send_to_fw(mvm, sta, true);
339}
340
Johannes Berg8ca151b2013-01-24 14:25:36 +0100341int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
342 bool drain)
343{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300344 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100345 int ret;
346 u32 status;
347
348 lockdep_assert_held(&mvm->mutex);
349
350 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
351 cmd.sta_id = mvmsta->sta_id;
352 cmd.add_modify = STA_MODE_MODIFY;
353 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
354 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
355
356 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300357 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
358 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100359 if (ret)
360 return ret;
361
362 switch (status) {
363 case ADD_STA_SUCCESS:
364 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
365 mvmsta->sta_id);
366 break;
367 default:
368 ret = -EIO;
369 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
370 mvmsta->sta_id);
371 break;
372 }
373
374 return ret;
375}
376
377/*
378 * Remove a station from the FW table. Before sending the command to remove
379 * the station validate that the station is indeed known to the driver (sanity
380 * only).
381 */
382static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
383{
384 struct ieee80211_sta *sta;
385 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
386 .sta_id = sta_id,
387 };
388 int ret;
389
390 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
391 lockdep_is_held(&mvm->mutex));
392
393 /* Note: internal stations are marked as error values */
394 if (!sta) {
395 IWL_ERR(mvm, "Invalid station id\n");
396 return -EINVAL;
397 }
398
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300399 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100400 sizeof(rm_sta_cmd), &rm_sta_cmd);
401 if (ret) {
402 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
403 return ret;
404 }
405
406 return 0;
407}
408
409void iwl_mvm_sta_drained_wk(struct work_struct *wk)
410{
411 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
412 u8 sta_id;
413
414 /*
415 * The mutex is needed because of the SYNC cmd, but not only: if the
416 * work would run concurrently with iwl_mvm_rm_sta, it would run before
417 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
418 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
419 * that later.
420 */
421 mutex_lock(&mvm->mutex);
422
423 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
424 int ret;
425 struct ieee80211_sta *sta =
426 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
427 lockdep_is_held(&mvm->mutex));
428
Johannes Berg1ddbbb02013-12-04 22:39:17 +0100429 /*
430 * This station is in use or RCU-removed; the latter happens in
431 * managed mode, where mac80211 removes the station before we
432 * can remove it from firmware (we can only do that after the
433 * MAC is marked unassociated), and possibly while the deauth
434 * frame to disconnect from the AP is still queued. Then, the
435 * station pointer is -ENOENT when the last skb is reclaimed.
436 */
437 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100438 continue;
439
440 if (PTR_ERR(sta) == -EINVAL) {
441 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
442 sta_id);
443 continue;
444 }
445
446 if (!sta) {
447 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
448 sta_id);
449 continue;
450 }
451
452 WARN_ON(PTR_ERR(sta) != -EBUSY);
453 /* This station was removed and we waited until it got drained,
454 * we can now proceed and remove it.
455 */
456 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
457 if (ret) {
458 IWL_ERR(mvm,
459 "Couldn't remove sta %d after it was drained\n",
460 sta_id);
461 continue;
462 }
Monam Agarwalc531c772014-03-24 00:05:56 +0530463 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100464 clear_bit(sta_id, mvm->sta_drained);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300465
466 if (mvm->tfd_drained[sta_id]) {
467 unsigned long i, msk = mvm->tfd_drained[sta_id];
468
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +0200469 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
Emmanuel Grumbach0294d9e2015-01-05 16:52:55 +0200470 iwl_mvm_disable_txq(mvm, i, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300471
472 mvm->tfd_drained[sta_id] = 0;
473 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
474 sta_id, msk);
475 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100476 }
477
478 mutex_unlock(&mvm->mutex);
479}
480
481int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
482 struct ieee80211_vif *vif,
483 struct ieee80211_sta *sta)
484{
485 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100486 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100487 int ret;
488
489 lockdep_assert_held(&mvm->mutex);
490
491 if (vif->type == NL80211_IFTYPE_STATION &&
492 mvmvif->ap_sta_id == mvm_sta->sta_id) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +0200493 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
494 if (ret)
495 return ret;
Emmanuel Grumbach80d85652013-02-19 15:32:42 +0200496 /* flush its queues here since we are freeing mvm_sta */
497 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true);
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +0200498 if (ret)
499 return ret;
500 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
501 mvm_sta->tfd_queue_msk);
502 if (ret)
503 return ret;
504 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
Emmanuel Grumbach80d85652013-02-19 15:32:42 +0200505
Johannes Berg8ca151b2013-01-24 14:25:36 +0100506 /* if we are associated - we can't remove the AP STA now */
507 if (vif->bss_conf.assoc)
508 return ret;
509
510 /* unassoc - go ahead - remove the AP STA now */
511 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
Eliad Peller37577fe2013-12-05 17:19:39 +0200512
513 /* clear d0i3_ap_sta_id if no longer relevant */
514 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
515 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100516 }
517
518 /*
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300519 * This shouldn't happen - the TDLS channel switch should be canceled
520 * before the STA is removed.
521 */
522 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
523 mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
524 cancel_delayed_work(&mvm->tdls_cs.dwork);
525 }
526
527 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300528 * Make sure that the tx response code sees the station as -EBUSY and
529 * calls the drain worker.
530 */
531 spin_lock_bh(&mvm_sta->lock);
532 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +0100533 * There are frames pending on the AC queues for this station.
534 * We need to wait until all the frames are drained...
535 */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300536 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100537 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
538 ERR_PTR(-EBUSY));
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300539 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300540
541 /* disable TDLS sta queues on drain complete */
542 if (sta->tdls) {
543 mvm->tfd_drained[mvm_sta->sta_id] =
544 mvm_sta->tfd_queue_msk;
545 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
546 mvm_sta->sta_id);
547 }
548
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300549 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100550 } else {
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300551 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300552
553 if (sta->tdls)
554 iwl_mvm_tdls_sta_deinit(mvm, sta);
555
Johannes Berg8ca151b2013-01-24 14:25:36 +0100556 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
Monam Agarwalc531c772014-03-24 00:05:56 +0530557 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100558 }
559
560 return ret;
561}
562
563int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
564 struct ieee80211_vif *vif,
565 u8 sta_id)
566{
567 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
568
569 lockdep_assert_held(&mvm->mutex);
570
Monam Agarwalc531c772014-03-24 00:05:56 +0530571 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100572 return ret;
573}
574
Johannes Berg712b24a2014-08-04 14:14:14 +0200575static int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
576 struct iwl_mvm_int_sta *sta,
577 u32 qmask, enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100578{
579 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +0200580 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100581 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
582 return -ENOSPC;
583 }
584
585 sta->tfd_queue_msk = qmask;
586
587 /* put a non-NULL value so iterating over the stations won't stop */
588 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
589 return 0;
590}
591
Johannes Berg712b24a2014-08-04 14:14:14 +0200592static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
593 struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100594{
Monam Agarwalc531c772014-03-24 00:05:56 +0530595 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100596 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
597 sta->sta_id = IWL_MVM_STATION_COUNT;
598}
599
600static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
601 struct iwl_mvm_int_sta *sta,
602 const u8 *addr,
603 u16 mac_id, u16 color)
604{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300605 struct iwl_mvm_add_sta_cmd cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100606 int ret;
607 u32 status;
608
609 lockdep_assert_held(&mvm->mutex);
610
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300611 memset(&cmd, 0, sizeof(cmd));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100612 cmd.sta_id = sta->sta_id;
613 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
614 color));
615
616 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
617
618 if (addr)
619 memcpy(cmd.addr, addr, ETH_ALEN);
620
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300621 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
622 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100623 if (ret)
624 return ret;
625
626 switch (status) {
627 case ADD_STA_SUCCESS:
628 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
629 return 0;
630 default:
631 ret = -EIO;
632 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
633 status);
634 break;
635 }
636 return ret;
637}
638
639int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
640{
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200641 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
642 mvm->cfg->base_params->wd_timeout :
643 IWL_WATCHDOG_DISABLED;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100644 int ret;
645
646 lockdep_assert_held(&mvm->mutex);
647
Ariej Marjieh7da91b02014-07-07 12:09:40 +0300648 /* Map Aux queue to fifo - needs to happen before adding Aux station */
Avri Altman3edf8ff2014-07-30 11:41:01 +0300649 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue,
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200650 IWL_MVM_TX_FIFO_MCAST, wdg_timeout);
Ariej Marjieh7da91b02014-07-07 12:09:40 +0300651
652 /* Allocate aux station and assign to it the aux queue */
653 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
Eliad Pellerb92e6612014-01-23 17:58:23 +0200654 NL80211_IFTYPE_UNSPECIFIED);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100655 if (ret)
656 return ret;
657
658 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
659 MAC_INDEX_AUX, 0);
660
661 if (ret)
662 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
663 return ret;
664}
665
Johannes Berg712b24a2014-08-04 14:14:14 +0200666void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
667{
668 lockdep_assert_held(&mvm->mutex);
669
670 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
671}
672
Johannes Berg8ca151b2013-01-24 14:25:36 +0100673/*
674 * Send the add station command for the vif's broadcast station.
675 * Assumes that the station was already allocated.
676 *
677 * @mvm: the mvm component
678 * @vif: the interface to which the broadcast station is added
679 * @bsta: the broadcast station to add.
680 */
Johannes Berg013290a2014-08-04 13:38:48 +0200681int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100682{
683 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +0200684 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +0200685 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +0100686 const u8 *baddr = _baddr;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100687
688 lockdep_assert_held(&mvm->mutex);
689
Johannes Berg5023d962013-07-31 14:07:43 +0200690 if (vif->type == NL80211_IFTYPE_ADHOC)
691 baddr = vif->bss_conf.bssid;
692
Johannes Berg8ca151b2013-01-24 14:25:36 +0100693 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
694 return -ENOSPC;
695
696 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
697 mvmvif->id, mvmvif->color);
698}
699
700/* Send the FW a request to remove the station from it's internal data
701 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +0200702int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100703{
Johannes Berg013290a2014-08-04 13:38:48 +0200704 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100705 int ret;
706
707 lockdep_assert_held(&mvm->mutex);
708
Johannes Berg013290a2014-08-04 13:38:48 +0200709 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100710 if (ret)
711 IWL_WARN(mvm, "Failed sending remove station\n");
712 return ret;
713}
714
Johannes Berg013290a2014-08-04 13:38:48 +0200715int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
716{
717 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
718 u32 qmask;
719
720 lockdep_assert_held(&mvm->mutex);
721
Arik Nemtsovd92b732e2014-09-21 19:00:42 +0300722 qmask = iwl_mvm_mac_get_queues_mask(vif);
Johannes Berg013290a2014-08-04 13:38:48 +0200723
724 /*
725 * The firmware defines the TFD queue mask to only be relevant
726 * for *unicast* queues, so the multicast (CAB) queue shouldn't
727 * be included.
728 */
729 if (vif->type == NL80211_IFTYPE_AP)
730 qmask &= ~BIT(vif->cab_queue);
731
732 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
733 ieee80211_vif_type_p2p(vif));
734}
735
Johannes Berg8ca151b2013-01-24 14:25:36 +0100736/* Allocate a new station entry for the broadcast station to the given vif,
737 * and send it to the FW.
738 * Note that each P2P mac should have its own broadcast station.
739 *
740 * @mvm: the mvm component
741 * @vif: the interface to which the broadcast station is added
742 * @bsta: the broadcast station to add. */
Johannes Berg013290a2014-08-04 13:38:48 +0200743int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100744{
745 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +0200746 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100747 int ret;
748
749 lockdep_assert_held(&mvm->mutex);
750
Johannes Berg013290a2014-08-04 13:38:48 +0200751 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100752 if (ret)
753 return ret;
754
Johannes Berg013290a2014-08-04 13:38:48 +0200755 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100756
757 if (ret)
758 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +0200759
Johannes Berg8ca151b2013-01-24 14:25:36 +0100760 return ret;
761}
762
Johannes Berg013290a2014-08-04 13:38:48 +0200763void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
764{
765 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
766
767 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
768}
769
Johannes Berg8ca151b2013-01-24 14:25:36 +0100770/*
771 * Send the FW a request to remove the station from it's internal data
772 * structures, and in addition remove it from the local data structure.
773 */
Johannes Berg013290a2014-08-04 13:38:48 +0200774int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100775{
776 int ret;
777
778 lockdep_assert_held(&mvm->mutex);
779
Johannes Berg013290a2014-08-04 13:38:48 +0200780 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100781
Johannes Berg013290a2014-08-04 13:38:48 +0200782 iwl_mvm_dealloc_bcast_sta(mvm, vif);
783
Johannes Berg8ca151b2013-01-24 14:25:36 +0100784 return ret;
785}
786
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300787#define IWL_MAX_RX_BA_SESSIONS 16
788
Johannes Berg8ca151b2013-01-24 14:25:36 +0100789int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
790 int tid, u16 ssn, bool start)
791{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100792 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300793 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100794 int ret;
795 u32 status;
796
797 lockdep_assert_held(&mvm->mutex);
798
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300799 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
800 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
801 return -ENOSPC;
802 }
803
Johannes Berg8ca151b2013-01-24 14:25:36 +0100804 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
805 cmd.sta_id = mvm_sta->sta_id;
806 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +0300807 if (start) {
808 cmd.add_immediate_ba_tid = (u8) tid;
809 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
810 } else {
811 cmd.remove_immediate_ba_tid = (u8) tid;
812 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100813 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
814 STA_MODIFY_REMOVE_BA_TID;
815
816 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300817 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
818 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100819 if (ret)
820 return ret;
821
822 switch (status) {
823 case ADD_STA_SUCCESS:
824 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
825 start ? "start" : "stopp");
826 break;
827 case ADD_STA_IMMEDIATE_BA_FAILURE:
828 IWL_WARN(mvm, "RX BA Session refused by fw\n");
829 ret = -ENOSPC;
830 break;
831 default:
832 ret = -EIO;
833 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
834 start ? "start" : "stopp", status);
835 break;
836 }
837
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300838 if (!ret) {
839 if (start)
840 mvm->rx_ba_sessions++;
841 else if (mvm->rx_ba_sessions > 0)
842 /* check that restart flow didn't zero the counter */
843 mvm->rx_ba_sessions--;
844 }
845
Johannes Berg8ca151b2013-01-24 14:25:36 +0100846 return ret;
847}
848
849static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
850 int tid, u8 queue, bool start)
851{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100852 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300853 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100854 int ret;
855 u32 status;
856
857 lockdep_assert_held(&mvm->mutex);
858
859 if (start) {
860 mvm_sta->tfd_queue_msk |= BIT(queue);
861 mvm_sta->tid_disable_agg &= ~BIT(tid);
862 } else {
863 mvm_sta->tfd_queue_msk &= ~BIT(queue);
864 mvm_sta->tid_disable_agg |= BIT(tid);
865 }
866
867 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
868 cmd.sta_id = mvm_sta->sta_id;
869 cmd.add_modify = STA_MODE_MODIFY;
870 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
871 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
872 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
873
874 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300875 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
876 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100877 if (ret)
878 return ret;
879
880 switch (status) {
881 case ADD_STA_SUCCESS:
882 break;
883 default:
884 ret = -EIO;
885 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
886 start ? "start" : "stopp", status);
887 break;
888 }
889
890 return ret;
891}
892
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +0200893const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100894 IEEE80211_AC_BE,
895 IEEE80211_AC_BK,
896 IEEE80211_AC_BK,
897 IEEE80211_AC_BE,
898 IEEE80211_AC_VI,
899 IEEE80211_AC_VI,
900 IEEE80211_AC_VO,
901 IEEE80211_AC_VO,
902};
903
Johannes Berg3e56ead2013-02-15 22:23:18 +0100904static const u8 tid_to_ucode_ac[] = {
905 AC_BE,
906 AC_BK,
907 AC_BK,
908 AC_BE,
909 AC_VI,
910 AC_VI,
911 AC_VO,
912 AC_VO,
913};
914
Johannes Berg8ca151b2013-01-24 14:25:36 +0100915int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
916 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
917{
Johannes Berg5b577a92013-11-14 18:20:04 +0100918 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100919 struct iwl_mvm_tid_data *tid_data;
920 int txq_id;
921
922 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
923 return -EINVAL;
924
925 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
926 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
927 mvmsta->tid_data[tid].state);
928 return -ENXIO;
929 }
930
931 lockdep_assert_held(&mvm->mutex);
932
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200933 for (txq_id = mvm->first_agg_queue;
934 txq_id <= mvm->last_agg_queue; txq_id++)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100935 if (mvm->queue_to_mac80211[txq_id] ==
936 IWL_INVALID_MAC80211_QUEUE)
937 break;
938
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200939 if (txq_id > mvm->last_agg_queue) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100940 IWL_ERR(mvm, "Failed to allocate agg queue\n");
941 return -EIO;
942 }
943
Arik Nemtsovb2492502014-03-13 12:21:50 +0200944 spin_lock_bh(&mvmsta->lock);
945
946 /* possible race condition - we entered D0i3 while starting agg */
947 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
948 spin_unlock_bh(&mvmsta->lock);
949 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
950 return -EIO;
951 }
952
Johannes Berg8ca151b2013-01-24 14:25:36 +0100953 /* the new tx queue is still connected to the same mac80211 queue */
Johannes Berg3e56ead2013-02-15 22:23:18 +0100954 mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +0100955
Johannes Berg8ca151b2013-01-24 14:25:36 +0100956 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +0100957 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100958 tid_data->txq_id = txq_id;
959 *ssn = tid_data->ssn;
960
961 IWL_DEBUG_TX_QUEUES(mvm,
962 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
963 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
964 tid_data->next_reclaimed);
965
966 if (tid_data->ssn == tid_data->next_reclaimed) {
967 tid_data->state = IWL_AGG_STARTING;
968 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
969 } else {
970 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
971 }
972
973 spin_unlock_bh(&mvmsta->lock);
974
975 return 0;
976}
977
978int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
979 struct ieee80211_sta *sta, u16 tid, u8 buf_size)
980{
Johannes Berg5b577a92013-11-14 18:20:04 +0100981 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100982 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +0200983 unsigned int wdg_timeout =
984 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100985 int queue, fifo, ret;
986 u16 ssn;
987
Eyal Shapiraefed6642014-09-14 15:58:53 +0300988 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
989 != IWL_MAX_TID_COUNT);
990
Johannes Berg8ca151b2013-01-24 14:25:36 +0100991 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
992
993 spin_lock_bh(&mvmsta->lock);
994 ssn = tid_data->ssn;
995 queue = tid_data->txq_id;
996 tid_data->state = IWL_AGG_ON;
Eyal Shapiraefed6642014-09-14 15:58:53 +0300997 mvmsta->agg_tids |= BIT(tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100998 tid_data->ssn = 0xffff;
999 spin_unlock_bh(&mvmsta->lock);
1000
Johannes Berg3e56ead2013-02-15 22:23:18 +01001001 fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001002
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03001003 iwl_mvm_enable_agg_txq(mvm, queue, fifo, mvmsta->sta_id, tid,
1004 buf_size, ssn, wdg_timeout);
1005
Johannes Berg8ca151b2013-01-24 14:25:36 +01001006 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1007 if (ret)
1008 return -EIO;
1009
Johannes Berg8ca151b2013-01-24 14:25:36 +01001010 /*
1011 * Even though in theory the peer could have different
1012 * aggregation reorder buffer sizes for different sessions,
1013 * our ucode doesn't allow for that and has a global limit
1014 * for each station. Therefore, use the minimum of all the
1015 * aggregation sessions and our default value.
1016 */
1017 mvmsta->max_agg_bufsize =
1018 min(mvmsta->max_agg_bufsize, buf_size);
1019 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
1020
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03001021 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
1022 sta->addr, tid);
1023
Eyal Shapira9e680942013-11-09 00:16:16 +02001024 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001025}
1026
1027int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1028 struct ieee80211_sta *sta, u16 tid)
1029{
Johannes Berg5b577a92013-11-14 18:20:04 +01001030 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001031 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1032 u16 txq_id;
1033 int err;
1034
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +02001035
1036 /*
1037 * If mac80211 is cleaning its state, then say that we finished since
1038 * our state has been cleared anyway.
1039 */
1040 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1041 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1042 return 0;
1043 }
1044
Johannes Berg8ca151b2013-01-24 14:25:36 +01001045 spin_lock_bh(&mvmsta->lock);
1046
1047 txq_id = tid_data->txq_id;
1048
1049 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
1050 mvmsta->sta_id, tid, txq_id, tid_data->state);
1051
Eyal Shapiraefed6642014-09-14 15:58:53 +03001052 mvmsta->agg_tids &= ~BIT(tid);
1053
Johannes Berg8ca151b2013-01-24 14:25:36 +01001054 switch (tid_data->state) {
1055 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01001056 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001057
1058 IWL_DEBUG_TX_QUEUES(mvm,
1059 "ssn = %d, next_recl = %d\n",
1060 tid_data->ssn, tid_data->next_reclaimed);
1061
1062 /* There are still packets for this RA / TID in the HW */
1063 if (tid_data->ssn != tid_data->next_reclaimed) {
1064 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
1065 err = 0;
1066 break;
1067 }
1068
1069 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +02001070 tid_data->state = IWL_AGG_OFF;
1071 mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
1072 spin_unlock_bh(&mvmsta->lock);
1073
1074 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1075
1076 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1077
Emmanuel Grumbach0294d9e2015-01-05 16:52:55 +02001078 iwl_mvm_disable_txq(mvm, txq_id, 0);
Johannes Bergf7f89e72014-08-05 15:24:44 +02001079 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001080 case IWL_AGG_STARTING:
1081 case IWL_EMPTYING_HW_QUEUE_ADDBA:
1082 /*
1083 * The agg session has been stopped before it was set up. This
1084 * can happen when the AddBA timer times out for example.
1085 */
1086
1087 /* No barriers since we are under mutex */
1088 lockdep_assert_held(&mvm->mutex);
1089 mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
1090
1091 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1092 tid_data->state = IWL_AGG_OFF;
1093 err = 0;
1094 break;
1095 default:
1096 IWL_ERR(mvm,
1097 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
1098 mvmsta->sta_id, tid, tid_data->state);
1099 IWL_ERR(mvm,
1100 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
1101 err = -EINVAL;
1102 }
1103
1104 spin_unlock_bh(&mvmsta->lock);
1105
1106 return err;
1107}
1108
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001109int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1110 struct ieee80211_sta *sta, u16 tid)
1111{
Johannes Berg5b577a92013-11-14 18:20:04 +01001112 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001113 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1114 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02001115 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001116
1117 /*
1118 * First set the agg state to OFF to avoid calling
1119 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
1120 */
1121 spin_lock_bh(&mvmsta->lock);
1122 txq_id = tid_data->txq_id;
1123 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
1124 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001125 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001126 tid_data->state = IWL_AGG_OFF;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001127 mvmsta->agg_tids &= ~BIT(tid);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001128 spin_unlock_bh(&mvmsta->lock);
1129
Johannes Bergb6658ff2013-07-24 13:55:51 +02001130 if (old_state >= IWL_AGG_ON) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001131 iwl_mvm_drain_sta(mvm, mvmsta, true);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001132 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true))
1133 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001134 iwl_trans_wait_tx_queue_empty(mvm->trans,
1135 mvmsta->tfd_queue_msk);
1136 iwl_mvm_drain_sta(mvm, mvmsta, false);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001137
Johannes Bergf7f89e72014-08-05 15:24:44 +02001138 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1139
Emmanuel Grumbach0294d9e2015-01-05 16:52:55 +02001140 iwl_mvm_disable_txq(mvm, tid_data->txq_id, 0);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001141 }
1142
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001143 mvm->queue_to_mac80211[tid_data->txq_id] =
1144 IWL_INVALID_MAC80211_QUEUE;
1145
1146 return 0;
1147}
1148
Johannes Berg8ca151b2013-01-24 14:25:36 +01001149static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
1150{
Johannes Berg2dc2a152015-06-16 17:09:18 +02001151 int i, max = -1, max_offs = -1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001152
1153 lockdep_assert_held(&mvm->mutex);
1154
Johannes Berg2dc2a152015-06-16 17:09:18 +02001155 /* Pick the unused key offset with the highest 'deleted'
1156 * counter. Every time a key is deleted, all the counters
1157 * are incremented and the one that was just deleted is
1158 * reset to zero. Thus, the highest counter is the one
1159 * that was deleted longest ago. Pick that one.
1160 */
1161 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
1162 if (test_bit(i, mvm->fw_key_table))
1163 continue;
1164 if (mvm->fw_key_deleted[i] > max) {
1165 max = mvm->fw_key_deleted[i];
1166 max_offs = i;
1167 }
1168 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001169
Johannes Berg2dc2a152015-06-16 17:09:18 +02001170 if (max_offs < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001171 return STA_KEY_IDX_INVALID;
1172
Johannes Berg2dc2a152015-06-16 17:09:18 +02001173 __set_bit(max_offs, mvm->fw_key_table);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001174
Johannes Berg2dc2a152015-06-16 17:09:18 +02001175 return max_offs;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001176}
1177
1178static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
1179 struct ieee80211_sta *sta)
1180{
Johannes Berg5b530e92014-12-23 16:00:17 +01001181 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001182
1183 if (sta) {
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001184 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001185
1186 return mvm_sta->sta_id;
1187 }
1188
1189 /*
1190 * The device expects GTKs for station interfaces to be
1191 * installed as GTKs for the AP station. If we have no
1192 * station ID, then use AP's station ID.
1193 */
1194 if (vif->type == NL80211_IFTYPE_STATION &&
1195 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
1196 return mvmvif->ap_sta_id;
1197
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001198 return IWL_MVM_STATION_COUNT;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001199}
1200
1201static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
1202 struct iwl_mvm_sta *mvm_sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01001203 struct ieee80211_key_conf *keyconf, bool mcast,
Johannes Berg2f6319d2014-11-12 23:39:56 +01001204 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001205{
Max Stepanov5a258aa2013-04-07 09:11:21 +03001206 struct iwl_mvm_add_sta_key_cmd cmd = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001207 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01001208 int ret;
1209 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001210 u16 keyidx;
1211 int i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001212 u8 sta_id = mvm_sta->sta_id;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001213
1214 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1215 STA_KEY_FLG_KEYID_MSK;
1216 key_flags = cpu_to_le16(keyidx);
1217 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
1218
1219 switch (keyconf->cipher) {
1220 case WLAN_CIPHER_SUITE_TKIP:
1221 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001222 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001223 for (i = 0; i < 5; i++)
Max Stepanov5a258aa2013-04-07 09:11:21 +03001224 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1225 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001226 break;
1227 case WLAN_CIPHER_SUITE_CCMP:
1228 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001229 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001230 break;
Johannes Bergba3943b2014-11-12 23:54:48 +01001231 case WLAN_CIPHER_SUITE_WEP104:
1232 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
John W. Linvilleaa0cb082015-01-12 16:18:11 -05001233 /* fall through */
Johannes Bergba3943b2014-11-12 23:54:48 +01001234 case WLAN_CIPHER_SUITE_WEP40:
1235 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
1236 memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
1237 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001238 default:
Max Stepanove36e5432013-08-27 19:56:13 +03001239 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
1240 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001241 }
1242
Johannes Bergba3943b2014-11-12 23:54:48 +01001243 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001244 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1245
Max Stepanov5a258aa2013-04-07 09:11:21 +03001246 cmd.key_offset = keyconf->hw_key_idx;
1247 cmd.key_flags = key_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001248 cmd.sta_id = sta_id;
1249
1250 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001251 if (cmd_flags & CMD_ASYNC)
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001252 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
1253 sizeof(cmd), &cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001254 else
1255 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1256 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001257
1258 switch (status) {
1259 case ADD_STA_SUCCESS:
1260 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1261 break;
1262 default:
1263 ret = -EIO;
1264 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1265 break;
1266 }
1267
1268 return ret;
1269}
1270
1271static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1272 struct ieee80211_key_conf *keyconf,
1273 u8 sta_id, bool remove_key)
1274{
1275 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1276
1277 /* verify the key details match the required command's expectations */
1278 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1279 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1280 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1281 return -EINVAL;
1282
1283 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1284 igtk_cmd.sta_id = cpu_to_le32(sta_id);
1285
1286 if (remove_key) {
1287 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1288 } else {
1289 struct ieee80211_key_seq seq;
1290 const u8 *pn;
1291
1292 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001293 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1294 pn = seq.aes_cmac.pn;
1295 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1296 ((u64) pn[4] << 8) |
1297 ((u64) pn[3] << 16) |
1298 ((u64) pn[2] << 24) |
1299 ((u64) pn[1] << 32) |
1300 ((u64) pn[0] << 40));
1301 }
1302
1303 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1304 remove_key ? "removing" : "installing",
1305 igtk_cmd.sta_id);
1306
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001307 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001308 sizeof(igtk_cmd), &igtk_cmd);
1309}
1310
1311
1312static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1313 struct ieee80211_vif *vif,
1314 struct ieee80211_sta *sta)
1315{
Johannes Berg5b530e92014-12-23 16:00:17 +01001316 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001317
1318 if (sta)
1319 return sta->addr;
1320
1321 if (vif->type == NL80211_IFTYPE_STATION &&
1322 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1323 u8 sta_id = mvmvif->ap_sta_id;
1324 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1325 lockdep_is_held(&mvm->mutex));
1326 return sta->addr;
1327 }
1328
1329
1330 return NULL;
1331}
1332
Johannes Berg2f6319d2014-11-12 23:39:56 +01001333static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1334 struct ieee80211_vif *vif,
1335 struct ieee80211_sta *sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01001336 struct ieee80211_key_conf *keyconf,
1337 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001338{
Johannes Berg2f6319d2014-11-12 23:39:56 +01001339 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001340 int ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001341 const u8 *addr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001342 struct ieee80211_key_seq seq;
1343 u16 p1k[5];
1344
Johannes Berg8ca151b2013-01-24 14:25:36 +01001345 switch (keyconf->cipher) {
1346 case WLAN_CIPHER_SUITE_TKIP:
1347 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1348 /* get phase 1 key from mac80211 */
1349 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1350 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Bergba3943b2014-11-12 23:54:48 +01001351 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001352 seq.tkip.iv32, p1k, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001353 break;
1354 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergba3943b2014-11-12 23:54:48 +01001355 case WLAN_CIPHER_SUITE_WEP40:
1356 case WLAN_CIPHER_SUITE_WEP104:
1357 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001358 0, NULL, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001359 break;
1360 default:
Johannes Bergba3943b2014-11-12 23:54:48 +01001361 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Johannes Berg2f6319d2014-11-12 23:39:56 +01001362 0, NULL, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001363 }
1364
Johannes Berg8ca151b2013-01-24 14:25:36 +01001365 return ret;
1366}
1367
Johannes Berg2f6319d2014-11-12 23:39:56 +01001368static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
Johannes Bergba3943b2014-11-12 23:54:48 +01001369 struct ieee80211_key_conf *keyconf,
1370 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001371{
Max Stepanov5a258aa2013-04-07 09:11:21 +03001372 struct iwl_mvm_add_sta_key_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001373 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01001374 int ret;
1375 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001376
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02001377 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1378 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001379 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1380 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1381
Johannes Bergba3943b2014-11-12 23:54:48 +01001382 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001383 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1384
Max Stepanov5a258aa2013-04-07 09:11:21 +03001385 cmd.key_flags = key_flags;
1386 cmd.key_offset = keyconf->hw_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001387 cmd.sta_id = sta_id;
1388
Johannes Berg8ca151b2013-01-24 14:25:36 +01001389 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001390 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1391 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001392
1393 switch (status) {
1394 case ADD_STA_SUCCESS:
1395 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1396 break;
1397 default:
1398 ret = -EIO;
1399 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1400 break;
1401 }
1402
1403 return ret;
1404}
1405
Johannes Berg2f6319d2014-11-12 23:39:56 +01001406int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1407 struct ieee80211_vif *vif,
1408 struct ieee80211_sta *sta,
1409 struct ieee80211_key_conf *keyconf,
1410 bool have_key_offset)
1411{
Johannes Bergba3943b2014-11-12 23:54:48 +01001412 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg2f6319d2014-11-12 23:39:56 +01001413 u8 sta_id;
1414 int ret;
Matti Gottlieb11828db2015-06-01 15:15:11 +03001415 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
Johannes Berg2f6319d2014-11-12 23:39:56 +01001416
1417 lockdep_assert_held(&mvm->mutex);
1418
1419 /* Get the station id from the mvm local station table */
1420 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1421 if (sta_id == IWL_MVM_STATION_COUNT) {
1422 IWL_ERR(mvm, "Failed to find station id\n");
1423 return -EINVAL;
1424 }
1425
1426 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1427 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1428 goto end;
1429 }
1430
1431 /*
1432 * It is possible that the 'sta' parameter is NULL, and thus
1433 * there is a need to retrieve the sta from the local station table.
1434 */
1435 if (!sta) {
1436 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1437 lockdep_is_held(&mvm->mutex));
1438 if (IS_ERR_OR_NULL(sta)) {
1439 IWL_ERR(mvm, "Invalid station id\n");
1440 return -EINVAL;
1441 }
1442 }
1443
1444 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
1445 return -EINVAL;
1446
1447 if (!have_key_offset) {
1448 /*
1449 * The D3 firmware hardcodes the PTK offset to 0, so we have to
1450 * configure it there. As a result, this workaround exists to
1451 * let the caller set the key offset (hw_key_idx), see d3.c.
1452 */
1453 keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
1454 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1455 return -ENOSPC;
1456 }
1457
Johannes Bergba3943b2014-11-12 23:54:48 +01001458 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, mcast);
1459 if (ret) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01001460 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
Johannes Bergba3943b2014-11-12 23:54:48 +01001461 goto end;
1462 }
1463
1464 /*
1465 * For WEP, the same key is used for multicast and unicast. Upload it
1466 * again, using the same key offset, and now pointing the other one
1467 * to the same key slot (offset).
1468 * If this fails, remove the original as well.
1469 */
1470 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
1471 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
1472 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, !mcast);
1473 if (ret) {
1474 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1475 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
1476 }
1477 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01001478
1479end:
1480 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1481 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Matti Gottlieb11828db2015-06-01 15:15:11 +03001482 sta ? sta->addr : zero_addr, ret);
Johannes Berg2f6319d2014-11-12 23:39:56 +01001483 return ret;
1484}
1485
1486int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1487 struct ieee80211_vif *vif,
1488 struct ieee80211_sta *sta,
1489 struct ieee80211_key_conf *keyconf)
1490{
Johannes Bergba3943b2014-11-12 23:54:48 +01001491 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg2f6319d2014-11-12 23:39:56 +01001492 u8 sta_id;
Johannes Berg2dc2a152015-06-16 17:09:18 +02001493 int ret, i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001494
1495 lockdep_assert_held(&mvm->mutex);
1496
1497 /* Get the station id from the mvm local station table */
1498 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1499
1500 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1501 keyconf->keyidx, sta_id);
1502
1503 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1504 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1505
1506 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
1507 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1508 keyconf->hw_key_idx);
1509 return -ENOENT;
1510 }
1511
Johannes Berg2dc2a152015-06-16 17:09:18 +02001512 /* track which key was deleted last */
1513 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
1514 if (mvm->fw_key_deleted[i] < U8_MAX)
1515 mvm->fw_key_deleted[i]++;
1516 }
1517 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
1518
Johannes Berg2f6319d2014-11-12 23:39:56 +01001519 if (sta_id == IWL_MVM_STATION_COUNT) {
1520 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1521 return 0;
1522 }
1523
1524 /*
1525 * It is possible that the 'sta' parameter is NULL, and thus
1526 * there is a need to retrieve the sta from the local station table,
1527 * for example when a GTK is removed (where the sta_id will then be
1528 * the AP ID, and no station was passed by mac80211.)
1529 */
1530 if (!sta) {
1531 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1532 lockdep_is_held(&mvm->mutex));
1533 if (!sta) {
1534 IWL_ERR(mvm, "Invalid station id\n");
1535 return -EINVAL;
1536 }
1537 }
1538
1539 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
1540 return -EINVAL;
1541
Johannes Bergba3943b2014-11-12 23:54:48 +01001542 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
1543 if (ret)
1544 return ret;
1545
1546 /* delete WEP key twice to get rid of (now useless) offset */
1547 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
1548 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
1549 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
1550
1551 return ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001552}
1553
Johannes Berg8ca151b2013-01-24 14:25:36 +01001554void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1555 struct ieee80211_vif *vif,
1556 struct ieee80211_key_conf *keyconf,
1557 struct ieee80211_sta *sta, u32 iv32,
1558 u16 *phase1key)
1559{
Beni Levc3eb5362013-02-06 17:22:18 +02001560 struct iwl_mvm_sta *mvm_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001561 u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
Johannes Bergba3943b2014-11-12 23:54:48 +01001562 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001563
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001564 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001565 return;
1566
Beni Levc3eb5362013-02-06 17:22:18 +02001567 rcu_read_lock();
1568
1569 if (!sta) {
1570 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1571 if (WARN_ON(IS_ERR_OR_NULL(sta))) {
1572 rcu_read_unlock();
1573 return;
1574 }
1575 }
1576
Johannes Berg2f6319d2014-11-12 23:39:56 +01001577 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Bergba3943b2014-11-12 23:54:48 +01001578 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001579 iv32, phase1key, CMD_ASYNC);
Beni Levc3eb5362013-02-06 17:22:18 +02001580 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01001581}
1582
Johannes Berg9cc40712013-02-15 22:47:48 +01001583void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
1584 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001585{
Johannes Berg5b577a92013-11-14 18:20:04 +01001586 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001587 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001588 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01001589 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03001590 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01001591 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01001592 };
1593 int ret;
1594
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001595 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001596 if (ret)
1597 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1598}
1599
Johannes Berg9cc40712013-02-15 22:47:48 +01001600void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
1601 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001602 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01001603 u16 cnt, u16 tids, bool more_data,
1604 bool agg)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001605{
Johannes Berg5b577a92013-11-14 18:20:04 +01001606 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001607 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001608 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01001609 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001610 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1611 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01001612 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01001613 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01001614 int tid, ret;
1615 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001616
Johannes Berg3e56ead2013-02-15 22:23:18 +01001617 /* convert TIDs to ACs - we don't support TSPEC so that's OK
1618 * Note that this field is reserved and unused by firmware not
1619 * supporting GO uAPSD, so it's safe to always do this.
1620 */
1621 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
1622 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
1623
1624 /* If we're releasing frames from aggregation queues then check if the
1625 * all queues combined that we're releasing frames from have
1626 * - more frames than the service period, in which case more_data
1627 * needs to be set
1628 * - fewer than 'cnt' frames, in which case we need to adjust the
1629 * firmware command (but do that unconditionally)
1630 */
1631 if (agg) {
1632 int remaining = cnt;
1633
1634 spin_lock_bh(&mvmsta->lock);
1635 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
1636 struct iwl_mvm_tid_data *tid_data;
1637 u16 n_queued;
1638
1639 tid_data = &mvmsta->tid_data[tid];
1640 if (WARN(tid_data->state != IWL_AGG_ON &&
1641 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
1642 "TID %d state is %d\n",
1643 tid, tid_data->state)) {
1644 spin_unlock_bh(&mvmsta->lock);
1645 ieee80211_sta_eosp(sta);
1646 return;
1647 }
1648
1649 n_queued = iwl_mvm_tid_queued(tid_data);
1650 if (n_queued > remaining) {
1651 more_data = true;
1652 remaining = 0;
1653 break;
1654 }
1655 remaining -= n_queued;
1656 }
1657 spin_unlock_bh(&mvmsta->lock);
1658
1659 cmd.sleep_tx_count = cpu_to_le16(cnt - remaining);
1660 if (WARN_ON(cnt - remaining == 0)) {
1661 ieee80211_sta_eosp(sta);
1662 return;
1663 }
1664 }
1665
1666 /* Note: this is ignored by firmware not supporting GO uAPSD */
1667 if (more_data)
1668 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
1669
1670 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
1671 mvmsta->next_status_eosp = true;
1672 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
1673 } else {
1674 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
1675 }
1676
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001677 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001678 if (ret)
1679 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1680}
Johannes Berg3e56ead2013-02-15 22:23:18 +01001681
Johannes Berg04168412015-06-23 21:22:09 +02001682void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
1683 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg3e56ead2013-02-15 22:23:18 +01001684{
1685 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1686 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
1687 struct ieee80211_sta *sta;
1688 u32 sta_id = le32_to_cpu(notif->sta_id);
1689
1690 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +02001691 return;
Johannes Berg3e56ead2013-02-15 22:23:18 +01001692
1693 rcu_read_lock();
1694 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1695 if (!IS_ERR_OR_NULL(sta))
1696 ieee80211_sta_eosp(sta);
1697 rcu_read_unlock();
Johannes Berg3e56ead2013-02-15 22:23:18 +01001698}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03001699
1700void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
1701 struct iwl_mvm_sta *mvmsta, bool disable)
1702{
1703 struct iwl_mvm_add_sta_cmd cmd = {
1704 .add_modify = STA_MODE_MODIFY,
1705 .sta_id = mvmsta->sta_id,
1706 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
1707 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
1708 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1709 };
1710 int ret;
1711
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03001712 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1713 if (ret)
1714 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1715}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001716
1717void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
1718 struct ieee80211_sta *sta,
1719 bool disable)
1720{
1721 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1722
1723 spin_lock_bh(&mvm_sta->lock);
1724
1725 if (mvm_sta->disable_tx == disable) {
1726 spin_unlock_bh(&mvm_sta->lock);
1727 return;
1728 }
1729
1730 mvm_sta->disable_tx = disable;
1731
1732 /*
Sara Sharon0d365ae2015-03-31 12:24:05 +03001733 * Tell mac80211 to start/stop queuing tx for this station,
1734 * but don't stop queuing if there are still pending frames
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001735 * for this station.
1736 */
1737 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
1738 ieee80211_sta_block_awake(mvm->hw, sta, disable);
1739
1740 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
1741
1742 spin_unlock_bh(&mvm_sta->lock);
1743}
1744
1745void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
1746 struct iwl_mvm_vif *mvmvif,
1747 bool disable)
1748{
1749 struct ieee80211_sta *sta;
1750 struct iwl_mvm_sta *mvm_sta;
1751 int i;
1752
1753 lockdep_assert_held(&mvm->mutex);
1754
1755 /* Block/unblock all the stations of the given mvmvif */
1756 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
1757 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
1758 lockdep_is_held(&mvm->mutex));
1759 if (IS_ERR_OR_NULL(sta))
1760 continue;
1761
1762 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1763 if (mvm_sta->mac_id_n_color !=
1764 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
1765 continue;
1766
1767 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
1768 }
1769}
Luciano Coelhodc88b4b2014-11-10 11:10:14 +02001770
1771void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1772{
1773 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1774 struct iwl_mvm_sta *mvmsta;
1775
1776 rcu_read_lock();
1777
1778 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
1779
1780 if (!WARN_ON(!mvmsta))
1781 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
1782
1783 rcu_read_unlock();
1784}