blob: 354acbde088e9e6fad0927a9ef73787ab235558c [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],
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300237 mvmsta->hw_queue[ac],
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300238 iwl_mvm_ac_to_tx_fifo[ac], 0,
239 wdg_timeout);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300240 mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
241 }
242
243 return 0;
244}
245
246static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
247 struct ieee80211_sta *sta)
248{
249 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
250 unsigned long sta_msk;
251 int i;
252
253 lockdep_assert_held(&mvm->mutex);
254
255 /* disable the TDLS STA-specific queues */
256 sta_msk = mvmsta->tfd_queue_msk;
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +0200257 for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +0300258 iwl_mvm_disable_txq(mvm, i, i, IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300259}
260
Johannes Berg8ca151b2013-01-24 14:25:36 +0100261int iwl_mvm_add_sta(struct iwl_mvm *mvm,
262 struct ieee80211_vif *vif,
263 struct ieee80211_sta *sta)
264{
265 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100266 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100267 int i, ret, sta_id;
268
269 lockdep_assert_held(&mvm->mutex);
270
271 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
Eliad Pellerb92e6612014-01-23 17:58:23 +0200272 sta_id = iwl_mvm_find_free_sta_id(mvm,
273 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100274 else
275 sta_id = mvm_sta->sta_id;
276
Johannes Berg36f46312015-03-10 20:32:08 +0100277 if (sta_id == IWL_MVM_STATION_COUNT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100278 return -ENOSPC;
279
Gregory Greenman94939082015-08-24 14:38:35 +0300280 if (vif->type == NL80211_IFTYPE_AP) {
281 mvmvif->ap_assoc_sta_count++;
Ilan Peerf82c8332015-09-10 12:54:38 +0300282 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
Gregory Greenman94939082015-08-24 14:38:35 +0300283 }
284
Johannes Berg8ca151b2013-01-24 14:25:36 +0100285 spin_lock_init(&mvm_sta->lock);
286
287 mvm_sta->sta_id = sta_id;
288 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
289 mvmvif->color);
290 mvm_sta->vif = vif;
291 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300292 mvm_sta->tx_protection = 0;
293 mvm_sta->tt_tx_protection = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100294
295 /* HW restart, don't assume the memory has been zeroed */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300296 atomic_set(&mvm->pending_frames[sta_id], 0);
Liad Kaufman69191af2015-09-01 18:50:22 +0300297 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100298 mvm_sta->tfd_queue_msk = 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300299
300 /* allocate new queues for a TDLS station */
301 if (sta->tdls) {
302 ret = iwl_mvm_tdls_sta_init(mvm, sta);
303 if (ret)
304 return ret;
305 } else {
306 for (i = 0; i < IEEE80211_NUM_ACS; i++)
307 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
308 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
309 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100310
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200311 /* for HW restart - reset everything but the sequence number */
312 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
313 u16 seq = mvm_sta->tid_data[i].seq_number;
314 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
315 mvm_sta->tid_data[i].seq_number = seq;
316 }
Eyal Shapiraefed6642014-09-14 15:58:53 +0300317 mvm_sta->agg_tids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100318
Johannes Berg7a453972013-02-12 13:10:44 +0100319 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100320 if (ret)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300321 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100322
Johannes Berg9e848012014-08-04 14:33:42 +0200323 if (vif->type == NL80211_IFTYPE_STATION) {
324 if (!sta->tdls) {
325 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
326 mvmvif->ap_sta_id = sta_id;
327 } else {
328 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
329 }
330 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100331
332 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
333
334 return 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300335
336err:
337 iwl_mvm_tdls_sta_deinit(mvm, sta);
338 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100339}
340
Johannes Berg7a453972013-02-12 13:10:44 +0100341int iwl_mvm_update_sta(struct iwl_mvm *mvm,
342 struct ieee80211_vif *vif,
343 struct ieee80211_sta *sta)
344{
345 return iwl_mvm_sta_send_to_fw(mvm, sta, true);
346}
347
Johannes Berg8ca151b2013-01-24 14:25:36 +0100348int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
349 bool drain)
350{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300351 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100352 int ret;
353 u32 status;
354
355 lockdep_assert_held(&mvm->mutex);
356
357 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
358 cmd.sta_id = mvmsta->sta_id;
359 cmd.add_modify = STA_MODE_MODIFY;
360 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
361 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
362
363 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300364 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
365 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100366 if (ret)
367 return ret;
368
369 switch (status) {
370 case ADD_STA_SUCCESS:
371 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
372 mvmsta->sta_id);
373 break;
374 default:
375 ret = -EIO;
376 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
377 mvmsta->sta_id);
378 break;
379 }
380
381 return ret;
382}
383
384/*
385 * Remove a station from the FW table. Before sending the command to remove
386 * the station validate that the station is indeed known to the driver (sanity
387 * only).
388 */
389static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
390{
391 struct ieee80211_sta *sta;
392 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
393 .sta_id = sta_id,
394 };
395 int ret;
396
397 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
398 lockdep_is_held(&mvm->mutex));
399
400 /* Note: internal stations are marked as error values */
401 if (!sta) {
402 IWL_ERR(mvm, "Invalid station id\n");
403 return -EINVAL;
404 }
405
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300406 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100407 sizeof(rm_sta_cmd), &rm_sta_cmd);
408 if (ret) {
409 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
410 return ret;
411 }
412
413 return 0;
414}
415
416void iwl_mvm_sta_drained_wk(struct work_struct *wk)
417{
418 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
419 u8 sta_id;
420
421 /*
422 * The mutex is needed because of the SYNC cmd, but not only: if the
423 * work would run concurrently with iwl_mvm_rm_sta, it would run before
424 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
425 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
426 * that later.
427 */
428 mutex_lock(&mvm->mutex);
429
430 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
431 int ret;
432 struct ieee80211_sta *sta =
433 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
434 lockdep_is_held(&mvm->mutex));
435
Johannes Berg1ddbbb02013-12-04 22:39:17 +0100436 /*
437 * This station is in use or RCU-removed; the latter happens in
438 * managed mode, where mac80211 removes the station before we
439 * can remove it from firmware (we can only do that after the
440 * MAC is marked unassociated), and possibly while the deauth
441 * frame to disconnect from the AP is still queued. Then, the
442 * station pointer is -ENOENT when the last skb is reclaimed.
443 */
444 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100445 continue;
446
447 if (PTR_ERR(sta) == -EINVAL) {
448 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
449 sta_id);
450 continue;
451 }
452
453 if (!sta) {
454 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
455 sta_id);
456 continue;
457 }
458
459 WARN_ON(PTR_ERR(sta) != -EBUSY);
460 /* This station was removed and we waited until it got drained,
461 * we can now proceed and remove it.
462 */
463 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
464 if (ret) {
465 IWL_ERR(mvm,
466 "Couldn't remove sta %d after it was drained\n",
467 sta_id);
468 continue;
469 }
Monam Agarwalc531c772014-03-24 00:05:56 +0530470 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100471 clear_bit(sta_id, mvm->sta_drained);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300472
473 if (mvm->tfd_drained[sta_id]) {
474 unsigned long i, msk = mvm->tfd_drained[sta_id];
475
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +0200476 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +0300477 iwl_mvm_disable_txq(mvm, i, i,
478 IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300479
480 mvm->tfd_drained[sta_id] = 0;
481 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
482 sta_id, msk);
483 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100484 }
485
486 mutex_unlock(&mvm->mutex);
487}
488
489int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
490 struct ieee80211_vif *vif,
491 struct ieee80211_sta *sta)
492{
493 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100494 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100495 int ret;
496
497 lockdep_assert_held(&mvm->mutex);
498
499 if (vif->type == NL80211_IFTYPE_STATION &&
500 mvmvif->ap_sta_id == mvm_sta->sta_id) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +0200501 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
502 if (ret)
503 return ret;
Emmanuel Grumbach80d85652013-02-19 15:32:42 +0200504 /* flush its queues here since we are freeing mvm_sta */
Luca Coelho5888a402015-10-06 09:54:57 +0300505 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +0200506 if (ret)
507 return ret;
508 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
509 mvm_sta->tfd_queue_msk);
510 if (ret)
511 return ret;
512 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
Emmanuel Grumbach80d85652013-02-19 15:32:42 +0200513
Johannes Berg8ca151b2013-01-24 14:25:36 +0100514 /* if we are associated - we can't remove the AP STA now */
515 if (vif->bss_conf.assoc)
516 return ret;
517
518 /* unassoc - go ahead - remove the AP STA now */
519 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
Eliad Peller37577fe2013-12-05 17:19:39 +0200520
521 /* clear d0i3_ap_sta_id if no longer relevant */
522 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
523 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100524 }
525
526 /*
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300527 * This shouldn't happen - the TDLS channel switch should be canceled
528 * before the STA is removed.
529 */
530 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
531 mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
532 cancel_delayed_work(&mvm->tdls_cs.dwork);
533 }
534
535 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300536 * Make sure that the tx response code sees the station as -EBUSY and
537 * calls the drain worker.
538 */
539 spin_lock_bh(&mvm_sta->lock);
540 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +0100541 * There are frames pending on the AC queues for this station.
542 * We need to wait until all the frames are drained...
543 */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300544 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100545 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
546 ERR_PTR(-EBUSY));
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300547 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300548
549 /* disable TDLS sta queues on drain complete */
550 if (sta->tdls) {
551 mvm->tfd_drained[mvm_sta->sta_id] =
552 mvm_sta->tfd_queue_msk;
553 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
554 mvm_sta->sta_id);
555 }
556
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300557 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100558 } else {
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300559 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300560
561 if (sta->tdls)
562 iwl_mvm_tdls_sta_deinit(mvm, sta);
563
Johannes Berg8ca151b2013-01-24 14:25:36 +0100564 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
Monam Agarwalc531c772014-03-24 00:05:56 +0530565 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100566 }
567
568 return ret;
569}
570
571int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
572 struct ieee80211_vif *vif,
573 u8 sta_id)
574{
575 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
576
577 lockdep_assert_held(&mvm->mutex);
578
Monam Agarwalc531c772014-03-24 00:05:56 +0530579 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100580 return ret;
581}
582
Johannes Berg712b24a2014-08-04 14:14:14 +0200583static int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
584 struct iwl_mvm_int_sta *sta,
585 u32 qmask, enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100586{
587 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +0200588 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100589 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
590 return -ENOSPC;
591 }
592
593 sta->tfd_queue_msk = qmask;
594
595 /* put a non-NULL value so iterating over the stations won't stop */
596 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
597 return 0;
598}
599
Johannes Berg712b24a2014-08-04 14:14:14 +0200600static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
601 struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100602{
Monam Agarwalc531c772014-03-24 00:05:56 +0530603 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100604 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
605 sta->sta_id = IWL_MVM_STATION_COUNT;
606}
607
608static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
609 struct iwl_mvm_int_sta *sta,
610 const u8 *addr,
611 u16 mac_id, u16 color)
612{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300613 struct iwl_mvm_add_sta_cmd cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100614 int ret;
615 u32 status;
616
617 lockdep_assert_held(&mvm->mutex);
618
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300619 memset(&cmd, 0, sizeof(cmd));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100620 cmd.sta_id = sta->sta_id;
621 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
622 color));
623
624 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
625
626 if (addr)
627 memcpy(cmd.addr, addr, ETH_ALEN);
628
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300629 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
630 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100631 if (ret)
632 return ret;
633
634 switch (status) {
635 case ADD_STA_SUCCESS:
636 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
637 return 0;
638 default:
639 ret = -EIO;
640 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
641 status);
642 break;
643 }
644 return ret;
645}
646
647int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
648{
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200649 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
650 mvm->cfg->base_params->wd_timeout :
651 IWL_WATCHDOG_DISABLED;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100652 int ret;
653
654 lockdep_assert_held(&mvm->mutex);
655
Ariej Marjieh7da91b02014-07-07 12:09:40 +0300656 /* Map Aux queue to fifo - needs to happen before adding Aux station */
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300657 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300658 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
Ariej Marjieh7da91b02014-07-07 12:09:40 +0300659
660 /* Allocate aux station and assign to it the aux queue */
661 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
Eliad Pellerb92e6612014-01-23 17:58:23 +0200662 NL80211_IFTYPE_UNSPECIFIED);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100663 if (ret)
664 return ret;
665
666 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
667 MAC_INDEX_AUX, 0);
668
669 if (ret)
670 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
671 return ret;
672}
673
Johannes Berg712b24a2014-08-04 14:14:14 +0200674void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
675{
676 lockdep_assert_held(&mvm->mutex);
677
678 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
679}
680
Johannes Berg8ca151b2013-01-24 14:25:36 +0100681/*
682 * Send the add station command for the vif's broadcast station.
683 * Assumes that the station was already allocated.
684 *
685 * @mvm: the mvm component
686 * @vif: the interface to which the broadcast station is added
687 * @bsta: the broadcast station to add.
688 */
Johannes Berg013290a2014-08-04 13:38:48 +0200689int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100690{
691 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +0200692 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +0200693 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +0100694 const u8 *baddr = _baddr;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100695
696 lockdep_assert_held(&mvm->mutex);
697
Johannes Berg5023d962013-07-31 14:07:43 +0200698 if (vif->type == NL80211_IFTYPE_ADHOC)
699 baddr = vif->bss_conf.bssid;
700
Johannes Berg8ca151b2013-01-24 14:25:36 +0100701 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
702 return -ENOSPC;
703
704 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
705 mvmvif->id, mvmvif->color);
706}
707
708/* Send the FW a request to remove the station from it's internal data
709 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +0200710int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100711{
Johannes Berg013290a2014-08-04 13:38:48 +0200712 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100713 int ret;
714
715 lockdep_assert_held(&mvm->mutex);
716
Johannes Berg013290a2014-08-04 13:38:48 +0200717 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100718 if (ret)
719 IWL_WARN(mvm, "Failed sending remove station\n");
720 return ret;
721}
722
Johannes Berg013290a2014-08-04 13:38:48 +0200723int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
724{
725 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
726 u32 qmask;
727
728 lockdep_assert_held(&mvm->mutex);
729
Arik Nemtsovd92b732e2014-09-21 19:00:42 +0300730 qmask = iwl_mvm_mac_get_queues_mask(vif);
Johannes Berg013290a2014-08-04 13:38:48 +0200731
732 /*
733 * The firmware defines the TFD queue mask to only be relevant
734 * for *unicast* queues, so the multicast (CAB) queue shouldn't
735 * be included.
736 */
737 if (vif->type == NL80211_IFTYPE_AP)
738 qmask &= ~BIT(vif->cab_queue);
739
740 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
741 ieee80211_vif_type_p2p(vif));
742}
743
Johannes Berg8ca151b2013-01-24 14:25:36 +0100744/* Allocate a new station entry for the broadcast station to the given vif,
745 * and send it to the FW.
746 * Note that each P2P mac should have its own broadcast station.
747 *
748 * @mvm: the mvm component
749 * @vif: the interface to which the broadcast station is added
750 * @bsta: the broadcast station to add. */
Johannes Berg013290a2014-08-04 13:38:48 +0200751int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100752{
753 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +0200754 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100755 int ret;
756
757 lockdep_assert_held(&mvm->mutex);
758
Johannes Berg013290a2014-08-04 13:38:48 +0200759 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100760 if (ret)
761 return ret;
762
Johannes Berg013290a2014-08-04 13:38:48 +0200763 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100764
765 if (ret)
766 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +0200767
Johannes Berg8ca151b2013-01-24 14:25:36 +0100768 return ret;
769}
770
Johannes Berg013290a2014-08-04 13:38:48 +0200771void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
772{
773 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
774
775 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
776}
777
Johannes Berg8ca151b2013-01-24 14:25:36 +0100778/*
779 * Send the FW a request to remove the station from it's internal data
780 * structures, and in addition remove it from the local data structure.
781 */
Johannes Berg013290a2014-08-04 13:38:48 +0200782int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100783{
784 int ret;
785
786 lockdep_assert_held(&mvm->mutex);
787
Johannes Berg013290a2014-08-04 13:38:48 +0200788 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100789
Johannes Berg013290a2014-08-04 13:38:48 +0200790 iwl_mvm_dealloc_bcast_sta(mvm, vif);
791
Johannes Berg8ca151b2013-01-24 14:25:36 +0100792 return ret;
793}
794
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300795#define IWL_MAX_RX_BA_SESSIONS 16
796
Johannes Berg8ca151b2013-01-24 14:25:36 +0100797int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
798 int tid, u16 ssn, bool start)
799{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100800 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300801 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100802 int ret;
803 u32 status;
804
805 lockdep_assert_held(&mvm->mutex);
806
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300807 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
808 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
809 return -ENOSPC;
810 }
811
Johannes Berg8ca151b2013-01-24 14:25:36 +0100812 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
813 cmd.sta_id = mvm_sta->sta_id;
814 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +0300815 if (start) {
816 cmd.add_immediate_ba_tid = (u8) tid;
817 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
818 } else {
819 cmd.remove_immediate_ba_tid = (u8) tid;
820 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100821 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
822 STA_MODIFY_REMOVE_BA_TID;
823
824 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300825 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
826 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100827 if (ret)
828 return ret;
829
830 switch (status) {
831 case ADD_STA_SUCCESS:
832 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
833 start ? "start" : "stopp");
834 break;
835 case ADD_STA_IMMEDIATE_BA_FAILURE:
836 IWL_WARN(mvm, "RX BA Session refused by fw\n");
837 ret = -ENOSPC;
838 break;
839 default:
840 ret = -EIO;
841 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
842 start ? "start" : "stopp", status);
843 break;
844 }
845
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300846 if (!ret) {
847 if (start)
848 mvm->rx_ba_sessions++;
849 else if (mvm->rx_ba_sessions > 0)
850 /* check that restart flow didn't zero the counter */
851 mvm->rx_ba_sessions--;
852 }
853
Johannes Berg8ca151b2013-01-24 14:25:36 +0100854 return ret;
855}
856
857static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
858 int tid, u8 queue, bool start)
859{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100860 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300861 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100862 int ret;
863 u32 status;
864
865 lockdep_assert_held(&mvm->mutex);
866
867 if (start) {
868 mvm_sta->tfd_queue_msk |= BIT(queue);
869 mvm_sta->tid_disable_agg &= ~BIT(tid);
870 } else {
871 mvm_sta->tfd_queue_msk &= ~BIT(queue);
872 mvm_sta->tid_disable_agg |= BIT(tid);
873 }
874
875 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
876 cmd.sta_id = mvm_sta->sta_id;
877 cmd.add_modify = STA_MODE_MODIFY;
878 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
879 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
880 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
881
882 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300883 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
884 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100885 if (ret)
886 return ret;
887
888 switch (status) {
889 case ADD_STA_SUCCESS:
890 break;
891 default:
892 ret = -EIO;
893 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
894 start ? "start" : "stopp", status);
895 break;
896 }
897
898 return ret;
899}
900
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +0200901const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100902 IEEE80211_AC_BE,
903 IEEE80211_AC_BK,
904 IEEE80211_AC_BK,
905 IEEE80211_AC_BE,
906 IEEE80211_AC_VI,
907 IEEE80211_AC_VI,
908 IEEE80211_AC_VO,
909 IEEE80211_AC_VO,
910};
911
Johannes Berg3e56ead2013-02-15 22:23:18 +0100912static const u8 tid_to_ucode_ac[] = {
913 AC_BE,
914 AC_BK,
915 AC_BK,
916 AC_BE,
917 AC_VI,
918 AC_VI,
919 AC_VO,
920 AC_VO,
921};
922
Johannes Berg8ca151b2013-01-24 14:25:36 +0100923int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
924 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
925{
Johannes Berg5b577a92013-11-14 18:20:04 +0100926 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100927 struct iwl_mvm_tid_data *tid_data;
928 int txq_id;
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300929 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100930
931 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
932 return -EINVAL;
933
934 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
935 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
936 mvmsta->tid_data[tid].state);
937 return -ENXIO;
938 }
939
940 lockdep_assert_held(&mvm->mutex);
941
Arik Nemtsovb2492502014-03-13 12:21:50 +0200942 spin_lock_bh(&mvmsta->lock);
943
944 /* possible race condition - we entered D0i3 while starting agg */
945 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
946 spin_unlock_bh(&mvmsta->lock);
947 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
948 return -EIO;
949 }
950
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300951 spin_lock_bh(&mvm->queue_info_lock);
952
953 txq_id = iwl_mvm_find_free_queue(mvm, mvm->first_agg_queue,
954 mvm->last_agg_queue);
955 if (txq_id < 0) {
956 ret = txq_id;
957 spin_unlock_bh(&mvm->queue_info_lock);
958 IWL_ERR(mvm, "Failed to allocate agg queue\n");
959 goto release_locks;
960 }
961 mvm->queue_info[txq_id].setup_reserved = true;
962 spin_unlock_bh(&mvm->queue_info_lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100963
Johannes Berg8ca151b2013-01-24 14:25:36 +0100964 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +0100965 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100966 tid_data->txq_id = txq_id;
967 *ssn = tid_data->ssn;
968
969 IWL_DEBUG_TX_QUEUES(mvm,
970 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
971 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
972 tid_data->next_reclaimed);
973
974 if (tid_data->ssn == tid_data->next_reclaimed) {
975 tid_data->state = IWL_AGG_STARTING;
976 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
977 } else {
978 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
979 }
980
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300981 ret = 0;
982
983release_locks:
Johannes Berg8ca151b2013-01-24 14:25:36 +0100984 spin_unlock_bh(&mvmsta->lock);
985
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300986 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100987}
988
989int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
990 struct ieee80211_sta *sta, u16 tid, u8 buf_size)
991{
Johannes Berg5b577a92013-11-14 18:20:04 +0100992 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100993 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +0200994 unsigned int wdg_timeout =
995 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100996 int queue, fifo, ret;
997 u16 ssn;
998
Eyal Shapiraefed6642014-09-14 15:58:53 +0300999 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
1000 != IWL_MAX_TID_COUNT);
1001
Johannes Berg8ca151b2013-01-24 14:25:36 +01001002 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
1003
1004 spin_lock_bh(&mvmsta->lock);
1005 ssn = tid_data->ssn;
1006 queue = tid_data->txq_id;
1007 tid_data->state = IWL_AGG_ON;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001008 mvmsta->agg_tids |= BIT(tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001009 tid_data->ssn = 0xffff;
1010 spin_unlock_bh(&mvmsta->lock);
1011
Johannes Berg3e56ead2013-02-15 22:23:18 +01001012 fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001013
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001014 iwl_mvm_enable_agg_txq(mvm, queue,
1015 vif->hw_queue[tid_to_mac80211_ac[tid]], fifo,
1016 mvmsta->sta_id, tid, buf_size, ssn, wdg_timeout);
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03001017
Johannes Berg8ca151b2013-01-24 14:25:36 +01001018 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1019 if (ret)
1020 return -EIO;
1021
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001022 /* No need to mark as reserved */
1023 spin_lock_bh(&mvm->queue_info_lock);
1024 mvm->queue_info[queue].setup_reserved = false;
1025 spin_unlock_bh(&mvm->queue_info_lock);
1026
Johannes Berg8ca151b2013-01-24 14:25:36 +01001027 /*
1028 * Even though in theory the peer could have different
1029 * aggregation reorder buffer sizes for different sessions,
1030 * our ucode doesn't allow for that and has a global limit
1031 * for each station. Therefore, use the minimum of all the
1032 * aggregation sessions and our default value.
1033 */
1034 mvmsta->max_agg_bufsize =
1035 min(mvmsta->max_agg_bufsize, buf_size);
1036 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
1037
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03001038 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
1039 sta->addr, tid);
1040
Eyal Shapira9e680942013-11-09 00:16:16 +02001041 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001042}
1043
1044int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1045 struct ieee80211_sta *sta, u16 tid)
1046{
Johannes Berg5b577a92013-11-14 18:20:04 +01001047 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001048 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1049 u16 txq_id;
1050 int err;
1051
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +02001052
1053 /*
1054 * If mac80211 is cleaning its state, then say that we finished since
1055 * our state has been cleared anyway.
1056 */
1057 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1058 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1059 return 0;
1060 }
1061
Johannes Berg8ca151b2013-01-24 14:25:36 +01001062 spin_lock_bh(&mvmsta->lock);
1063
1064 txq_id = tid_data->txq_id;
1065
1066 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
1067 mvmsta->sta_id, tid, txq_id, tid_data->state);
1068
Eyal Shapiraefed6642014-09-14 15:58:53 +03001069 mvmsta->agg_tids &= ~BIT(tid);
1070
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001071 /* No need to mark as reserved anymore */
1072 spin_lock_bh(&mvm->queue_info_lock);
1073 mvm->queue_info[txq_id].setup_reserved = false;
1074 spin_unlock_bh(&mvm->queue_info_lock);
1075
Johannes Berg8ca151b2013-01-24 14:25:36 +01001076 switch (tid_data->state) {
1077 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01001078 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001079
1080 IWL_DEBUG_TX_QUEUES(mvm,
1081 "ssn = %d, next_recl = %d\n",
1082 tid_data->ssn, tid_data->next_reclaimed);
1083
1084 /* There are still packets for this RA / TID in the HW */
1085 if (tid_data->ssn != tid_data->next_reclaimed) {
1086 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
1087 err = 0;
1088 break;
1089 }
1090
1091 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +02001092 tid_data->state = IWL_AGG_OFF;
Johannes Bergf7f89e72014-08-05 15:24:44 +02001093 spin_unlock_bh(&mvmsta->lock);
1094
1095 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1096
1097 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1098
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001099 iwl_mvm_disable_txq(mvm, txq_id,
1100 vif->hw_queue[tid_to_mac80211_ac[tid]], tid,
1101 0);
Johannes Bergf7f89e72014-08-05 15:24:44 +02001102 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001103 case IWL_AGG_STARTING:
1104 case IWL_EMPTYING_HW_QUEUE_ADDBA:
1105 /*
1106 * The agg session has been stopped before it was set up. This
1107 * can happen when the AddBA timer times out for example.
1108 */
1109
1110 /* No barriers since we are under mutex */
1111 lockdep_assert_held(&mvm->mutex);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001112
1113 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1114 tid_data->state = IWL_AGG_OFF;
1115 err = 0;
1116 break;
1117 default:
1118 IWL_ERR(mvm,
1119 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
1120 mvmsta->sta_id, tid, tid_data->state);
1121 IWL_ERR(mvm,
1122 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
1123 err = -EINVAL;
1124 }
1125
1126 spin_unlock_bh(&mvmsta->lock);
1127
1128 return err;
1129}
1130
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001131int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1132 struct ieee80211_sta *sta, u16 tid)
1133{
Johannes Berg5b577a92013-11-14 18:20:04 +01001134 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001135 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1136 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02001137 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001138
1139 /*
1140 * First set the agg state to OFF to avoid calling
1141 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
1142 */
1143 spin_lock_bh(&mvmsta->lock);
1144 txq_id = tid_data->txq_id;
1145 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
1146 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001147 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001148 tid_data->state = IWL_AGG_OFF;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001149 mvmsta->agg_tids &= ~BIT(tid);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001150 spin_unlock_bh(&mvmsta->lock);
1151
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001152 /* No need to mark as reserved */
1153 spin_lock_bh(&mvm->queue_info_lock);
1154 mvm->queue_info[txq_id].setup_reserved = false;
1155 spin_unlock_bh(&mvm->queue_info_lock);
1156
Johannes Bergb6658ff2013-07-24 13:55:51 +02001157 if (old_state >= IWL_AGG_ON) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001158 iwl_mvm_drain_sta(mvm, mvmsta, true);
Luca Coelho5888a402015-10-06 09:54:57 +03001159 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
Johannes Bergb6658ff2013-07-24 13:55:51 +02001160 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001161 iwl_trans_wait_tx_queue_empty(mvm->trans,
1162 mvmsta->tfd_queue_msk);
1163 iwl_mvm_drain_sta(mvm, mvmsta, false);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001164
Johannes Bergf7f89e72014-08-05 15:24:44 +02001165 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1166
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001167 iwl_mvm_disable_txq(mvm, tid_data->txq_id,
1168 vif->hw_queue[tid_to_mac80211_ac[tid]], tid,
1169 0);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001170 }
1171
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001172 return 0;
1173}
1174
Johannes Berg8ca151b2013-01-24 14:25:36 +01001175static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
1176{
Johannes Berg2dc2a152015-06-16 17:09:18 +02001177 int i, max = -1, max_offs = -1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001178
1179 lockdep_assert_held(&mvm->mutex);
1180
Johannes Berg2dc2a152015-06-16 17:09:18 +02001181 /* Pick the unused key offset with the highest 'deleted'
1182 * counter. Every time a key is deleted, all the counters
1183 * are incremented and the one that was just deleted is
1184 * reset to zero. Thus, the highest counter is the one
1185 * that was deleted longest ago. Pick that one.
1186 */
1187 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
1188 if (test_bit(i, mvm->fw_key_table))
1189 continue;
1190 if (mvm->fw_key_deleted[i] > max) {
1191 max = mvm->fw_key_deleted[i];
1192 max_offs = i;
1193 }
1194 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001195
Johannes Berg2dc2a152015-06-16 17:09:18 +02001196 if (max_offs < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001197 return STA_KEY_IDX_INVALID;
1198
Johannes Berg2dc2a152015-06-16 17:09:18 +02001199 __set_bit(max_offs, mvm->fw_key_table);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001200
Johannes Berg2dc2a152015-06-16 17:09:18 +02001201 return max_offs;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001202}
1203
Avri Altman9513c5e2015-10-19 16:29:11 +02001204static u8 iwl_mvm_get_key_sta_id(struct iwl_mvm *mvm,
1205 struct ieee80211_vif *vif,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001206 struct ieee80211_sta *sta)
1207{
Johannes Berg5b530e92014-12-23 16:00:17 +01001208 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001209
1210 if (sta) {
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001211 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001212
1213 return mvm_sta->sta_id;
1214 }
1215
1216 /*
1217 * The device expects GTKs for station interfaces to be
1218 * installed as GTKs for the AP station. If we have no
1219 * station ID, then use AP's station ID.
1220 */
1221 if (vif->type == NL80211_IFTYPE_STATION &&
Avri Altman9513c5e2015-10-19 16:29:11 +02001222 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1223 u8 sta_id = mvmvif->ap_sta_id;
1224
1225 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1226 lockdep_is_held(&mvm->mutex));
1227 /*
1228 * It is possible that the 'sta' parameter is NULL,
1229 * for example when a GTK is removed - the sta_id will then
1230 * be the AP ID, and no station was passed by mac80211.
1231 */
1232 if (IS_ERR_OR_NULL(sta))
1233 return IWL_MVM_STATION_COUNT;
1234
1235 return sta_id;
1236 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001237
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001238 return IWL_MVM_STATION_COUNT;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001239}
1240
1241static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
1242 struct iwl_mvm_sta *mvm_sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01001243 struct ieee80211_key_conf *keyconf, bool mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001244 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
1245 u8 key_offset)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001246{
Max Stepanov5a258aa2013-04-07 09:11:21 +03001247 struct iwl_mvm_add_sta_key_cmd cmd = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001248 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01001249 int ret;
1250 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001251 u16 keyidx;
1252 int i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001253 u8 sta_id = mvm_sta->sta_id;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001254
1255 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1256 STA_KEY_FLG_KEYID_MSK;
1257 key_flags = cpu_to_le16(keyidx);
1258 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
1259
1260 switch (keyconf->cipher) {
1261 case WLAN_CIPHER_SUITE_TKIP:
1262 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001263 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001264 for (i = 0; i < 5; i++)
Max Stepanov5a258aa2013-04-07 09:11:21 +03001265 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1266 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001267 break;
1268 case WLAN_CIPHER_SUITE_CCMP:
1269 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001270 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001271 break;
Johannes Bergba3943b2014-11-12 23:54:48 +01001272 case WLAN_CIPHER_SUITE_WEP104:
1273 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
John W. Linvilleaa0cb082015-01-12 16:18:11 -05001274 /* fall through */
Johannes Bergba3943b2014-11-12 23:54:48 +01001275 case WLAN_CIPHER_SUITE_WEP40:
1276 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
1277 memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
1278 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001279 default:
Max Stepanove36e5432013-08-27 19:56:13 +03001280 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
1281 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001282 }
1283
Johannes Bergba3943b2014-11-12 23:54:48 +01001284 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001285 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1286
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001287 cmd.key_offset = key_offset;
Max Stepanov5a258aa2013-04-07 09:11:21 +03001288 cmd.key_flags = key_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001289 cmd.sta_id = sta_id;
1290
1291 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001292 if (cmd_flags & CMD_ASYNC)
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001293 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
1294 sizeof(cmd), &cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001295 else
1296 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1297 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001298
1299 switch (status) {
1300 case ADD_STA_SUCCESS:
1301 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1302 break;
1303 default:
1304 ret = -EIO;
1305 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1306 break;
1307 }
1308
1309 return ret;
1310}
1311
1312static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1313 struct ieee80211_key_conf *keyconf,
1314 u8 sta_id, bool remove_key)
1315{
1316 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1317
1318 /* verify the key details match the required command's expectations */
1319 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1320 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1321 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1322 return -EINVAL;
1323
1324 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1325 igtk_cmd.sta_id = cpu_to_le32(sta_id);
1326
1327 if (remove_key) {
1328 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1329 } else {
1330 struct ieee80211_key_seq seq;
1331 const u8 *pn;
1332
1333 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001334 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1335 pn = seq.aes_cmac.pn;
1336 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1337 ((u64) pn[4] << 8) |
1338 ((u64) pn[3] << 16) |
1339 ((u64) pn[2] << 24) |
1340 ((u64) pn[1] << 32) |
1341 ((u64) pn[0] << 40));
1342 }
1343
1344 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1345 remove_key ? "removing" : "installing",
1346 igtk_cmd.sta_id);
1347
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001348 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001349 sizeof(igtk_cmd), &igtk_cmd);
1350}
1351
1352
1353static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1354 struct ieee80211_vif *vif,
1355 struct ieee80211_sta *sta)
1356{
Johannes Berg5b530e92014-12-23 16:00:17 +01001357 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001358
1359 if (sta)
1360 return sta->addr;
1361
1362 if (vif->type == NL80211_IFTYPE_STATION &&
1363 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1364 u8 sta_id = mvmvif->ap_sta_id;
1365 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1366 lockdep_is_held(&mvm->mutex));
1367 return sta->addr;
1368 }
1369
1370
1371 return NULL;
1372}
1373
Johannes Berg2f6319d2014-11-12 23:39:56 +01001374static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1375 struct ieee80211_vif *vif,
1376 struct ieee80211_sta *sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01001377 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001378 u8 key_offset,
Johannes Bergba3943b2014-11-12 23:54:48 +01001379 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001380{
Johannes Berg2f6319d2014-11-12 23:39:56 +01001381 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001382 int ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001383 const u8 *addr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001384 struct ieee80211_key_seq seq;
1385 u16 p1k[5];
1386
Johannes Berg8ca151b2013-01-24 14:25:36 +01001387 switch (keyconf->cipher) {
1388 case WLAN_CIPHER_SUITE_TKIP:
1389 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1390 /* get phase 1 key from mac80211 */
1391 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1392 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Bergba3943b2014-11-12 23:54:48 +01001393 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001394 seq.tkip.iv32, p1k, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001395 break;
1396 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergba3943b2014-11-12 23:54:48 +01001397 case WLAN_CIPHER_SUITE_WEP40:
1398 case WLAN_CIPHER_SUITE_WEP104:
1399 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001400 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001401 break;
1402 default:
Johannes Bergba3943b2014-11-12 23:54:48 +01001403 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001404 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001405 }
1406
Johannes Berg8ca151b2013-01-24 14:25:36 +01001407 return ret;
1408}
1409
Johannes Berg2f6319d2014-11-12 23:39:56 +01001410static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
Johannes Bergba3943b2014-11-12 23:54:48 +01001411 struct ieee80211_key_conf *keyconf,
1412 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001413{
Max Stepanov5a258aa2013-04-07 09:11:21 +03001414 struct iwl_mvm_add_sta_key_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001415 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01001416 int ret;
1417 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001418
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02001419 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1420 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001421 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1422 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1423
Johannes Bergba3943b2014-11-12 23:54:48 +01001424 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001425 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1426
Max Stepanov5a258aa2013-04-07 09:11:21 +03001427 cmd.key_flags = key_flags;
1428 cmd.key_offset = keyconf->hw_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001429 cmd.sta_id = sta_id;
1430
Johannes Berg8ca151b2013-01-24 14:25:36 +01001431 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001432 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1433 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001434
1435 switch (status) {
1436 case ADD_STA_SUCCESS:
1437 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1438 break;
1439 default:
1440 ret = -EIO;
1441 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1442 break;
1443 }
1444
1445 return ret;
1446}
1447
Johannes Berg2f6319d2014-11-12 23:39:56 +01001448int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1449 struct ieee80211_vif *vif,
1450 struct ieee80211_sta *sta,
1451 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001452 u8 key_offset)
Johannes Berg2f6319d2014-11-12 23:39:56 +01001453{
Johannes Bergba3943b2014-11-12 23:54:48 +01001454 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg2f6319d2014-11-12 23:39:56 +01001455 u8 sta_id;
1456 int ret;
Matti Gottlieb11828db2015-06-01 15:15:11 +03001457 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
Johannes Berg2f6319d2014-11-12 23:39:56 +01001458
1459 lockdep_assert_held(&mvm->mutex);
1460
1461 /* Get the station id from the mvm local station table */
Avri Altman9513c5e2015-10-19 16:29:11 +02001462 sta_id = iwl_mvm_get_key_sta_id(mvm, vif, sta);
Johannes Berg2f6319d2014-11-12 23:39:56 +01001463 if (sta_id == IWL_MVM_STATION_COUNT) {
1464 IWL_ERR(mvm, "Failed to find station id\n");
1465 return -EINVAL;
1466 }
1467
1468 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1469 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1470 goto end;
1471 }
1472
1473 /*
1474 * It is possible that the 'sta' parameter is NULL, and thus
1475 * there is a need to retrieve the sta from the local station table.
1476 */
1477 if (!sta) {
1478 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1479 lockdep_is_held(&mvm->mutex));
1480 if (IS_ERR_OR_NULL(sta)) {
1481 IWL_ERR(mvm, "Invalid station id\n");
1482 return -EINVAL;
1483 }
1484 }
1485
1486 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
1487 return -EINVAL;
1488
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001489 /* If the key_offset is not pre-assigned, we need to find a
1490 * new offset to use. In normal cases, the offset is not
1491 * pre-assigned, but during HW_RESTART we want to reuse the
1492 * same indices, so we pass them when this function is called.
1493 *
1494 * In D3 entry, we need to hardcoded the indices (because the
1495 * firmware hardcodes the PTK offset to 0). In this case, we
1496 * need to make sure we don't overwrite the hw_key_idx in the
1497 * keyconf structure, because otherwise we cannot configure
1498 * the original ones back when resuming.
1499 */
1500 if (key_offset == STA_KEY_IDX_INVALID) {
1501 key_offset = iwl_mvm_set_fw_key_idx(mvm);
1502 if (key_offset == STA_KEY_IDX_INVALID)
Johannes Berg2f6319d2014-11-12 23:39:56 +01001503 return -ENOSPC;
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001504 keyconf->hw_key_idx = key_offset;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001505 }
1506
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001507 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
Johannes Bergba3943b2014-11-12 23:54:48 +01001508 if (ret) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01001509 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
Johannes Bergba3943b2014-11-12 23:54:48 +01001510 goto end;
1511 }
1512
1513 /*
1514 * For WEP, the same key is used for multicast and unicast. Upload it
1515 * again, using the same key offset, and now pointing the other one
1516 * to the same key slot (offset).
1517 * If this fails, remove the original as well.
1518 */
1519 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
1520 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001521 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
1522 key_offset, !mcast);
Johannes Bergba3943b2014-11-12 23:54:48 +01001523 if (ret) {
1524 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1525 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
1526 }
1527 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01001528
1529end:
1530 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1531 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Matti Gottlieb11828db2015-06-01 15:15:11 +03001532 sta ? sta->addr : zero_addr, ret);
Johannes Berg2f6319d2014-11-12 23:39:56 +01001533 return ret;
1534}
1535
1536int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1537 struct ieee80211_vif *vif,
1538 struct ieee80211_sta *sta,
1539 struct ieee80211_key_conf *keyconf)
1540{
Johannes Bergba3943b2014-11-12 23:54:48 +01001541 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg2f6319d2014-11-12 23:39:56 +01001542 u8 sta_id;
Johannes Berg2dc2a152015-06-16 17:09:18 +02001543 int ret, i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001544
1545 lockdep_assert_held(&mvm->mutex);
1546
1547 /* Get the station id from the mvm local station table */
Avri Altman9513c5e2015-10-19 16:29:11 +02001548 sta_id = iwl_mvm_get_key_sta_id(mvm, vif, sta);
Johannes Berg2f6319d2014-11-12 23:39:56 +01001549
1550 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1551 keyconf->keyidx, sta_id);
1552
1553 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1554 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1555
1556 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
1557 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1558 keyconf->hw_key_idx);
1559 return -ENOENT;
1560 }
1561
Johannes Berg2dc2a152015-06-16 17:09:18 +02001562 /* track which key was deleted last */
1563 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
1564 if (mvm->fw_key_deleted[i] < U8_MAX)
1565 mvm->fw_key_deleted[i]++;
1566 }
1567 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
1568
Johannes Berg2f6319d2014-11-12 23:39:56 +01001569 if (sta_id == IWL_MVM_STATION_COUNT) {
1570 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1571 return 0;
1572 }
1573
Johannes Bergba3943b2014-11-12 23:54:48 +01001574 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
1575 if (ret)
1576 return ret;
1577
1578 /* delete WEP key twice to get rid of (now useless) offset */
1579 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
1580 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
1581 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
1582
1583 return ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001584}
1585
Johannes Berg8ca151b2013-01-24 14:25:36 +01001586void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1587 struct ieee80211_vif *vif,
1588 struct ieee80211_key_conf *keyconf,
1589 struct ieee80211_sta *sta, u32 iv32,
1590 u16 *phase1key)
1591{
Beni Levc3eb5362013-02-06 17:22:18 +02001592 struct iwl_mvm_sta *mvm_sta;
Avri Altman9513c5e2015-10-19 16:29:11 +02001593 u8 sta_id = iwl_mvm_get_key_sta_id(mvm, vif, sta);
Johannes Bergba3943b2014-11-12 23:54:48 +01001594 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001595
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001596 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001597 return;
1598
Beni Levc3eb5362013-02-06 17:22:18 +02001599 rcu_read_lock();
1600
1601 if (!sta) {
1602 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1603 if (WARN_ON(IS_ERR_OR_NULL(sta))) {
1604 rcu_read_unlock();
1605 return;
1606 }
1607 }
1608
Johannes Berg2f6319d2014-11-12 23:39:56 +01001609 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Bergba3943b2014-11-12 23:54:48 +01001610 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001611 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
Beni Levc3eb5362013-02-06 17:22:18 +02001612 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01001613}
1614
Johannes Berg9cc40712013-02-15 22:47:48 +01001615void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
1616 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001617{
Johannes Berg5b577a92013-11-14 18:20:04 +01001618 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001619 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001620 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01001621 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03001622 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01001623 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01001624 };
1625 int ret;
1626
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001627 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001628 if (ret)
1629 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1630}
1631
Johannes Berg9cc40712013-02-15 22:47:48 +01001632void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
1633 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001634 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01001635 u16 cnt, u16 tids, bool more_data,
1636 bool agg)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001637{
Johannes Berg5b577a92013-11-14 18:20:04 +01001638 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001639 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001640 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01001641 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001642 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1643 .sleep_tx_count = cpu_to_le16(cnt),
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 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01001646 int tid, ret;
1647 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001648
Johannes Berg3e56ead2013-02-15 22:23:18 +01001649 /* convert TIDs to ACs - we don't support TSPEC so that's OK
1650 * Note that this field is reserved and unused by firmware not
1651 * supporting GO uAPSD, so it's safe to always do this.
1652 */
1653 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
1654 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
1655
1656 /* If we're releasing frames from aggregation queues then check if the
1657 * all queues combined that we're releasing frames from have
1658 * - more frames than the service period, in which case more_data
1659 * needs to be set
1660 * - fewer than 'cnt' frames, in which case we need to adjust the
1661 * firmware command (but do that unconditionally)
1662 */
1663 if (agg) {
1664 int remaining = cnt;
1665
1666 spin_lock_bh(&mvmsta->lock);
1667 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
1668 struct iwl_mvm_tid_data *tid_data;
1669 u16 n_queued;
1670
1671 tid_data = &mvmsta->tid_data[tid];
1672 if (WARN(tid_data->state != IWL_AGG_ON &&
1673 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
1674 "TID %d state is %d\n",
1675 tid, tid_data->state)) {
1676 spin_unlock_bh(&mvmsta->lock);
1677 ieee80211_sta_eosp(sta);
1678 return;
1679 }
1680
1681 n_queued = iwl_mvm_tid_queued(tid_data);
1682 if (n_queued > remaining) {
1683 more_data = true;
1684 remaining = 0;
1685 break;
1686 }
1687 remaining -= n_queued;
1688 }
1689 spin_unlock_bh(&mvmsta->lock);
1690
1691 cmd.sleep_tx_count = cpu_to_le16(cnt - remaining);
1692 if (WARN_ON(cnt - remaining == 0)) {
1693 ieee80211_sta_eosp(sta);
1694 return;
1695 }
1696 }
1697
1698 /* Note: this is ignored by firmware not supporting GO uAPSD */
1699 if (more_data)
1700 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
1701
1702 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
1703 mvmsta->next_status_eosp = true;
1704 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
1705 } else {
1706 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
1707 }
1708
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001709 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001710 if (ret)
1711 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1712}
Johannes Berg3e56ead2013-02-15 22:23:18 +01001713
Johannes Berg04168412015-06-23 21:22:09 +02001714void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
1715 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg3e56ead2013-02-15 22:23:18 +01001716{
1717 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1718 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
1719 struct ieee80211_sta *sta;
1720 u32 sta_id = le32_to_cpu(notif->sta_id);
1721
1722 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +02001723 return;
Johannes Berg3e56ead2013-02-15 22:23:18 +01001724
1725 rcu_read_lock();
1726 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1727 if (!IS_ERR_OR_NULL(sta))
1728 ieee80211_sta_eosp(sta);
1729 rcu_read_unlock();
Johannes Berg3e56ead2013-02-15 22:23:18 +01001730}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03001731
1732void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
1733 struct iwl_mvm_sta *mvmsta, bool disable)
1734{
1735 struct iwl_mvm_add_sta_cmd cmd = {
1736 .add_modify = STA_MODE_MODIFY,
1737 .sta_id = mvmsta->sta_id,
1738 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
1739 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
1740 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1741 };
1742 int ret;
1743
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03001744 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1745 if (ret)
1746 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1747}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001748
1749void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
1750 struct ieee80211_sta *sta,
1751 bool disable)
1752{
1753 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1754
1755 spin_lock_bh(&mvm_sta->lock);
1756
1757 if (mvm_sta->disable_tx == disable) {
1758 spin_unlock_bh(&mvm_sta->lock);
1759 return;
1760 }
1761
1762 mvm_sta->disable_tx = disable;
1763
1764 /*
Sara Sharon0d365ae2015-03-31 12:24:05 +03001765 * Tell mac80211 to start/stop queuing tx for this station,
1766 * but don't stop queuing if there are still pending frames
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001767 * for this station.
1768 */
1769 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
1770 ieee80211_sta_block_awake(mvm->hw, sta, disable);
1771
1772 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
1773
1774 spin_unlock_bh(&mvm_sta->lock);
1775}
1776
1777void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
1778 struct iwl_mvm_vif *mvmvif,
1779 bool disable)
1780{
1781 struct ieee80211_sta *sta;
1782 struct iwl_mvm_sta *mvm_sta;
1783 int i;
1784
1785 lockdep_assert_held(&mvm->mutex);
1786
1787 /* Block/unblock all the stations of the given mvmvif */
1788 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
1789 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
1790 lockdep_is_held(&mvm->mutex));
1791 if (IS_ERR_OR_NULL(sta))
1792 continue;
1793
1794 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1795 if (mvm_sta->mac_id_n_color !=
1796 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
1797 continue;
1798
1799 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
1800 }
1801}
Luciano Coelhodc88b4b2014-11-10 11:10:14 +02001802
1803void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1804{
1805 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1806 struct iwl_mvm_sta *mvmsta;
1807
1808 rcu_read_lock();
1809
1810 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
1811
1812 if (!WARN_ON(!mvmsta))
1813 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
1814
1815 rcu_read_unlock();
1816}