blob: 2c9675b45350800fb4fda68807309a61f8f51299 [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:
Emmanuel Grumbachcb2f8272015-11-17 15:39:56 +020029 * Intel Linux Wireless <linuxwifi@intel.com>
Johannes Berg8ca151b2013-01-24 14:25:36 +010030 * 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),
Liad Kaufmancf0cda12015-09-24 10:44:12 +0200109 .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
Emmanuel Grumbach4b8265a2014-07-13 08:58:04 +0300110 };
Johannes Berg8ca151b2013-01-24 14:25:36 +0100111 int ret;
112 u32 status;
113 u32 agg_size = 0, mpdu_dens = 0;
114
Johannes Berg7a453972013-02-12 13:10:44 +0100115 if (!update) {
116 add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
117 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
118 }
Johannes Berg5bc5aaa2013-02-12 14:35:36 +0100119
120 switch (sta->bandwidth) {
121 case IEEE80211_STA_RX_BW_160:
122 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
123 /* fall through */
124 case IEEE80211_STA_RX_BW_80:
125 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
126 /* fall through */
127 case IEEE80211_STA_RX_BW_40:
128 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
129 /* fall through */
130 case IEEE80211_STA_RX_BW_20:
131 if (sta->ht_cap.ht_supported)
132 add_sta_cmd.station_flags |=
133 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
134 break;
135 }
136
137 switch (sta->rx_nss) {
138 case 1:
139 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
140 break;
141 case 2:
142 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
143 break;
144 case 3 ... 8:
145 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
146 break;
147 }
148
149 switch (sta->smps_mode) {
150 case IEEE80211_SMPS_AUTOMATIC:
151 case IEEE80211_SMPS_NUM_MODES:
152 WARN_ON(1);
153 break;
154 case IEEE80211_SMPS_STATIC:
155 /* override NSS */
156 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
157 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
158 break;
159 case IEEE80211_SMPS_DYNAMIC:
160 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
161 break;
162 case IEEE80211_SMPS_OFF:
163 /* nothing */
164 break;
165 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100166
167 if (sta->ht_cap.ht_supported) {
168 add_sta_cmd.station_flags_msk |=
169 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
170 STA_FLG_AGG_MPDU_DENS_MSK);
171
172 mpdu_dens = sta->ht_cap.ampdu_density;
173 }
174
175 if (sta->vht_cap.vht_supported) {
176 agg_size = sta->vht_cap.cap &
177 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
178 agg_size >>=
179 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
180 } else if (sta->ht_cap.ht_supported) {
181 agg_size = sta->ht_cap.ampdu_factor;
182 }
183
184 add_sta_cmd.station_flags |=
185 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
186 add_sta_cmd.station_flags |=
187 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
188
189 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300190 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(add_sta_cmd),
191 &add_sta_cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100192 if (ret)
193 return ret;
194
195 switch (status) {
196 case ADD_STA_SUCCESS:
197 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
198 break;
199 default:
200 ret = -EIO;
201 IWL_ERR(mvm, "ADD_STA failed\n");
202 break;
203 }
204
205 return ret;
206}
207
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300208static int iwl_mvm_tdls_sta_init(struct iwl_mvm *mvm,
209 struct ieee80211_sta *sta)
210{
211 unsigned long used_hw_queues;
212 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +0200213 unsigned int wdg_timeout =
214 iwl_mvm_get_wd_timeout(mvm, NULL, true, false);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300215 u32 ac;
216
217 lockdep_assert_held(&mvm->mutex);
218
219 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, NULL);
220
221 /* Find available queues, and allocate them to the ACs */
222 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
223 u8 queue = find_first_zero_bit(&used_hw_queues,
224 mvm->first_agg_queue);
225
226 if (queue >= mvm->first_agg_queue) {
227 IWL_ERR(mvm, "Failed to allocate STA queue\n");
228 return -EBUSY;
229 }
230
231 __set_bit(queue, &used_hw_queues);
232 mvmsta->hw_queue[ac] = queue;
233 }
234
235 /* Found a place for all queues - enable them */
236 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
237 iwl_mvm_enable_ac_txq(mvm, mvmsta->hw_queue[ac],
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300238 mvmsta->hw_queue[ac],
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300239 iwl_mvm_ac_to_tx_fifo[ac], 0,
240 wdg_timeout);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300241 mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
242 }
243
244 return 0;
245}
246
247static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
248 struct ieee80211_sta *sta)
249{
250 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
251 unsigned long sta_msk;
252 int i;
253
254 lockdep_assert_held(&mvm->mutex);
255
256 /* disable the TDLS STA-specific queues */
257 sta_msk = mvmsta->tfd_queue_msk;
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +0200258 for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +0300259 iwl_mvm_disable_txq(mvm, i, i, IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300260}
261
Johannes Berg8ca151b2013-01-24 14:25:36 +0100262int iwl_mvm_add_sta(struct iwl_mvm *mvm,
263 struct ieee80211_vif *vif,
264 struct ieee80211_sta *sta)
265{
266 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100267 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100268 int i, ret, sta_id;
269
270 lockdep_assert_held(&mvm->mutex);
271
272 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
Eliad Pellerb92e6612014-01-23 17:58:23 +0200273 sta_id = iwl_mvm_find_free_sta_id(mvm,
274 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100275 else
276 sta_id = mvm_sta->sta_id;
277
Johannes Berg36f46312015-03-10 20:32:08 +0100278 if (sta_id == IWL_MVM_STATION_COUNT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100279 return -ENOSPC;
280
Gregory Greenman94939082015-08-24 14:38:35 +0300281 if (vif->type == NL80211_IFTYPE_AP) {
282 mvmvif->ap_assoc_sta_count++;
Ilan Peerf82c8332015-09-10 12:54:38 +0300283 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
Gregory Greenman94939082015-08-24 14:38:35 +0300284 }
285
Johannes Berg8ca151b2013-01-24 14:25:36 +0100286 spin_lock_init(&mvm_sta->lock);
287
288 mvm_sta->sta_id = sta_id;
289 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
290 mvmvif->color);
291 mvm_sta->vif = vif;
292 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300293 mvm_sta->tx_protection = 0;
294 mvm_sta->tt_tx_protection = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100295
296 /* HW restart, don't assume the memory has been zeroed */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300297 atomic_set(&mvm->pending_frames[sta_id], 0);
Liad Kaufman69191af2015-09-01 18:50:22 +0300298 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100299 mvm_sta->tfd_queue_msk = 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300300
301 /* allocate new queues for a TDLS station */
302 if (sta->tdls) {
303 ret = iwl_mvm_tdls_sta_init(mvm, sta);
304 if (ret)
305 return ret;
306 } else {
307 for (i = 0; i < IEEE80211_NUM_ACS; i++)
308 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
309 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
310 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100311
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200312 /* for HW restart - reset everything but the sequence number */
313 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
314 u16 seq = mvm_sta->tid_data[i].seq_number;
315 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
316 mvm_sta->tid_data[i].seq_number = seq;
317 }
Eyal Shapiraefed6642014-09-14 15:58:53 +0300318 mvm_sta->agg_tids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100319
Johannes Berg7a453972013-02-12 13:10:44 +0100320 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100321 if (ret)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300322 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100323
Johannes Berg9e848012014-08-04 14:33:42 +0200324 if (vif->type == NL80211_IFTYPE_STATION) {
325 if (!sta->tdls) {
326 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
327 mvmvif->ap_sta_id = sta_id;
328 } else {
329 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
330 }
331 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100332
333 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
334
335 return 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300336
337err:
338 iwl_mvm_tdls_sta_deinit(mvm, sta);
339 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100340}
341
Johannes Berg7a453972013-02-12 13:10:44 +0100342int iwl_mvm_update_sta(struct iwl_mvm *mvm,
343 struct ieee80211_vif *vif,
344 struct ieee80211_sta *sta)
345{
346 return iwl_mvm_sta_send_to_fw(mvm, sta, true);
347}
348
Johannes Berg8ca151b2013-01-24 14:25:36 +0100349int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
350 bool drain)
351{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300352 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100353 int ret;
354 u32 status;
355
356 lockdep_assert_held(&mvm->mutex);
357
358 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
359 cmd.sta_id = mvmsta->sta_id;
360 cmd.add_modify = STA_MODE_MODIFY;
361 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
362 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
363
364 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300365 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
366 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100367 if (ret)
368 return ret;
369
370 switch (status) {
371 case ADD_STA_SUCCESS:
372 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
373 mvmsta->sta_id);
374 break;
375 default:
376 ret = -EIO;
377 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
378 mvmsta->sta_id);
379 break;
380 }
381
382 return ret;
383}
384
385/*
386 * Remove a station from the FW table. Before sending the command to remove
387 * the station validate that the station is indeed known to the driver (sanity
388 * only).
389 */
390static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
391{
392 struct ieee80211_sta *sta;
393 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
394 .sta_id = sta_id,
395 };
396 int ret;
397
398 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
399 lockdep_is_held(&mvm->mutex));
400
401 /* Note: internal stations are marked as error values */
402 if (!sta) {
403 IWL_ERR(mvm, "Invalid station id\n");
404 return -EINVAL;
405 }
406
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300407 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100408 sizeof(rm_sta_cmd), &rm_sta_cmd);
409 if (ret) {
410 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
411 return ret;
412 }
413
414 return 0;
415}
416
417void iwl_mvm_sta_drained_wk(struct work_struct *wk)
418{
419 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
420 u8 sta_id;
421
422 /*
423 * The mutex is needed because of the SYNC cmd, but not only: if the
424 * work would run concurrently with iwl_mvm_rm_sta, it would run before
425 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
426 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
427 * that later.
428 */
429 mutex_lock(&mvm->mutex);
430
431 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
432 int ret;
433 struct ieee80211_sta *sta =
434 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
435 lockdep_is_held(&mvm->mutex));
436
Johannes Berg1ddbbb02013-12-04 22:39:17 +0100437 /*
438 * This station is in use or RCU-removed; the latter happens in
439 * managed mode, where mac80211 removes the station before we
440 * can remove it from firmware (we can only do that after the
441 * MAC is marked unassociated), and possibly while the deauth
442 * frame to disconnect from the AP is still queued. Then, the
443 * station pointer is -ENOENT when the last skb is reclaimed.
444 */
445 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100446 continue;
447
448 if (PTR_ERR(sta) == -EINVAL) {
449 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
450 sta_id);
451 continue;
452 }
453
454 if (!sta) {
455 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
456 sta_id);
457 continue;
458 }
459
460 WARN_ON(PTR_ERR(sta) != -EBUSY);
461 /* This station was removed and we waited until it got drained,
462 * we can now proceed and remove it.
463 */
464 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
465 if (ret) {
466 IWL_ERR(mvm,
467 "Couldn't remove sta %d after it was drained\n",
468 sta_id);
469 continue;
470 }
Monam Agarwalc531c772014-03-24 00:05:56 +0530471 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100472 clear_bit(sta_id, mvm->sta_drained);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300473
474 if (mvm->tfd_drained[sta_id]) {
475 unsigned long i, msk = mvm->tfd_drained[sta_id];
476
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +0200477 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +0300478 iwl_mvm_disable_txq(mvm, i, i,
479 IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300480
481 mvm->tfd_drained[sta_id] = 0;
482 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
483 sta_id, msk);
484 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100485 }
486
487 mutex_unlock(&mvm->mutex);
488}
489
490int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
491 struct ieee80211_vif *vif,
492 struct ieee80211_sta *sta)
493{
494 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100495 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100496 int ret;
497
498 lockdep_assert_held(&mvm->mutex);
499
500 if (vif->type == NL80211_IFTYPE_STATION &&
501 mvmvif->ap_sta_id == mvm_sta->sta_id) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +0200502 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
503 if (ret)
504 return ret;
Emmanuel Grumbach80d85652013-02-19 15:32:42 +0200505 /* flush its queues here since we are freeing mvm_sta */
Luca Coelho5888a402015-10-06 09:54:57 +0300506 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +0200507 if (ret)
508 return ret;
509 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
510 mvm_sta->tfd_queue_msk);
511 if (ret)
512 return ret;
513 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
Emmanuel Grumbach80d85652013-02-19 15:32:42 +0200514
Johannes Berg8ca151b2013-01-24 14:25:36 +0100515 /* if we are associated - we can't remove the AP STA now */
516 if (vif->bss_conf.assoc)
517 return ret;
518
519 /* unassoc - go ahead - remove the AP STA now */
520 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
Eliad Peller37577fe2013-12-05 17:19:39 +0200521
522 /* clear d0i3_ap_sta_id if no longer relevant */
523 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
524 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100525 }
526
527 /*
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300528 * This shouldn't happen - the TDLS channel switch should be canceled
529 * before the STA is removed.
530 */
531 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
532 mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
533 cancel_delayed_work(&mvm->tdls_cs.dwork);
534 }
535
536 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300537 * Make sure that the tx response code sees the station as -EBUSY and
538 * calls the drain worker.
539 */
540 spin_lock_bh(&mvm_sta->lock);
541 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +0100542 * There are frames pending on the AC queues for this station.
543 * We need to wait until all the frames are drained...
544 */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300545 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100546 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
547 ERR_PTR(-EBUSY));
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300548 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300549
550 /* disable TDLS sta queues on drain complete */
551 if (sta->tdls) {
552 mvm->tfd_drained[mvm_sta->sta_id] =
553 mvm_sta->tfd_queue_msk;
554 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
555 mvm_sta->sta_id);
556 }
557
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300558 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100559 } else {
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300560 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300561
562 if (sta->tdls)
563 iwl_mvm_tdls_sta_deinit(mvm, sta);
564
Johannes Berg8ca151b2013-01-24 14:25:36 +0100565 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
Monam Agarwalc531c772014-03-24 00:05:56 +0530566 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100567 }
568
569 return ret;
570}
571
572int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
573 struct ieee80211_vif *vif,
574 u8 sta_id)
575{
576 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
577
578 lockdep_assert_held(&mvm->mutex);
579
Monam Agarwalc531c772014-03-24 00:05:56 +0530580 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100581 return ret;
582}
583
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200584int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
585 struct iwl_mvm_int_sta *sta,
586 u32 qmask, enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100587{
588 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +0200589 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100590 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
591 return -ENOSPC;
592 }
593
594 sta->tfd_queue_msk = qmask;
595
596 /* put a non-NULL value so iterating over the stations won't stop */
597 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
598 return 0;
599}
600
Johannes Berg712b24a2014-08-04 14:14:14 +0200601static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
602 struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100603{
Monam Agarwalc531c772014-03-24 00:05:56 +0530604 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100605 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
606 sta->sta_id = IWL_MVM_STATION_COUNT;
607}
608
609static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
610 struct iwl_mvm_int_sta *sta,
611 const u8 *addr,
612 u16 mac_id, u16 color)
613{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300614 struct iwl_mvm_add_sta_cmd cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100615 int ret;
616 u32 status;
617
618 lockdep_assert_held(&mvm->mutex);
619
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300620 memset(&cmd, 0, sizeof(cmd));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100621 cmd.sta_id = sta->sta_id;
622 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
623 color));
624
625 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
Liad Kaufmancf0cda12015-09-24 10:44:12 +0200626 cmd.tid_disable_tx = cpu_to_le16(0xffff);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100627
628 if (addr)
629 memcpy(cmd.addr, addr, ETH_ALEN);
630
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300631 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
632 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100633 if (ret)
634 return ret;
635
636 switch (status) {
637 case ADD_STA_SUCCESS:
638 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
639 return 0;
640 default:
641 ret = -EIO;
642 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
643 status);
644 break;
645 }
646 return ret;
647}
648
649int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
650{
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200651 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
652 mvm->cfg->base_params->wd_timeout :
653 IWL_WATCHDOG_DISABLED;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100654 int ret;
655
656 lockdep_assert_held(&mvm->mutex);
657
Ariej Marjieh7da91b02014-07-07 12:09:40 +0300658 /* Map Aux queue to fifo - needs to happen before adding Aux station */
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300659 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300660 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
Ariej Marjieh7da91b02014-07-07 12:09:40 +0300661
662 /* Allocate aux station and assign to it the aux queue */
663 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
Eliad Pellerb92e6612014-01-23 17:58:23 +0200664 NL80211_IFTYPE_UNSPECIFIED);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100665 if (ret)
666 return ret;
667
668 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
669 MAC_INDEX_AUX, 0);
670
671 if (ret)
672 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
673 return ret;
674}
675
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200676int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
677{
678 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
679
680 lockdep_assert_held(&mvm->mutex);
681 return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
682 mvmvif->id, 0);
683}
684
685int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
686{
687 int ret;
688
689 lockdep_assert_held(&mvm->mutex);
690
691 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
692 if (ret)
693 IWL_WARN(mvm, "Failed sending remove station\n");
694
695 return ret;
696}
697
698void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
699{
700 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
701}
702
Johannes Berg712b24a2014-08-04 14:14:14 +0200703void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
704{
705 lockdep_assert_held(&mvm->mutex);
706
707 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
708}
709
Johannes Berg8ca151b2013-01-24 14:25:36 +0100710/*
711 * Send the add station command for the vif's broadcast station.
712 * Assumes that the station was already allocated.
713 *
714 * @mvm: the mvm component
715 * @vif: the interface to which the broadcast station is added
716 * @bsta: the broadcast station to add.
717 */
Johannes Berg013290a2014-08-04 13:38:48 +0200718int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100719{
720 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +0200721 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +0200722 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +0100723 const u8 *baddr = _baddr;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100724
725 lockdep_assert_held(&mvm->mutex);
726
Johannes Berg5023d962013-07-31 14:07:43 +0200727 if (vif->type == NL80211_IFTYPE_ADHOC)
728 baddr = vif->bss_conf.bssid;
729
Johannes Berg8ca151b2013-01-24 14:25:36 +0100730 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
731 return -ENOSPC;
732
733 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
734 mvmvif->id, mvmvif->color);
735}
736
737/* Send the FW a request to remove the station from it's internal data
738 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +0200739int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100740{
Johannes Berg013290a2014-08-04 13:38:48 +0200741 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100742 int ret;
743
744 lockdep_assert_held(&mvm->mutex);
745
Johannes Berg013290a2014-08-04 13:38:48 +0200746 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100747 if (ret)
748 IWL_WARN(mvm, "Failed sending remove station\n");
749 return ret;
750}
751
Johannes Berg013290a2014-08-04 13:38:48 +0200752int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
753{
754 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
755 u32 qmask;
756
757 lockdep_assert_held(&mvm->mutex);
758
Arik Nemtsovd92b732e2014-09-21 19:00:42 +0300759 qmask = iwl_mvm_mac_get_queues_mask(vif);
Johannes Berg013290a2014-08-04 13:38:48 +0200760
761 /*
762 * The firmware defines the TFD queue mask to only be relevant
763 * for *unicast* queues, so the multicast (CAB) queue shouldn't
764 * be included.
765 */
766 if (vif->type == NL80211_IFTYPE_AP)
767 qmask &= ~BIT(vif->cab_queue);
768
769 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
770 ieee80211_vif_type_p2p(vif));
771}
772
Johannes Berg8ca151b2013-01-24 14:25:36 +0100773/* Allocate a new station entry for the broadcast station to the given vif,
774 * and send it to the FW.
775 * Note that each P2P mac should have its own broadcast station.
776 *
777 * @mvm: the mvm component
778 * @vif: the interface to which the broadcast station is added
779 * @bsta: the broadcast station to add. */
Johannes Berg013290a2014-08-04 13:38:48 +0200780int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100781{
782 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +0200783 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100784 int ret;
785
786 lockdep_assert_held(&mvm->mutex);
787
Johannes Berg013290a2014-08-04 13:38:48 +0200788 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100789 if (ret)
790 return ret;
791
Johannes Berg013290a2014-08-04 13:38:48 +0200792 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100793
794 if (ret)
795 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +0200796
Johannes Berg8ca151b2013-01-24 14:25:36 +0100797 return ret;
798}
799
Johannes Berg013290a2014-08-04 13:38:48 +0200800void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
801{
802 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
803
804 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
805}
806
Johannes Berg8ca151b2013-01-24 14:25:36 +0100807/*
808 * Send the FW a request to remove the station from it's internal data
809 * structures, and in addition remove it from the local data structure.
810 */
Johannes Berg013290a2014-08-04 13:38:48 +0200811int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100812{
813 int ret;
814
815 lockdep_assert_held(&mvm->mutex);
816
Johannes Berg013290a2014-08-04 13:38:48 +0200817 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100818
Johannes Berg013290a2014-08-04 13:38:48 +0200819 iwl_mvm_dealloc_bcast_sta(mvm, vif);
820
Johannes Berg8ca151b2013-01-24 14:25:36 +0100821 return ret;
822}
823
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300824#define IWL_MAX_RX_BA_SESSIONS 16
825
Johannes Berg8ca151b2013-01-24 14:25:36 +0100826int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
827 int tid, u16 ssn, bool start)
828{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100829 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300830 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100831 int ret;
832 u32 status;
833
834 lockdep_assert_held(&mvm->mutex);
835
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300836 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
837 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
838 return -ENOSPC;
839 }
840
Johannes Berg8ca151b2013-01-24 14:25:36 +0100841 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
842 cmd.sta_id = mvm_sta->sta_id;
843 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +0300844 if (start) {
845 cmd.add_immediate_ba_tid = (u8) tid;
846 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
847 } else {
848 cmd.remove_immediate_ba_tid = (u8) tid;
849 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100850 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
851 STA_MODIFY_REMOVE_BA_TID;
852
853 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300854 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
855 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100856 if (ret)
857 return ret;
858
859 switch (status) {
860 case ADD_STA_SUCCESS:
861 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
862 start ? "start" : "stopp");
863 break;
864 case ADD_STA_IMMEDIATE_BA_FAILURE:
865 IWL_WARN(mvm, "RX BA Session refused by fw\n");
866 ret = -ENOSPC;
867 break;
868 default:
869 ret = -EIO;
870 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
871 start ? "start" : "stopp", status);
872 break;
873 }
874
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300875 if (!ret) {
876 if (start)
877 mvm->rx_ba_sessions++;
878 else if (mvm->rx_ba_sessions > 0)
879 /* check that restart flow didn't zero the counter */
880 mvm->rx_ba_sessions--;
881 }
882
Johannes Berg8ca151b2013-01-24 14:25:36 +0100883 return ret;
884}
885
886static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
887 int tid, u8 queue, bool start)
888{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100889 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300890 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100891 int ret;
892 u32 status;
893
894 lockdep_assert_held(&mvm->mutex);
895
896 if (start) {
897 mvm_sta->tfd_queue_msk |= BIT(queue);
898 mvm_sta->tid_disable_agg &= ~BIT(tid);
899 } else {
900 mvm_sta->tfd_queue_msk &= ~BIT(queue);
901 mvm_sta->tid_disable_agg |= BIT(tid);
902 }
903
904 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
905 cmd.sta_id = mvm_sta->sta_id;
906 cmd.add_modify = STA_MODE_MODIFY;
907 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
908 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
909 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
910
911 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300912 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
913 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100914 if (ret)
915 return ret;
916
917 switch (status) {
918 case ADD_STA_SUCCESS:
919 break;
920 default:
921 ret = -EIO;
922 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
923 start ? "start" : "stopp", status);
924 break;
925 }
926
927 return ret;
928}
929
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +0200930const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100931 IEEE80211_AC_BE,
932 IEEE80211_AC_BK,
933 IEEE80211_AC_BK,
934 IEEE80211_AC_BE,
935 IEEE80211_AC_VI,
936 IEEE80211_AC_VI,
937 IEEE80211_AC_VO,
938 IEEE80211_AC_VO,
939};
940
Johannes Berg3e56ead2013-02-15 22:23:18 +0100941static const u8 tid_to_ucode_ac[] = {
942 AC_BE,
943 AC_BK,
944 AC_BK,
945 AC_BE,
946 AC_VI,
947 AC_VI,
948 AC_VO,
949 AC_VO,
950};
951
Johannes Berg8ca151b2013-01-24 14:25:36 +0100952int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
953 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
954{
Johannes Berg5b577a92013-11-14 18:20:04 +0100955 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100956 struct iwl_mvm_tid_data *tid_data;
957 int txq_id;
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300958 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100959
960 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
961 return -EINVAL;
962
963 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
964 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
965 mvmsta->tid_data[tid].state);
966 return -ENXIO;
967 }
968
969 lockdep_assert_held(&mvm->mutex);
970
Arik Nemtsovb2492502014-03-13 12:21:50 +0200971 spin_lock_bh(&mvmsta->lock);
972
973 /* possible race condition - we entered D0i3 while starting agg */
974 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
975 spin_unlock_bh(&mvmsta->lock);
976 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
977 return -EIO;
978 }
979
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300980 spin_lock_bh(&mvm->queue_info_lock);
981
982 txq_id = iwl_mvm_find_free_queue(mvm, mvm->first_agg_queue,
983 mvm->last_agg_queue);
984 if (txq_id < 0) {
985 ret = txq_id;
986 spin_unlock_bh(&mvm->queue_info_lock);
987 IWL_ERR(mvm, "Failed to allocate agg queue\n");
988 goto release_locks;
989 }
990 mvm->queue_info[txq_id].setup_reserved = true;
991 spin_unlock_bh(&mvm->queue_info_lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100992
Johannes Berg8ca151b2013-01-24 14:25:36 +0100993 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +0100994 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100995 tid_data->txq_id = txq_id;
996 *ssn = tid_data->ssn;
997
998 IWL_DEBUG_TX_QUEUES(mvm,
999 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
1000 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
1001 tid_data->next_reclaimed);
1002
1003 if (tid_data->ssn == tid_data->next_reclaimed) {
1004 tid_data->state = IWL_AGG_STARTING;
1005 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1006 } else {
1007 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
1008 }
1009
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001010 ret = 0;
1011
1012release_locks:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001013 spin_unlock_bh(&mvmsta->lock);
1014
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001015 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001016}
1017
1018int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1019 struct ieee80211_sta *sta, u16 tid, u8 buf_size)
1020{
Johannes Berg5b577a92013-11-14 18:20:04 +01001021 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001022 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02001023 unsigned int wdg_timeout =
1024 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001025 int queue, fifo, ret;
1026 u16 ssn;
1027
Eyal Shapiraefed6642014-09-14 15:58:53 +03001028 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
1029 != IWL_MAX_TID_COUNT);
1030
Johannes Berg8ca151b2013-01-24 14:25:36 +01001031 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
1032
1033 spin_lock_bh(&mvmsta->lock);
1034 ssn = tid_data->ssn;
1035 queue = tid_data->txq_id;
1036 tid_data->state = IWL_AGG_ON;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001037 mvmsta->agg_tids |= BIT(tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001038 tid_data->ssn = 0xffff;
1039 spin_unlock_bh(&mvmsta->lock);
1040
Johannes Berg3e56ead2013-02-15 22:23:18 +01001041 fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001042
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001043 iwl_mvm_enable_agg_txq(mvm, queue,
1044 vif->hw_queue[tid_to_mac80211_ac[tid]], fifo,
1045 mvmsta->sta_id, tid, buf_size, ssn, wdg_timeout);
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03001046
Johannes Berg8ca151b2013-01-24 14:25:36 +01001047 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1048 if (ret)
1049 return -EIO;
1050
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001051 /* No need to mark as reserved */
1052 spin_lock_bh(&mvm->queue_info_lock);
1053 mvm->queue_info[queue].setup_reserved = false;
1054 spin_unlock_bh(&mvm->queue_info_lock);
1055
Johannes Berg8ca151b2013-01-24 14:25:36 +01001056 /*
1057 * Even though in theory the peer could have different
1058 * aggregation reorder buffer sizes for different sessions,
1059 * our ucode doesn't allow for that and has a global limit
1060 * for each station. Therefore, use the minimum of all the
1061 * aggregation sessions and our default value.
1062 */
1063 mvmsta->max_agg_bufsize =
1064 min(mvmsta->max_agg_bufsize, buf_size);
1065 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
1066
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03001067 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
1068 sta->addr, tid);
1069
Eyal Shapira9e680942013-11-09 00:16:16 +02001070 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001071}
1072
1073int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1074 struct ieee80211_sta *sta, u16 tid)
1075{
Johannes Berg5b577a92013-11-14 18:20:04 +01001076 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001077 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1078 u16 txq_id;
1079 int err;
1080
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +02001081
1082 /*
1083 * If mac80211 is cleaning its state, then say that we finished since
1084 * our state has been cleared anyway.
1085 */
1086 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1087 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1088 return 0;
1089 }
1090
Johannes Berg8ca151b2013-01-24 14:25:36 +01001091 spin_lock_bh(&mvmsta->lock);
1092
1093 txq_id = tid_data->txq_id;
1094
1095 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
1096 mvmsta->sta_id, tid, txq_id, tid_data->state);
1097
Eyal Shapiraefed6642014-09-14 15:58:53 +03001098 mvmsta->agg_tids &= ~BIT(tid);
1099
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001100 /* No need to mark as reserved anymore */
1101 spin_lock_bh(&mvm->queue_info_lock);
1102 mvm->queue_info[txq_id].setup_reserved = false;
1103 spin_unlock_bh(&mvm->queue_info_lock);
1104
Johannes Berg8ca151b2013-01-24 14:25:36 +01001105 switch (tid_data->state) {
1106 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01001107 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001108
1109 IWL_DEBUG_TX_QUEUES(mvm,
1110 "ssn = %d, next_recl = %d\n",
1111 tid_data->ssn, tid_data->next_reclaimed);
1112
1113 /* There are still packets for this RA / TID in the HW */
1114 if (tid_data->ssn != tid_data->next_reclaimed) {
1115 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
1116 err = 0;
1117 break;
1118 }
1119
1120 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +02001121 tid_data->state = IWL_AGG_OFF;
Johannes Bergf7f89e72014-08-05 15:24:44 +02001122 spin_unlock_bh(&mvmsta->lock);
1123
1124 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1125
1126 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1127
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001128 iwl_mvm_disable_txq(mvm, txq_id,
1129 vif->hw_queue[tid_to_mac80211_ac[tid]], tid,
1130 0);
Johannes Bergf7f89e72014-08-05 15:24:44 +02001131 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001132 case IWL_AGG_STARTING:
1133 case IWL_EMPTYING_HW_QUEUE_ADDBA:
1134 /*
1135 * The agg session has been stopped before it was set up. This
1136 * can happen when the AddBA timer times out for example.
1137 */
1138
1139 /* No barriers since we are under mutex */
1140 lockdep_assert_held(&mvm->mutex);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001141
1142 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1143 tid_data->state = IWL_AGG_OFF;
1144 err = 0;
1145 break;
1146 default:
1147 IWL_ERR(mvm,
1148 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
1149 mvmsta->sta_id, tid, tid_data->state);
1150 IWL_ERR(mvm,
1151 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
1152 err = -EINVAL;
1153 }
1154
1155 spin_unlock_bh(&mvmsta->lock);
1156
1157 return err;
1158}
1159
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001160int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1161 struct ieee80211_sta *sta, u16 tid)
1162{
Johannes Berg5b577a92013-11-14 18:20:04 +01001163 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001164 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1165 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02001166 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001167
1168 /*
1169 * First set the agg state to OFF to avoid calling
1170 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
1171 */
1172 spin_lock_bh(&mvmsta->lock);
1173 txq_id = tid_data->txq_id;
1174 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
1175 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001176 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001177 tid_data->state = IWL_AGG_OFF;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001178 mvmsta->agg_tids &= ~BIT(tid);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001179 spin_unlock_bh(&mvmsta->lock);
1180
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001181 /* No need to mark as reserved */
1182 spin_lock_bh(&mvm->queue_info_lock);
1183 mvm->queue_info[txq_id].setup_reserved = false;
1184 spin_unlock_bh(&mvm->queue_info_lock);
1185
Johannes Bergb6658ff2013-07-24 13:55:51 +02001186 if (old_state >= IWL_AGG_ON) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001187 iwl_mvm_drain_sta(mvm, mvmsta, true);
Luca Coelho5888a402015-10-06 09:54:57 +03001188 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
Johannes Bergb6658ff2013-07-24 13:55:51 +02001189 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001190 iwl_trans_wait_tx_queue_empty(mvm->trans,
1191 mvmsta->tfd_queue_msk);
1192 iwl_mvm_drain_sta(mvm, mvmsta, false);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001193
Johannes Bergf7f89e72014-08-05 15:24:44 +02001194 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1195
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001196 iwl_mvm_disable_txq(mvm, tid_data->txq_id,
1197 vif->hw_queue[tid_to_mac80211_ac[tid]], tid,
1198 0);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001199 }
1200
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001201 return 0;
1202}
1203
Johannes Berg8ca151b2013-01-24 14:25:36 +01001204static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
1205{
Johannes Berg2dc2a152015-06-16 17:09:18 +02001206 int i, max = -1, max_offs = -1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001207
1208 lockdep_assert_held(&mvm->mutex);
1209
Johannes Berg2dc2a152015-06-16 17:09:18 +02001210 /* Pick the unused key offset with the highest 'deleted'
1211 * counter. Every time a key is deleted, all the counters
1212 * are incremented and the one that was just deleted is
1213 * reset to zero. Thus, the highest counter is the one
1214 * that was deleted longest ago. Pick that one.
1215 */
1216 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
1217 if (test_bit(i, mvm->fw_key_table))
1218 continue;
1219 if (mvm->fw_key_deleted[i] > max) {
1220 max = mvm->fw_key_deleted[i];
1221 max_offs = i;
1222 }
1223 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001224
Johannes Berg2dc2a152015-06-16 17:09:18 +02001225 if (max_offs < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001226 return STA_KEY_IDX_INVALID;
1227
Johannes Berg2dc2a152015-06-16 17:09:18 +02001228 return max_offs;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001229}
1230
Johannes Berg5f7a1842015-12-11 09:36:10 +01001231static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
1232 struct ieee80211_vif *vif,
1233 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001234{
Johannes Berg5b530e92014-12-23 16:00:17 +01001235 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001236
Johannes Berg5f7a1842015-12-11 09:36:10 +01001237 if (sta)
1238 return iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001239
1240 /*
1241 * The device expects GTKs for station interfaces to be
1242 * installed as GTKs for the AP station. If we have no
1243 * station ID, then use AP's station ID.
1244 */
1245 if (vif->type == NL80211_IFTYPE_STATION &&
Avri Altman9513c5e2015-10-19 16:29:11 +02001246 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1247 u8 sta_id = mvmvif->ap_sta_id;
1248
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02001249 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
1250 lockdep_is_held(&mvm->mutex));
Avri Altman9513c5e2015-10-19 16:29:11 +02001251 /*
1252 * It is possible that the 'sta' parameter is NULL,
1253 * for example when a GTK is removed - the sta_id will then
1254 * be the AP ID, and no station was passed by mac80211.
1255 */
1256 if (IS_ERR_OR_NULL(sta))
Johannes Berg5f7a1842015-12-11 09:36:10 +01001257 return NULL;
Avri Altman9513c5e2015-10-19 16:29:11 +02001258
Johannes Berg5f7a1842015-12-11 09:36:10 +01001259 return iwl_mvm_sta_from_mac80211(sta);
Avri Altman9513c5e2015-10-19 16:29:11 +02001260 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001261
Johannes Berg5f7a1842015-12-11 09:36:10 +01001262 return NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001263}
1264
1265static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
1266 struct iwl_mvm_sta *mvm_sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01001267 struct ieee80211_key_conf *keyconf, bool mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001268 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
1269 u8 key_offset)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001270{
Max Stepanov5a258aa2013-04-07 09:11:21 +03001271 struct iwl_mvm_add_sta_key_cmd cmd = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001272 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01001273 int ret;
1274 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001275 u16 keyidx;
1276 int i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001277 u8 sta_id = mvm_sta->sta_id;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001278
1279 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1280 STA_KEY_FLG_KEYID_MSK;
1281 key_flags = cpu_to_le16(keyidx);
1282 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
1283
1284 switch (keyconf->cipher) {
1285 case WLAN_CIPHER_SUITE_TKIP:
1286 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001287 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001288 for (i = 0; i < 5; i++)
Max Stepanov5a258aa2013-04-07 09:11:21 +03001289 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1290 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001291 break;
1292 case WLAN_CIPHER_SUITE_CCMP:
1293 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001294 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001295 break;
Johannes Bergba3943b2014-11-12 23:54:48 +01001296 case WLAN_CIPHER_SUITE_WEP104:
1297 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
John W. Linvilleaa0cb082015-01-12 16:18:11 -05001298 /* fall through */
Johannes Bergba3943b2014-11-12 23:54:48 +01001299 case WLAN_CIPHER_SUITE_WEP40:
1300 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
1301 memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
1302 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001303 default:
Max Stepanove36e5432013-08-27 19:56:13 +03001304 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
1305 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001306 }
1307
Johannes Bergba3943b2014-11-12 23:54:48 +01001308 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001309 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1310
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001311 cmd.key_offset = key_offset;
Max Stepanov5a258aa2013-04-07 09:11:21 +03001312 cmd.key_flags = key_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001313 cmd.sta_id = sta_id;
1314
1315 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001316 if (cmd_flags & CMD_ASYNC)
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001317 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
1318 sizeof(cmd), &cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001319 else
1320 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1321 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001322
1323 switch (status) {
1324 case ADD_STA_SUCCESS:
1325 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1326 break;
1327 default:
1328 ret = -EIO;
1329 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1330 break;
1331 }
1332
1333 return ret;
1334}
1335
1336static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1337 struct ieee80211_key_conf *keyconf,
1338 u8 sta_id, bool remove_key)
1339{
1340 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1341
1342 /* verify the key details match the required command's expectations */
1343 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1344 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1345 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1346 return -EINVAL;
1347
1348 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1349 igtk_cmd.sta_id = cpu_to_le32(sta_id);
1350
1351 if (remove_key) {
1352 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1353 } else {
1354 struct ieee80211_key_seq seq;
1355 const u8 *pn;
1356
1357 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001358 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1359 pn = seq.aes_cmac.pn;
1360 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1361 ((u64) pn[4] << 8) |
1362 ((u64) pn[3] << 16) |
1363 ((u64) pn[2] << 24) |
1364 ((u64) pn[1] << 32) |
1365 ((u64) pn[0] << 40));
1366 }
1367
1368 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1369 remove_key ? "removing" : "installing",
1370 igtk_cmd.sta_id);
1371
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001372 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001373 sizeof(igtk_cmd), &igtk_cmd);
1374}
1375
1376
1377static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1378 struct ieee80211_vif *vif,
1379 struct ieee80211_sta *sta)
1380{
Johannes Berg5b530e92014-12-23 16:00:17 +01001381 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001382
1383 if (sta)
1384 return sta->addr;
1385
1386 if (vif->type == NL80211_IFTYPE_STATION &&
1387 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1388 u8 sta_id = mvmvif->ap_sta_id;
1389 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1390 lockdep_is_held(&mvm->mutex));
1391 return sta->addr;
1392 }
1393
1394
1395 return NULL;
1396}
1397
Johannes Berg2f6319d2014-11-12 23:39:56 +01001398static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1399 struct ieee80211_vif *vif,
1400 struct ieee80211_sta *sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01001401 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001402 u8 key_offset,
Johannes Bergba3943b2014-11-12 23:54:48 +01001403 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001404{
Johannes Berg2f6319d2014-11-12 23:39:56 +01001405 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001406 int ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001407 const u8 *addr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001408 struct ieee80211_key_seq seq;
1409 u16 p1k[5];
1410
Johannes Berg8ca151b2013-01-24 14:25:36 +01001411 switch (keyconf->cipher) {
1412 case WLAN_CIPHER_SUITE_TKIP:
1413 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1414 /* get phase 1 key from mac80211 */
1415 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1416 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Bergba3943b2014-11-12 23:54:48 +01001417 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001418 seq.tkip.iv32, p1k, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001419 break;
1420 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergba3943b2014-11-12 23:54:48 +01001421 case WLAN_CIPHER_SUITE_WEP40:
1422 case WLAN_CIPHER_SUITE_WEP104:
1423 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001424 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001425 break;
1426 default:
Johannes Bergba3943b2014-11-12 23:54:48 +01001427 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001428 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001429 }
1430
Johannes Berg8ca151b2013-01-24 14:25:36 +01001431 return ret;
1432}
1433
Johannes Berg2f6319d2014-11-12 23:39:56 +01001434static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
Johannes Bergba3943b2014-11-12 23:54:48 +01001435 struct ieee80211_key_conf *keyconf,
1436 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001437{
Max Stepanov5a258aa2013-04-07 09:11:21 +03001438 struct iwl_mvm_add_sta_key_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001439 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01001440 int ret;
1441 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001442
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02001443 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1444 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001445 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1446 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1447
Johannes Bergba3943b2014-11-12 23:54:48 +01001448 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001449 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1450
Max Stepanov5a258aa2013-04-07 09:11:21 +03001451 cmd.key_flags = key_flags;
1452 cmd.key_offset = keyconf->hw_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001453 cmd.sta_id = sta_id;
1454
Johannes Berg8ca151b2013-01-24 14:25:36 +01001455 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001456 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1457 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001458
1459 switch (status) {
1460 case ADD_STA_SUCCESS:
1461 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1462 break;
1463 default:
1464 ret = -EIO;
1465 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1466 break;
1467 }
1468
1469 return ret;
1470}
1471
Johannes Berg2f6319d2014-11-12 23:39:56 +01001472int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1473 struct ieee80211_vif *vif,
1474 struct ieee80211_sta *sta,
1475 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001476 u8 key_offset)
Johannes Berg2f6319d2014-11-12 23:39:56 +01001477{
Johannes Bergba3943b2014-11-12 23:54:48 +01001478 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01001479 struct iwl_mvm_sta *mvm_sta;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001480 u8 sta_id;
1481 int ret;
Matti Gottlieb11828db2015-06-01 15:15:11 +03001482 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
Johannes Berg2f6319d2014-11-12 23:39:56 +01001483
1484 lockdep_assert_held(&mvm->mutex);
1485
1486 /* Get the station id from the mvm local station table */
Johannes Berg5f7a1842015-12-11 09:36:10 +01001487 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
1488 if (!mvm_sta) {
1489 IWL_ERR(mvm, "Failed to find station\n");
Johannes Berg2f6319d2014-11-12 23:39:56 +01001490 return -EINVAL;
1491 }
Johannes Berg5f7a1842015-12-11 09:36:10 +01001492 sta_id = mvm_sta->sta_id;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001493
1494 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1495 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1496 goto end;
1497 }
1498
1499 /*
1500 * It is possible that the 'sta' parameter is NULL, and thus
1501 * there is a need to retrieve the sta from the local station table.
1502 */
1503 if (!sta) {
1504 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1505 lockdep_is_held(&mvm->mutex));
1506 if (IS_ERR_OR_NULL(sta)) {
1507 IWL_ERR(mvm, "Invalid station id\n");
1508 return -EINVAL;
1509 }
1510 }
1511
1512 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
1513 return -EINVAL;
1514
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001515 /* If the key_offset is not pre-assigned, we need to find a
1516 * new offset to use. In normal cases, the offset is not
1517 * pre-assigned, but during HW_RESTART we want to reuse the
1518 * same indices, so we pass them when this function is called.
1519 *
1520 * In D3 entry, we need to hardcoded the indices (because the
1521 * firmware hardcodes the PTK offset to 0). In this case, we
1522 * need to make sure we don't overwrite the hw_key_idx in the
1523 * keyconf structure, because otherwise we cannot configure
1524 * the original ones back when resuming.
1525 */
1526 if (key_offset == STA_KEY_IDX_INVALID) {
1527 key_offset = iwl_mvm_set_fw_key_idx(mvm);
1528 if (key_offset == STA_KEY_IDX_INVALID)
Johannes Berg2f6319d2014-11-12 23:39:56 +01001529 return -ENOSPC;
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001530 keyconf->hw_key_idx = key_offset;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001531 }
1532
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001533 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02001534 if (ret)
Johannes Bergba3943b2014-11-12 23:54:48 +01001535 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01001536
1537 /*
1538 * For WEP, the same key is used for multicast and unicast. Upload it
1539 * again, using the same key offset, and now pointing the other one
1540 * to the same key slot (offset).
1541 * If this fails, remove the original as well.
1542 */
1543 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
1544 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001545 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
1546 key_offset, !mcast);
Johannes Bergba3943b2014-11-12 23:54:48 +01001547 if (ret) {
Johannes Bergba3943b2014-11-12 23:54:48 +01001548 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02001549 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01001550 }
1551 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01001552
Luca Coelho9c3deeb2015-11-11 01:06:17 +02001553 __set_bit(key_offset, mvm->fw_key_table);
1554
Johannes Berg2f6319d2014-11-12 23:39:56 +01001555end:
1556 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1557 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Matti Gottlieb11828db2015-06-01 15:15:11 +03001558 sta ? sta->addr : zero_addr, ret);
Johannes Berg2f6319d2014-11-12 23:39:56 +01001559 return ret;
1560}
1561
1562int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1563 struct ieee80211_vif *vif,
1564 struct ieee80211_sta *sta,
1565 struct ieee80211_key_conf *keyconf)
1566{
Johannes Bergba3943b2014-11-12 23:54:48 +01001567 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01001568 struct iwl_mvm_sta *mvm_sta;
1569 u8 sta_id = IWL_MVM_STATION_COUNT;
Johannes Berg2dc2a152015-06-16 17:09:18 +02001570 int ret, i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001571
1572 lockdep_assert_held(&mvm->mutex);
1573
Johannes Berg5f7a1842015-12-11 09:36:10 +01001574 /* Get the station from the mvm local station table */
1575 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
Johannes Berg2f6319d2014-11-12 23:39:56 +01001576
1577 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1578 keyconf->keyidx, sta_id);
1579
1580 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1581 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1582
1583 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
1584 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1585 keyconf->hw_key_idx);
1586 return -ENOENT;
1587 }
1588
Johannes Berg2dc2a152015-06-16 17:09:18 +02001589 /* track which key was deleted last */
1590 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
1591 if (mvm->fw_key_deleted[i] < U8_MAX)
1592 mvm->fw_key_deleted[i]++;
1593 }
1594 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
1595
Johannes Berg5f7a1842015-12-11 09:36:10 +01001596 if (!mvm_sta) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01001597 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1598 return 0;
1599 }
1600
Johannes Berg5f7a1842015-12-11 09:36:10 +01001601 sta_id = mvm_sta->sta_id;
1602
Johannes Bergba3943b2014-11-12 23:54:48 +01001603 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
1604 if (ret)
1605 return ret;
1606
1607 /* delete WEP key twice to get rid of (now useless) offset */
1608 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
1609 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
1610 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
1611
1612 return ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001613}
1614
Johannes Berg8ca151b2013-01-24 14:25:36 +01001615void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1616 struct ieee80211_vif *vif,
1617 struct ieee80211_key_conf *keyconf,
1618 struct ieee80211_sta *sta, u32 iv32,
1619 u16 *phase1key)
1620{
Beni Levc3eb5362013-02-06 17:22:18 +02001621 struct iwl_mvm_sta *mvm_sta;
Johannes Bergba3943b2014-11-12 23:54:48 +01001622 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001623
Beni Levc3eb5362013-02-06 17:22:18 +02001624 rcu_read_lock();
1625
Johannes Berg5f7a1842015-12-11 09:36:10 +01001626 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
1627 if (WARN_ON_ONCE(!mvm_sta))
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02001628 goto unlock;
Johannes Bergba3943b2014-11-12 23:54:48 +01001629 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001630 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02001631
1632 unlock:
Beni Levc3eb5362013-02-06 17:22:18 +02001633 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01001634}
1635
Johannes Berg9cc40712013-02-15 22:47:48 +01001636void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
1637 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001638{
Johannes Berg5b577a92013-11-14 18:20:04 +01001639 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001640 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001641 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01001642 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03001643 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01001644 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01001645 };
1646 int ret;
1647
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001648 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001649 if (ret)
1650 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1651}
1652
Johannes Berg9cc40712013-02-15 22:47:48 +01001653void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
1654 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001655 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01001656 u16 cnt, u16 tids, bool more_data,
1657 bool agg)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001658{
Johannes Berg5b577a92013-11-14 18:20:04 +01001659 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001660 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001661 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01001662 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001663 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1664 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01001665 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01001666 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01001667 int tid, ret;
1668 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001669
Johannes Berg3e56ead2013-02-15 22:23:18 +01001670 /* convert TIDs to ACs - we don't support TSPEC so that's OK
1671 * Note that this field is reserved and unused by firmware not
1672 * supporting GO uAPSD, so it's safe to always do this.
1673 */
1674 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
1675 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
1676
1677 /* If we're releasing frames from aggregation queues then check if the
1678 * all queues combined that we're releasing frames from have
1679 * - more frames than the service period, in which case more_data
1680 * needs to be set
1681 * - fewer than 'cnt' frames, in which case we need to adjust the
1682 * firmware command (but do that unconditionally)
1683 */
1684 if (agg) {
1685 int remaining = cnt;
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02001686 int sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01001687
1688 spin_lock_bh(&mvmsta->lock);
1689 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
1690 struct iwl_mvm_tid_data *tid_data;
1691 u16 n_queued;
1692
1693 tid_data = &mvmsta->tid_data[tid];
1694 if (WARN(tid_data->state != IWL_AGG_ON &&
1695 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
1696 "TID %d state is %d\n",
1697 tid, tid_data->state)) {
1698 spin_unlock_bh(&mvmsta->lock);
1699 ieee80211_sta_eosp(sta);
1700 return;
1701 }
1702
1703 n_queued = iwl_mvm_tid_queued(tid_data);
1704 if (n_queued > remaining) {
1705 more_data = true;
1706 remaining = 0;
1707 break;
1708 }
1709 remaining -= n_queued;
1710 }
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02001711 sleep_tx_count = cnt - remaining;
1712 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1713 mvmsta->sleep_tx_count = sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01001714 spin_unlock_bh(&mvmsta->lock);
1715
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02001716 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
Johannes Berg3e56ead2013-02-15 22:23:18 +01001717 if (WARN_ON(cnt - remaining == 0)) {
1718 ieee80211_sta_eosp(sta);
1719 return;
1720 }
1721 }
1722
1723 /* Note: this is ignored by firmware not supporting GO uAPSD */
1724 if (more_data)
1725 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
1726
1727 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
1728 mvmsta->next_status_eosp = true;
1729 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
1730 } else {
1731 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
1732 }
1733
Emmanuel Grumbach156f92f2015-11-24 14:55:18 +02001734 /* block the Tx queues until the FW updated the sleep Tx count */
1735 iwl_trans_block_txq_ptrs(mvm->trans, true);
1736
1737 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
1738 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
1739 sizeof(cmd), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001740 if (ret)
1741 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1742}
Johannes Berg3e56ead2013-02-15 22:23:18 +01001743
Johannes Berg04168412015-06-23 21:22:09 +02001744void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
1745 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg3e56ead2013-02-15 22:23:18 +01001746{
1747 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1748 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
1749 struct ieee80211_sta *sta;
1750 u32 sta_id = le32_to_cpu(notif->sta_id);
1751
1752 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +02001753 return;
Johannes Berg3e56ead2013-02-15 22:23:18 +01001754
1755 rcu_read_lock();
1756 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1757 if (!IS_ERR_OR_NULL(sta))
1758 ieee80211_sta_eosp(sta);
1759 rcu_read_unlock();
Johannes Berg3e56ead2013-02-15 22:23:18 +01001760}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03001761
1762void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
1763 struct iwl_mvm_sta *mvmsta, bool disable)
1764{
1765 struct iwl_mvm_add_sta_cmd cmd = {
1766 .add_modify = STA_MODE_MODIFY,
1767 .sta_id = mvmsta->sta_id,
1768 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
1769 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
1770 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1771 };
1772 int ret;
1773
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03001774 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1775 if (ret)
1776 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1777}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001778
1779void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
1780 struct ieee80211_sta *sta,
1781 bool disable)
1782{
1783 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1784
1785 spin_lock_bh(&mvm_sta->lock);
1786
1787 if (mvm_sta->disable_tx == disable) {
1788 spin_unlock_bh(&mvm_sta->lock);
1789 return;
1790 }
1791
1792 mvm_sta->disable_tx = disable;
1793
1794 /*
Sara Sharon0d365ae2015-03-31 12:24:05 +03001795 * Tell mac80211 to start/stop queuing tx for this station,
1796 * but don't stop queuing if there are still pending frames
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001797 * for this station.
1798 */
1799 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
1800 ieee80211_sta_block_awake(mvm->hw, sta, disable);
1801
1802 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
1803
1804 spin_unlock_bh(&mvm_sta->lock);
1805}
1806
1807void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
1808 struct iwl_mvm_vif *mvmvif,
1809 bool disable)
1810{
1811 struct ieee80211_sta *sta;
1812 struct iwl_mvm_sta *mvm_sta;
1813 int i;
1814
1815 lockdep_assert_held(&mvm->mutex);
1816
1817 /* Block/unblock all the stations of the given mvmvif */
1818 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
1819 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
1820 lockdep_is_held(&mvm->mutex));
1821 if (IS_ERR_OR_NULL(sta))
1822 continue;
1823
1824 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1825 if (mvm_sta->mac_id_n_color !=
1826 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
1827 continue;
1828
1829 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
1830 }
1831}
Luciano Coelhodc88b4b2014-11-10 11:10:14 +02001832
1833void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1834{
1835 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1836 struct iwl_mvm_sta *mvmsta;
1837
1838 rcu_read_lock();
1839
1840 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
1841
1842 if (!WARN_ON(!mvmsta))
1843 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
1844
1845 rcu_read_unlock();
1846}