blob: 9bda6c99eff219036d1c743b585b887e3571a9eb [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],
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200238 iwl_mvm_ac_to_tx_fifo[ac], wdg_timeout);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300239 mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
240 }
241
242 return 0;
243}
244
245static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
246 struct ieee80211_sta *sta)
247{
248 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
249 unsigned long sta_msk;
250 int i;
251
252 lockdep_assert_held(&mvm->mutex);
253
254 /* disable the TDLS STA-specific queues */
255 sta_msk = mvmsta->tfd_queue_msk;
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +0200256 for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300257 iwl_mvm_disable_txq(mvm, i, i, 0, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300258}
259
Johannes Berg8ca151b2013-01-24 14:25:36 +0100260int iwl_mvm_add_sta(struct iwl_mvm *mvm,
261 struct ieee80211_vif *vif,
262 struct ieee80211_sta *sta)
263{
264 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100265 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100266 int i, ret, sta_id;
267
268 lockdep_assert_held(&mvm->mutex);
269
270 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
Eliad Pellerb92e6612014-01-23 17:58:23 +0200271 sta_id = iwl_mvm_find_free_sta_id(mvm,
272 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100273 else
274 sta_id = mvm_sta->sta_id;
275
Johannes Berg36f46312015-03-10 20:32:08 +0100276 if (sta_id == IWL_MVM_STATION_COUNT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100277 return -ENOSPC;
278
Gregory Greenman94939082015-08-24 14:38:35 +0300279 if (vif->type == NL80211_IFTYPE_AP) {
280 mvmvif->ap_assoc_sta_count++;
Ilan Peerf82c8332015-09-10 12:54:38 +0300281 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
Gregory Greenman94939082015-08-24 14:38:35 +0300282 }
283
Johannes Berg8ca151b2013-01-24 14:25:36 +0100284 spin_lock_init(&mvm_sta->lock);
285
286 mvm_sta->sta_id = sta_id;
287 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
288 mvmvif->color);
289 mvm_sta->vif = vif;
290 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300291 mvm_sta->tx_protection = 0;
292 mvm_sta->tt_tx_protection = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100293
294 /* HW restart, don't assume the memory has been zeroed */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300295 atomic_set(&mvm->pending_frames[sta_id], 0);
Liad Kaufman69191af2015-09-01 18:50:22 +0300296 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100297 mvm_sta->tfd_queue_msk = 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300298
299 /* allocate new queues for a TDLS station */
300 if (sta->tdls) {
301 ret = iwl_mvm_tdls_sta_init(mvm, sta);
302 if (ret)
303 return ret;
304 } else {
305 for (i = 0; i < IEEE80211_NUM_ACS; i++)
306 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
307 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
308 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100309
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200310 /* for HW restart - reset everything but the sequence number */
311 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
312 u16 seq = mvm_sta->tid_data[i].seq_number;
313 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
314 mvm_sta->tid_data[i].seq_number = seq;
315 }
Eyal Shapiraefed6642014-09-14 15:58:53 +0300316 mvm_sta->agg_tids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100317
Johannes Berg7a453972013-02-12 13:10:44 +0100318 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100319 if (ret)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300320 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100321
Johannes Berg9e848012014-08-04 14:33:42 +0200322 if (vif->type == NL80211_IFTYPE_STATION) {
323 if (!sta->tdls) {
324 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
325 mvmvif->ap_sta_id = sta_id;
326 } else {
327 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
328 }
329 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100330
331 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
332
333 return 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300334
335err:
336 iwl_mvm_tdls_sta_deinit(mvm, sta);
337 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100338}
339
Johannes Berg7a453972013-02-12 13:10:44 +0100340int iwl_mvm_update_sta(struct iwl_mvm *mvm,
341 struct ieee80211_vif *vif,
342 struct ieee80211_sta *sta)
343{
344 return iwl_mvm_sta_send_to_fw(mvm, sta, true);
345}
346
Johannes Berg8ca151b2013-01-24 14:25:36 +0100347int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
348 bool drain)
349{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300350 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100351 int ret;
352 u32 status;
353
354 lockdep_assert_held(&mvm->mutex);
355
356 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
357 cmd.sta_id = mvmsta->sta_id;
358 cmd.add_modify = STA_MODE_MODIFY;
359 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
360 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
361
362 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300363 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
364 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100365 if (ret)
366 return ret;
367
368 switch (status) {
369 case ADD_STA_SUCCESS:
370 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
371 mvmsta->sta_id);
372 break;
373 default:
374 ret = -EIO;
375 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
376 mvmsta->sta_id);
377 break;
378 }
379
380 return ret;
381}
382
383/*
384 * Remove a station from the FW table. Before sending the command to remove
385 * the station validate that the station is indeed known to the driver (sanity
386 * only).
387 */
388static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
389{
390 struct ieee80211_sta *sta;
391 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
392 .sta_id = sta_id,
393 };
394 int ret;
395
396 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
397 lockdep_is_held(&mvm->mutex));
398
399 /* Note: internal stations are marked as error values */
400 if (!sta) {
401 IWL_ERR(mvm, "Invalid station id\n");
402 return -EINVAL;
403 }
404
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300405 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100406 sizeof(rm_sta_cmd), &rm_sta_cmd);
407 if (ret) {
408 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
409 return ret;
410 }
411
412 return 0;
413}
414
415void iwl_mvm_sta_drained_wk(struct work_struct *wk)
416{
417 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
418 u8 sta_id;
419
420 /*
421 * The mutex is needed because of the SYNC cmd, but not only: if the
422 * work would run concurrently with iwl_mvm_rm_sta, it would run before
423 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
424 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
425 * that later.
426 */
427 mutex_lock(&mvm->mutex);
428
429 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
430 int ret;
431 struct ieee80211_sta *sta =
432 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
433 lockdep_is_held(&mvm->mutex));
434
Johannes Berg1ddbbb02013-12-04 22:39:17 +0100435 /*
436 * This station is in use or RCU-removed; the latter happens in
437 * managed mode, where mac80211 removes the station before we
438 * can remove it from firmware (we can only do that after the
439 * MAC is marked unassociated), and possibly while the deauth
440 * frame to disconnect from the AP is still queued. Then, the
441 * station pointer is -ENOENT when the last skb is reclaimed.
442 */
443 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100444 continue;
445
446 if (PTR_ERR(sta) == -EINVAL) {
447 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
448 sta_id);
449 continue;
450 }
451
452 if (!sta) {
453 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
454 sta_id);
455 continue;
456 }
457
458 WARN_ON(PTR_ERR(sta) != -EBUSY);
459 /* This station was removed and we waited until it got drained,
460 * we can now proceed and remove it.
461 */
462 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
463 if (ret) {
464 IWL_ERR(mvm,
465 "Couldn't remove sta %d after it was drained\n",
466 sta_id);
467 continue;
468 }
Monam Agarwalc531c772014-03-24 00:05:56 +0530469 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100470 clear_bit(sta_id, mvm->sta_drained);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300471
472 if (mvm->tfd_drained[sta_id]) {
473 unsigned long i, msk = mvm->tfd_drained[sta_id];
474
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +0200475 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300476 iwl_mvm_disable_txq(mvm, i, i, 0, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300477
478 mvm->tfd_drained[sta_id] = 0;
479 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
480 sta_id, msk);
481 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100482 }
483
484 mutex_unlock(&mvm->mutex);
485}
486
487int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
488 struct ieee80211_vif *vif,
489 struct ieee80211_sta *sta)
490{
491 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100492 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100493 int ret;
494
495 lockdep_assert_held(&mvm->mutex);
496
497 if (vif->type == NL80211_IFTYPE_STATION &&
498 mvmvif->ap_sta_id == mvm_sta->sta_id) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +0200499 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
500 if (ret)
501 return ret;
Emmanuel Grumbach80d85652013-02-19 15:32:42 +0200502 /* flush its queues here since we are freeing mvm_sta */
503 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true);
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +0200504 if (ret)
505 return ret;
506 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
507 mvm_sta->tfd_queue_msk);
508 if (ret)
509 return ret;
510 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
Emmanuel Grumbach80d85652013-02-19 15:32:42 +0200511
Johannes Berg8ca151b2013-01-24 14:25:36 +0100512 /* if we are associated - we can't remove the AP STA now */
513 if (vif->bss_conf.assoc)
514 return ret;
515
516 /* unassoc - go ahead - remove the AP STA now */
517 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
Eliad Peller37577fe2013-12-05 17:19:39 +0200518
519 /* clear d0i3_ap_sta_id if no longer relevant */
520 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
521 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100522 }
523
524 /*
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300525 * This shouldn't happen - the TDLS channel switch should be canceled
526 * before the STA is removed.
527 */
528 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
529 mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
530 cancel_delayed_work(&mvm->tdls_cs.dwork);
531 }
532
533 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300534 * Make sure that the tx response code sees the station as -EBUSY and
535 * calls the drain worker.
536 */
537 spin_lock_bh(&mvm_sta->lock);
538 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +0100539 * There are frames pending on the AC queues for this station.
540 * We need to wait until all the frames are drained...
541 */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300542 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100543 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
544 ERR_PTR(-EBUSY));
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300545 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300546
547 /* disable TDLS sta queues on drain complete */
548 if (sta->tdls) {
549 mvm->tfd_drained[mvm_sta->sta_id] =
550 mvm_sta->tfd_queue_msk;
551 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
552 mvm_sta->sta_id);
553 }
554
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300555 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100556 } else {
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300557 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300558
559 if (sta->tdls)
560 iwl_mvm_tdls_sta_deinit(mvm, sta);
561
Johannes Berg8ca151b2013-01-24 14:25:36 +0100562 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
Monam Agarwalc531c772014-03-24 00:05:56 +0530563 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100564 }
565
566 return ret;
567}
568
569int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
570 struct ieee80211_vif *vif,
571 u8 sta_id)
572{
573 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
574
575 lockdep_assert_held(&mvm->mutex);
576
Monam Agarwalc531c772014-03-24 00:05:56 +0530577 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100578 return ret;
579}
580
Johannes Berg712b24a2014-08-04 14:14:14 +0200581static int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
582 struct iwl_mvm_int_sta *sta,
583 u32 qmask, enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100584{
585 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +0200586 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100587 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
588 return -ENOSPC;
589 }
590
591 sta->tfd_queue_msk = qmask;
592
593 /* put a non-NULL value so iterating over the stations won't stop */
594 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
595 return 0;
596}
597
Johannes Berg712b24a2014-08-04 14:14:14 +0200598static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
599 struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100600{
Monam Agarwalc531c772014-03-24 00:05:56 +0530601 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100602 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
603 sta->sta_id = IWL_MVM_STATION_COUNT;
604}
605
606static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
607 struct iwl_mvm_int_sta *sta,
608 const u8 *addr,
609 u16 mac_id, u16 color)
610{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300611 struct iwl_mvm_add_sta_cmd cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100612 int ret;
613 u32 status;
614
615 lockdep_assert_held(&mvm->mutex);
616
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300617 memset(&cmd, 0, sizeof(cmd));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100618 cmd.sta_id = sta->sta_id;
619 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
620 color));
621
622 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
623
624 if (addr)
625 memcpy(cmd.addr, addr, ETH_ALEN);
626
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300627 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
628 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100629 if (ret)
630 return ret;
631
632 switch (status) {
633 case ADD_STA_SUCCESS:
634 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
635 return 0;
636 default:
637 ret = -EIO;
638 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
639 status);
640 break;
641 }
642 return ret;
643}
644
645int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
646{
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200647 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
648 mvm->cfg->base_params->wd_timeout :
649 IWL_WATCHDOG_DISABLED;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100650 int ret;
651
652 lockdep_assert_held(&mvm->mutex);
653
Ariej Marjieh7da91b02014-07-07 12:09:40 +0300654 /* Map Aux queue to fifo - needs to happen before adding Aux station */
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300655 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200656 IWL_MVM_TX_FIFO_MCAST, wdg_timeout);
Ariej Marjieh7da91b02014-07-07 12:09:40 +0300657
658 /* Allocate aux station and assign to it the aux queue */
659 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
Eliad Pellerb92e6612014-01-23 17:58:23 +0200660 NL80211_IFTYPE_UNSPECIFIED);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100661 if (ret)
662 return ret;
663
664 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
665 MAC_INDEX_AUX, 0);
666
667 if (ret)
668 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
669 return ret;
670}
671
Johannes Berg712b24a2014-08-04 14:14:14 +0200672void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
673{
674 lockdep_assert_held(&mvm->mutex);
675
676 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
677}
678
Johannes Berg8ca151b2013-01-24 14:25:36 +0100679/*
680 * Send the add station command for the vif's broadcast station.
681 * Assumes that the station was already allocated.
682 *
683 * @mvm: the mvm component
684 * @vif: the interface to which the broadcast station is added
685 * @bsta: the broadcast station to add.
686 */
Johannes Berg013290a2014-08-04 13:38:48 +0200687int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100688{
689 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +0200690 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +0200691 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +0100692 const u8 *baddr = _baddr;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100693
694 lockdep_assert_held(&mvm->mutex);
695
Johannes Berg5023d962013-07-31 14:07:43 +0200696 if (vif->type == NL80211_IFTYPE_ADHOC)
697 baddr = vif->bss_conf.bssid;
698
Johannes Berg8ca151b2013-01-24 14:25:36 +0100699 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
700 return -ENOSPC;
701
702 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
703 mvmvif->id, mvmvif->color);
704}
705
706/* Send the FW a request to remove the station from it's internal data
707 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +0200708int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100709{
Johannes Berg013290a2014-08-04 13:38:48 +0200710 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100711 int ret;
712
713 lockdep_assert_held(&mvm->mutex);
714
Johannes Berg013290a2014-08-04 13:38:48 +0200715 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100716 if (ret)
717 IWL_WARN(mvm, "Failed sending remove station\n");
718 return ret;
719}
720
Johannes Berg013290a2014-08-04 13:38:48 +0200721int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
722{
723 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
724 u32 qmask;
725
726 lockdep_assert_held(&mvm->mutex);
727
Arik Nemtsovd92b732e2014-09-21 19:00:42 +0300728 qmask = iwl_mvm_mac_get_queues_mask(vif);
Johannes Berg013290a2014-08-04 13:38:48 +0200729
730 /*
731 * The firmware defines the TFD queue mask to only be relevant
732 * for *unicast* queues, so the multicast (CAB) queue shouldn't
733 * be included.
734 */
735 if (vif->type == NL80211_IFTYPE_AP)
736 qmask &= ~BIT(vif->cab_queue);
737
738 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
739 ieee80211_vif_type_p2p(vif));
740}
741
Johannes Berg8ca151b2013-01-24 14:25:36 +0100742/* Allocate a new station entry for the broadcast station to the given vif,
743 * and send it to the FW.
744 * Note that each P2P mac should have its own broadcast station.
745 *
746 * @mvm: the mvm component
747 * @vif: the interface to which the broadcast station is added
748 * @bsta: the broadcast station to add. */
Johannes Berg013290a2014-08-04 13:38:48 +0200749int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100750{
751 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +0200752 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100753 int ret;
754
755 lockdep_assert_held(&mvm->mutex);
756
Johannes Berg013290a2014-08-04 13:38:48 +0200757 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100758 if (ret)
759 return ret;
760
Johannes Berg013290a2014-08-04 13:38:48 +0200761 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100762
763 if (ret)
764 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +0200765
Johannes Berg8ca151b2013-01-24 14:25:36 +0100766 return ret;
767}
768
Johannes Berg013290a2014-08-04 13:38:48 +0200769void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
770{
771 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
772
773 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
774}
775
Johannes Berg8ca151b2013-01-24 14:25:36 +0100776/*
777 * Send the FW a request to remove the station from it's internal data
778 * structures, and in addition remove it from the local data structure.
779 */
Johannes Berg013290a2014-08-04 13:38:48 +0200780int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100781{
782 int ret;
783
784 lockdep_assert_held(&mvm->mutex);
785
Johannes Berg013290a2014-08-04 13:38:48 +0200786 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100787
Johannes Berg013290a2014-08-04 13:38:48 +0200788 iwl_mvm_dealloc_bcast_sta(mvm, vif);
789
Johannes Berg8ca151b2013-01-24 14:25:36 +0100790 return ret;
791}
792
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300793#define IWL_MAX_RX_BA_SESSIONS 16
794
Johannes Berg8ca151b2013-01-24 14:25:36 +0100795int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
796 int tid, u16 ssn, bool start)
797{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100798 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300799 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100800 int ret;
801 u32 status;
802
803 lockdep_assert_held(&mvm->mutex);
804
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300805 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
806 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
807 return -ENOSPC;
808 }
809
Johannes Berg8ca151b2013-01-24 14:25:36 +0100810 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
811 cmd.sta_id = mvm_sta->sta_id;
812 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +0300813 if (start) {
814 cmd.add_immediate_ba_tid = (u8) tid;
815 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
816 } else {
817 cmd.remove_immediate_ba_tid = (u8) tid;
818 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100819 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
820 STA_MODIFY_REMOVE_BA_TID;
821
822 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300823 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
824 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100825 if (ret)
826 return ret;
827
828 switch (status) {
829 case ADD_STA_SUCCESS:
830 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
831 start ? "start" : "stopp");
832 break;
833 case ADD_STA_IMMEDIATE_BA_FAILURE:
834 IWL_WARN(mvm, "RX BA Session refused by fw\n");
835 ret = -ENOSPC;
836 break;
837 default:
838 ret = -EIO;
839 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
840 start ? "start" : "stopp", status);
841 break;
842 }
843
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300844 if (!ret) {
845 if (start)
846 mvm->rx_ba_sessions++;
847 else if (mvm->rx_ba_sessions > 0)
848 /* check that restart flow didn't zero the counter */
849 mvm->rx_ba_sessions--;
850 }
851
Johannes Berg8ca151b2013-01-24 14:25:36 +0100852 return ret;
853}
854
855static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
856 int tid, u8 queue, bool start)
857{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100858 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300859 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100860 int ret;
861 u32 status;
862
863 lockdep_assert_held(&mvm->mutex);
864
865 if (start) {
866 mvm_sta->tfd_queue_msk |= BIT(queue);
867 mvm_sta->tid_disable_agg &= ~BIT(tid);
868 } else {
869 mvm_sta->tfd_queue_msk &= ~BIT(queue);
870 mvm_sta->tid_disable_agg |= BIT(tid);
871 }
872
873 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
874 cmd.sta_id = mvm_sta->sta_id;
875 cmd.add_modify = STA_MODE_MODIFY;
876 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
877 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
878 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
879
880 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300881 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
882 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100883 if (ret)
884 return ret;
885
886 switch (status) {
887 case ADD_STA_SUCCESS:
888 break;
889 default:
890 ret = -EIO;
891 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
892 start ? "start" : "stopp", status);
893 break;
894 }
895
896 return ret;
897}
898
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +0200899const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100900 IEEE80211_AC_BE,
901 IEEE80211_AC_BK,
902 IEEE80211_AC_BK,
903 IEEE80211_AC_BE,
904 IEEE80211_AC_VI,
905 IEEE80211_AC_VI,
906 IEEE80211_AC_VO,
907 IEEE80211_AC_VO,
908};
909
Johannes Berg3e56ead2013-02-15 22:23:18 +0100910static const u8 tid_to_ucode_ac[] = {
911 AC_BE,
912 AC_BK,
913 AC_BK,
914 AC_BE,
915 AC_VI,
916 AC_VI,
917 AC_VO,
918 AC_VO,
919};
920
Johannes Berg8ca151b2013-01-24 14:25:36 +0100921int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
922 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
923{
Johannes Berg5b577a92013-11-14 18:20:04 +0100924 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100925 struct iwl_mvm_tid_data *tid_data;
926 int txq_id;
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300927 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100928
929 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
930 return -EINVAL;
931
932 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
933 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
934 mvmsta->tid_data[tid].state);
935 return -ENXIO;
936 }
937
938 lockdep_assert_held(&mvm->mutex);
939
Arik Nemtsovb2492502014-03-13 12:21:50 +0200940 spin_lock_bh(&mvmsta->lock);
941
942 /* possible race condition - we entered D0i3 while starting agg */
943 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
944 spin_unlock_bh(&mvmsta->lock);
945 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
946 return -EIO;
947 }
948
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300949 spin_lock_bh(&mvm->queue_info_lock);
950
951 txq_id = iwl_mvm_find_free_queue(mvm, mvm->first_agg_queue,
952 mvm->last_agg_queue);
953 if (txq_id < 0) {
954 ret = txq_id;
955 spin_unlock_bh(&mvm->queue_info_lock);
956 IWL_ERR(mvm, "Failed to allocate agg queue\n");
957 goto release_locks;
958 }
959 mvm->queue_info[txq_id].setup_reserved = true;
960 spin_unlock_bh(&mvm->queue_info_lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100961
Johannes Berg8ca151b2013-01-24 14:25:36 +0100962 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +0100963 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100964 tid_data->txq_id = txq_id;
965 *ssn = tid_data->ssn;
966
967 IWL_DEBUG_TX_QUEUES(mvm,
968 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
969 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
970 tid_data->next_reclaimed);
971
972 if (tid_data->ssn == tid_data->next_reclaimed) {
973 tid_data->state = IWL_AGG_STARTING;
974 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
975 } else {
976 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
977 }
978
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300979 ret = 0;
980
981release_locks:
Johannes Berg8ca151b2013-01-24 14:25:36 +0100982 spin_unlock_bh(&mvmsta->lock);
983
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300984 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100985}
986
987int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
988 struct ieee80211_sta *sta, u16 tid, u8 buf_size)
989{
Johannes Berg5b577a92013-11-14 18:20:04 +0100990 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100991 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +0200992 unsigned int wdg_timeout =
993 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100994 int queue, fifo, ret;
995 u16 ssn;
996
Eyal Shapiraefed6642014-09-14 15:58:53 +0300997 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
998 != IWL_MAX_TID_COUNT);
999
Johannes Berg8ca151b2013-01-24 14:25:36 +01001000 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
1001
1002 spin_lock_bh(&mvmsta->lock);
1003 ssn = tid_data->ssn;
1004 queue = tid_data->txq_id;
1005 tid_data->state = IWL_AGG_ON;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001006 mvmsta->agg_tids |= BIT(tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001007 tid_data->ssn = 0xffff;
1008 spin_unlock_bh(&mvmsta->lock);
1009
Johannes Berg3e56ead2013-02-15 22:23:18 +01001010 fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001011
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001012 iwl_mvm_enable_agg_txq(mvm, queue,
1013 vif->hw_queue[tid_to_mac80211_ac[tid]], fifo,
1014 mvmsta->sta_id, tid, buf_size, ssn, wdg_timeout);
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03001015
Johannes Berg8ca151b2013-01-24 14:25:36 +01001016 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1017 if (ret)
1018 return -EIO;
1019
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001020 /* No need to mark as reserved */
1021 spin_lock_bh(&mvm->queue_info_lock);
1022 mvm->queue_info[queue].setup_reserved = false;
1023 spin_unlock_bh(&mvm->queue_info_lock);
1024
Johannes Berg8ca151b2013-01-24 14:25:36 +01001025 /*
1026 * Even though in theory the peer could have different
1027 * aggregation reorder buffer sizes for different sessions,
1028 * our ucode doesn't allow for that and has a global limit
1029 * for each station. Therefore, use the minimum of all the
1030 * aggregation sessions and our default value.
1031 */
1032 mvmsta->max_agg_bufsize =
1033 min(mvmsta->max_agg_bufsize, buf_size);
1034 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
1035
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03001036 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
1037 sta->addr, tid);
1038
Eyal Shapira9e680942013-11-09 00:16:16 +02001039 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001040}
1041
1042int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1043 struct ieee80211_sta *sta, u16 tid)
1044{
Johannes Berg5b577a92013-11-14 18:20:04 +01001045 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001046 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1047 u16 txq_id;
1048 int err;
1049
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +02001050
1051 /*
1052 * If mac80211 is cleaning its state, then say that we finished since
1053 * our state has been cleared anyway.
1054 */
1055 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1056 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1057 return 0;
1058 }
1059
Johannes Berg8ca151b2013-01-24 14:25:36 +01001060 spin_lock_bh(&mvmsta->lock);
1061
1062 txq_id = tid_data->txq_id;
1063
1064 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
1065 mvmsta->sta_id, tid, txq_id, tid_data->state);
1066
Eyal Shapiraefed6642014-09-14 15:58:53 +03001067 mvmsta->agg_tids &= ~BIT(tid);
1068
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001069 /* No need to mark as reserved anymore */
1070 spin_lock_bh(&mvm->queue_info_lock);
1071 mvm->queue_info[txq_id].setup_reserved = false;
1072 spin_unlock_bh(&mvm->queue_info_lock);
1073
Johannes Berg8ca151b2013-01-24 14:25:36 +01001074 switch (tid_data->state) {
1075 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01001076 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001077
1078 IWL_DEBUG_TX_QUEUES(mvm,
1079 "ssn = %d, next_recl = %d\n",
1080 tid_data->ssn, tid_data->next_reclaimed);
1081
1082 /* There are still packets for this RA / TID in the HW */
1083 if (tid_data->ssn != tid_data->next_reclaimed) {
1084 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
1085 err = 0;
1086 break;
1087 }
1088
1089 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +02001090 tid_data->state = IWL_AGG_OFF;
Johannes Bergf7f89e72014-08-05 15:24:44 +02001091 spin_unlock_bh(&mvmsta->lock);
1092
1093 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1094
1095 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1096
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001097 iwl_mvm_disable_txq(mvm, txq_id,
1098 vif->hw_queue[tid_to_mac80211_ac[tid]], tid,
1099 0);
Johannes Bergf7f89e72014-08-05 15:24:44 +02001100 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001101 case IWL_AGG_STARTING:
1102 case IWL_EMPTYING_HW_QUEUE_ADDBA:
1103 /*
1104 * The agg session has been stopped before it was set up. This
1105 * can happen when the AddBA timer times out for example.
1106 */
1107
1108 /* No barriers since we are under mutex */
1109 lockdep_assert_held(&mvm->mutex);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001110
1111 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1112 tid_data->state = IWL_AGG_OFF;
1113 err = 0;
1114 break;
1115 default:
1116 IWL_ERR(mvm,
1117 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
1118 mvmsta->sta_id, tid, tid_data->state);
1119 IWL_ERR(mvm,
1120 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
1121 err = -EINVAL;
1122 }
1123
1124 spin_unlock_bh(&mvmsta->lock);
1125
1126 return err;
1127}
1128
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001129int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1130 struct ieee80211_sta *sta, u16 tid)
1131{
Johannes Berg5b577a92013-11-14 18:20:04 +01001132 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001133 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1134 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02001135 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001136
1137 /*
1138 * First set the agg state to OFF to avoid calling
1139 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
1140 */
1141 spin_lock_bh(&mvmsta->lock);
1142 txq_id = tid_data->txq_id;
1143 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
1144 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001145 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001146 tid_data->state = IWL_AGG_OFF;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001147 mvmsta->agg_tids &= ~BIT(tid);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001148 spin_unlock_bh(&mvmsta->lock);
1149
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001150 /* No need to mark as reserved */
1151 spin_lock_bh(&mvm->queue_info_lock);
1152 mvm->queue_info[txq_id].setup_reserved = false;
1153 spin_unlock_bh(&mvm->queue_info_lock);
1154
Johannes Bergb6658ff2013-07-24 13:55:51 +02001155 if (old_state >= IWL_AGG_ON) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001156 iwl_mvm_drain_sta(mvm, mvmsta, true);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001157 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true))
1158 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001159 iwl_trans_wait_tx_queue_empty(mvm->trans,
1160 mvmsta->tfd_queue_msk);
1161 iwl_mvm_drain_sta(mvm, mvmsta, false);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001162
Johannes Bergf7f89e72014-08-05 15:24:44 +02001163 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1164
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001165 iwl_mvm_disable_txq(mvm, tid_data->txq_id,
1166 vif->hw_queue[tid_to_mac80211_ac[tid]], tid,
1167 0);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001168 }
1169
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001170 return 0;
1171}
1172
Johannes Berg8ca151b2013-01-24 14:25:36 +01001173static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
1174{
Johannes Berg2dc2a152015-06-16 17:09:18 +02001175 int i, max = -1, max_offs = -1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001176
1177 lockdep_assert_held(&mvm->mutex);
1178
Johannes Berg2dc2a152015-06-16 17:09:18 +02001179 /* Pick the unused key offset with the highest 'deleted'
1180 * counter. Every time a key is deleted, all the counters
1181 * are incremented and the one that was just deleted is
1182 * reset to zero. Thus, the highest counter is the one
1183 * that was deleted longest ago. Pick that one.
1184 */
1185 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
1186 if (test_bit(i, mvm->fw_key_table))
1187 continue;
1188 if (mvm->fw_key_deleted[i] > max) {
1189 max = mvm->fw_key_deleted[i];
1190 max_offs = i;
1191 }
1192 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001193
Johannes Berg2dc2a152015-06-16 17:09:18 +02001194 if (max_offs < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001195 return STA_KEY_IDX_INVALID;
1196
Johannes Berg2dc2a152015-06-16 17:09:18 +02001197 __set_bit(max_offs, mvm->fw_key_table);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001198
Johannes Berg2dc2a152015-06-16 17:09:18 +02001199 return max_offs;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001200}
1201
1202static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
1203 struct ieee80211_sta *sta)
1204{
Johannes Berg5b530e92014-12-23 16:00:17 +01001205 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001206
1207 if (sta) {
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001208 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001209
1210 return mvm_sta->sta_id;
1211 }
1212
1213 /*
1214 * The device expects GTKs for station interfaces to be
1215 * installed as GTKs for the AP station. If we have no
1216 * station ID, then use AP's station ID.
1217 */
1218 if (vif->type == NL80211_IFTYPE_STATION &&
1219 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
1220 return mvmvif->ap_sta_id;
1221
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001222 return IWL_MVM_STATION_COUNT;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001223}
1224
1225static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
1226 struct iwl_mvm_sta *mvm_sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01001227 struct ieee80211_key_conf *keyconf, bool mcast,
Johannes Berg2f6319d2014-11-12 23:39:56 +01001228 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001229{
Max Stepanov5a258aa2013-04-07 09:11:21 +03001230 struct iwl_mvm_add_sta_key_cmd cmd = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001231 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01001232 int ret;
1233 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001234 u16 keyidx;
1235 int i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001236 u8 sta_id = mvm_sta->sta_id;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001237
1238 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1239 STA_KEY_FLG_KEYID_MSK;
1240 key_flags = cpu_to_le16(keyidx);
1241 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
1242
1243 switch (keyconf->cipher) {
1244 case WLAN_CIPHER_SUITE_TKIP:
1245 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001246 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001247 for (i = 0; i < 5; i++)
Max Stepanov5a258aa2013-04-07 09:11:21 +03001248 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1249 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001250 break;
1251 case WLAN_CIPHER_SUITE_CCMP:
1252 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001253 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001254 break;
Johannes Bergba3943b2014-11-12 23:54:48 +01001255 case WLAN_CIPHER_SUITE_WEP104:
1256 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
John W. Linvilleaa0cb082015-01-12 16:18:11 -05001257 /* fall through */
Johannes Bergba3943b2014-11-12 23:54:48 +01001258 case WLAN_CIPHER_SUITE_WEP40:
1259 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
1260 memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
1261 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001262 default:
Max Stepanove36e5432013-08-27 19:56:13 +03001263 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
1264 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001265 }
1266
Johannes Bergba3943b2014-11-12 23:54:48 +01001267 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001268 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1269
Max Stepanov5a258aa2013-04-07 09:11:21 +03001270 cmd.key_offset = keyconf->hw_key_idx;
1271 cmd.key_flags = key_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001272 cmd.sta_id = sta_id;
1273
1274 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001275 if (cmd_flags & CMD_ASYNC)
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001276 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
1277 sizeof(cmd), &cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001278 else
1279 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1280 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001281
1282 switch (status) {
1283 case ADD_STA_SUCCESS:
1284 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1285 break;
1286 default:
1287 ret = -EIO;
1288 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1289 break;
1290 }
1291
1292 return ret;
1293}
1294
1295static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1296 struct ieee80211_key_conf *keyconf,
1297 u8 sta_id, bool remove_key)
1298{
1299 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1300
1301 /* verify the key details match the required command's expectations */
1302 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1303 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1304 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1305 return -EINVAL;
1306
1307 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1308 igtk_cmd.sta_id = cpu_to_le32(sta_id);
1309
1310 if (remove_key) {
1311 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1312 } else {
1313 struct ieee80211_key_seq seq;
1314 const u8 *pn;
1315
1316 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001317 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1318 pn = seq.aes_cmac.pn;
1319 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1320 ((u64) pn[4] << 8) |
1321 ((u64) pn[3] << 16) |
1322 ((u64) pn[2] << 24) |
1323 ((u64) pn[1] << 32) |
1324 ((u64) pn[0] << 40));
1325 }
1326
1327 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1328 remove_key ? "removing" : "installing",
1329 igtk_cmd.sta_id);
1330
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001331 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001332 sizeof(igtk_cmd), &igtk_cmd);
1333}
1334
1335
1336static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1337 struct ieee80211_vif *vif,
1338 struct ieee80211_sta *sta)
1339{
Johannes Berg5b530e92014-12-23 16:00:17 +01001340 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001341
1342 if (sta)
1343 return sta->addr;
1344
1345 if (vif->type == NL80211_IFTYPE_STATION &&
1346 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1347 u8 sta_id = mvmvif->ap_sta_id;
1348 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1349 lockdep_is_held(&mvm->mutex));
1350 return sta->addr;
1351 }
1352
1353
1354 return NULL;
1355}
1356
Johannes Berg2f6319d2014-11-12 23:39:56 +01001357static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1358 struct ieee80211_vif *vif,
1359 struct ieee80211_sta *sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01001360 struct ieee80211_key_conf *keyconf,
1361 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001362{
Johannes Berg2f6319d2014-11-12 23:39:56 +01001363 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001364 int ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001365 const u8 *addr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001366 struct ieee80211_key_seq seq;
1367 u16 p1k[5];
1368
Johannes Berg8ca151b2013-01-24 14:25:36 +01001369 switch (keyconf->cipher) {
1370 case WLAN_CIPHER_SUITE_TKIP:
1371 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1372 /* get phase 1 key from mac80211 */
1373 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1374 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Bergba3943b2014-11-12 23:54:48 +01001375 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001376 seq.tkip.iv32, p1k, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001377 break;
1378 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergba3943b2014-11-12 23:54:48 +01001379 case WLAN_CIPHER_SUITE_WEP40:
1380 case WLAN_CIPHER_SUITE_WEP104:
1381 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001382 0, NULL, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001383 break;
1384 default:
Johannes Bergba3943b2014-11-12 23:54:48 +01001385 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Johannes Berg2f6319d2014-11-12 23:39:56 +01001386 0, NULL, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001387 }
1388
Johannes Berg8ca151b2013-01-24 14:25:36 +01001389 return ret;
1390}
1391
Johannes Berg2f6319d2014-11-12 23:39:56 +01001392static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
Johannes Bergba3943b2014-11-12 23:54:48 +01001393 struct ieee80211_key_conf *keyconf,
1394 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001395{
Max Stepanov5a258aa2013-04-07 09:11:21 +03001396 struct iwl_mvm_add_sta_key_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001397 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01001398 int ret;
1399 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001400
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02001401 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1402 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001403 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1404 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1405
Johannes Bergba3943b2014-11-12 23:54:48 +01001406 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001407 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1408
Max Stepanov5a258aa2013-04-07 09:11:21 +03001409 cmd.key_flags = key_flags;
1410 cmd.key_offset = keyconf->hw_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001411 cmd.sta_id = sta_id;
1412
Johannes Berg8ca151b2013-01-24 14:25:36 +01001413 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001414 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1415 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001416
1417 switch (status) {
1418 case ADD_STA_SUCCESS:
1419 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1420 break;
1421 default:
1422 ret = -EIO;
1423 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1424 break;
1425 }
1426
1427 return ret;
1428}
1429
Johannes Berg2f6319d2014-11-12 23:39:56 +01001430int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1431 struct ieee80211_vif *vif,
1432 struct ieee80211_sta *sta,
1433 struct ieee80211_key_conf *keyconf,
1434 bool have_key_offset)
1435{
Johannes Bergba3943b2014-11-12 23:54:48 +01001436 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg2f6319d2014-11-12 23:39:56 +01001437 u8 sta_id;
1438 int ret;
Matti Gottlieb11828db2015-06-01 15:15:11 +03001439 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
Johannes Berg2f6319d2014-11-12 23:39:56 +01001440
1441 lockdep_assert_held(&mvm->mutex);
1442
1443 /* Get the station id from the mvm local station table */
1444 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1445 if (sta_id == IWL_MVM_STATION_COUNT) {
1446 IWL_ERR(mvm, "Failed to find station id\n");
1447 return -EINVAL;
1448 }
1449
1450 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1451 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1452 goto end;
1453 }
1454
1455 /*
1456 * It is possible that the 'sta' parameter is NULL, and thus
1457 * there is a need to retrieve the sta from the local station table.
1458 */
1459 if (!sta) {
1460 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1461 lockdep_is_held(&mvm->mutex));
1462 if (IS_ERR_OR_NULL(sta)) {
1463 IWL_ERR(mvm, "Invalid station id\n");
1464 return -EINVAL;
1465 }
1466 }
1467
1468 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
1469 return -EINVAL;
1470
1471 if (!have_key_offset) {
1472 /*
1473 * The D3 firmware hardcodes the PTK offset to 0, so we have to
1474 * configure it there. As a result, this workaround exists to
1475 * let the caller set the key offset (hw_key_idx), see d3.c.
1476 */
1477 keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
1478 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1479 return -ENOSPC;
1480 }
1481
Johannes Bergba3943b2014-11-12 23:54:48 +01001482 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, mcast);
1483 if (ret) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01001484 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
Johannes Bergba3943b2014-11-12 23:54:48 +01001485 goto end;
1486 }
1487
1488 /*
1489 * For WEP, the same key is used for multicast and unicast. Upload it
1490 * again, using the same key offset, and now pointing the other one
1491 * to the same key slot (offset).
1492 * If this fails, remove the original as well.
1493 */
1494 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
1495 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
1496 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, !mcast);
1497 if (ret) {
1498 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1499 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
1500 }
1501 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01001502
1503end:
1504 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1505 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Matti Gottlieb11828db2015-06-01 15:15:11 +03001506 sta ? sta->addr : zero_addr, ret);
Johannes Berg2f6319d2014-11-12 23:39:56 +01001507 return ret;
1508}
1509
1510int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1511 struct ieee80211_vif *vif,
1512 struct ieee80211_sta *sta,
1513 struct ieee80211_key_conf *keyconf)
1514{
Johannes Bergba3943b2014-11-12 23:54:48 +01001515 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg2f6319d2014-11-12 23:39:56 +01001516 u8 sta_id;
Johannes Berg2dc2a152015-06-16 17:09:18 +02001517 int ret, i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001518
1519 lockdep_assert_held(&mvm->mutex);
1520
1521 /* Get the station id from the mvm local station table */
1522 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1523
1524 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1525 keyconf->keyidx, sta_id);
1526
1527 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1528 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1529
1530 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
1531 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1532 keyconf->hw_key_idx);
1533 return -ENOENT;
1534 }
1535
Johannes Berg2dc2a152015-06-16 17:09:18 +02001536 /* track which key was deleted last */
1537 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
1538 if (mvm->fw_key_deleted[i] < U8_MAX)
1539 mvm->fw_key_deleted[i]++;
1540 }
1541 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
1542
Johannes Berg2f6319d2014-11-12 23:39:56 +01001543 if (sta_id == IWL_MVM_STATION_COUNT) {
1544 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1545 return 0;
1546 }
1547
1548 /*
1549 * It is possible that the 'sta' parameter is NULL, and thus
1550 * there is a need to retrieve the sta from the local station table,
1551 * for example when a GTK is removed (where the sta_id will then be
1552 * the AP ID, and no station was passed by mac80211.)
1553 */
1554 if (!sta) {
1555 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1556 lockdep_is_held(&mvm->mutex));
1557 if (!sta) {
1558 IWL_ERR(mvm, "Invalid station id\n");
1559 return -EINVAL;
1560 }
1561 }
1562
1563 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
1564 return -EINVAL;
1565
Johannes Bergba3943b2014-11-12 23:54:48 +01001566 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
1567 if (ret)
1568 return ret;
1569
1570 /* delete WEP key twice to get rid of (now useless) offset */
1571 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
1572 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
1573 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
1574
1575 return ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001576}
1577
Johannes Berg8ca151b2013-01-24 14:25:36 +01001578void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1579 struct ieee80211_vif *vif,
1580 struct ieee80211_key_conf *keyconf,
1581 struct ieee80211_sta *sta, u32 iv32,
1582 u16 *phase1key)
1583{
Beni Levc3eb5362013-02-06 17:22:18 +02001584 struct iwl_mvm_sta *mvm_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001585 u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
Johannes Bergba3943b2014-11-12 23:54:48 +01001586 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001587
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001588 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001589 return;
1590
Beni Levc3eb5362013-02-06 17:22:18 +02001591 rcu_read_lock();
1592
1593 if (!sta) {
1594 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1595 if (WARN_ON(IS_ERR_OR_NULL(sta))) {
1596 rcu_read_unlock();
1597 return;
1598 }
1599 }
1600
Johannes Berg2f6319d2014-11-12 23:39:56 +01001601 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Bergba3943b2014-11-12 23:54:48 +01001602 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001603 iv32, phase1key, CMD_ASYNC);
Beni Levc3eb5362013-02-06 17:22:18 +02001604 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01001605}
1606
Johannes Berg9cc40712013-02-15 22:47:48 +01001607void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
1608 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001609{
Johannes Berg5b577a92013-11-14 18:20:04 +01001610 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001611 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001612 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01001613 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03001614 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01001615 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01001616 };
1617 int ret;
1618
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001619 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001620 if (ret)
1621 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1622}
1623
Johannes Berg9cc40712013-02-15 22:47:48 +01001624void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
1625 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001626 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01001627 u16 cnt, u16 tids, bool more_data,
1628 bool agg)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001629{
Johannes Berg5b577a92013-11-14 18:20:04 +01001630 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001631 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001632 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01001633 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001634 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1635 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01001636 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01001637 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01001638 int tid, ret;
1639 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001640
Johannes Berg3e56ead2013-02-15 22:23:18 +01001641 /* convert TIDs to ACs - we don't support TSPEC so that's OK
1642 * Note that this field is reserved and unused by firmware not
1643 * supporting GO uAPSD, so it's safe to always do this.
1644 */
1645 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
1646 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
1647
1648 /* If we're releasing frames from aggregation queues then check if the
1649 * all queues combined that we're releasing frames from have
1650 * - more frames than the service period, in which case more_data
1651 * needs to be set
1652 * - fewer than 'cnt' frames, in which case we need to adjust the
1653 * firmware command (but do that unconditionally)
1654 */
1655 if (agg) {
1656 int remaining = cnt;
1657
1658 spin_lock_bh(&mvmsta->lock);
1659 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
1660 struct iwl_mvm_tid_data *tid_data;
1661 u16 n_queued;
1662
1663 tid_data = &mvmsta->tid_data[tid];
1664 if (WARN(tid_data->state != IWL_AGG_ON &&
1665 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
1666 "TID %d state is %d\n",
1667 tid, tid_data->state)) {
1668 spin_unlock_bh(&mvmsta->lock);
1669 ieee80211_sta_eosp(sta);
1670 return;
1671 }
1672
1673 n_queued = iwl_mvm_tid_queued(tid_data);
1674 if (n_queued > remaining) {
1675 more_data = true;
1676 remaining = 0;
1677 break;
1678 }
1679 remaining -= n_queued;
1680 }
1681 spin_unlock_bh(&mvmsta->lock);
1682
1683 cmd.sleep_tx_count = cpu_to_le16(cnt - remaining);
1684 if (WARN_ON(cnt - remaining == 0)) {
1685 ieee80211_sta_eosp(sta);
1686 return;
1687 }
1688 }
1689
1690 /* Note: this is ignored by firmware not supporting GO uAPSD */
1691 if (more_data)
1692 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
1693
1694 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
1695 mvmsta->next_status_eosp = true;
1696 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
1697 } else {
1698 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
1699 }
1700
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001701 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001702 if (ret)
1703 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1704}
Johannes Berg3e56ead2013-02-15 22:23:18 +01001705
Johannes Berg04168412015-06-23 21:22:09 +02001706void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
1707 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg3e56ead2013-02-15 22:23:18 +01001708{
1709 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1710 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
1711 struct ieee80211_sta *sta;
1712 u32 sta_id = le32_to_cpu(notif->sta_id);
1713
1714 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +02001715 return;
Johannes Berg3e56ead2013-02-15 22:23:18 +01001716
1717 rcu_read_lock();
1718 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1719 if (!IS_ERR_OR_NULL(sta))
1720 ieee80211_sta_eosp(sta);
1721 rcu_read_unlock();
Johannes Berg3e56ead2013-02-15 22:23:18 +01001722}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03001723
1724void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
1725 struct iwl_mvm_sta *mvmsta, bool disable)
1726{
1727 struct iwl_mvm_add_sta_cmd cmd = {
1728 .add_modify = STA_MODE_MODIFY,
1729 .sta_id = mvmsta->sta_id,
1730 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
1731 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
1732 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1733 };
1734 int ret;
1735
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03001736 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1737 if (ret)
1738 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1739}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001740
1741void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
1742 struct ieee80211_sta *sta,
1743 bool disable)
1744{
1745 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1746
1747 spin_lock_bh(&mvm_sta->lock);
1748
1749 if (mvm_sta->disable_tx == disable) {
1750 spin_unlock_bh(&mvm_sta->lock);
1751 return;
1752 }
1753
1754 mvm_sta->disable_tx = disable;
1755
1756 /*
Sara Sharon0d365ae2015-03-31 12:24:05 +03001757 * Tell mac80211 to start/stop queuing tx for this station,
1758 * but don't stop queuing if there are still pending frames
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001759 * for this station.
1760 */
1761 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
1762 ieee80211_sta_block_awake(mvm->hw, sta, disable);
1763
1764 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
1765
1766 spin_unlock_bh(&mvm_sta->lock);
1767}
1768
1769void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
1770 struct iwl_mvm_vif *mvmvif,
1771 bool disable)
1772{
1773 struct ieee80211_sta *sta;
1774 struct iwl_mvm_sta *mvm_sta;
1775 int i;
1776
1777 lockdep_assert_held(&mvm->mutex);
1778
1779 /* Block/unblock all the stations of the given mvmvif */
1780 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
1781 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
1782 lockdep_is_held(&mvm->mutex));
1783 if (IS_ERR_OR_NULL(sta))
1784 continue;
1785
1786 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1787 if (mvm_sta->mac_id_n_color !=
1788 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
1789 continue;
1790
1791 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
1792 }
1793}
Luciano Coelhodc88b4b2014-11-10 11:10:14 +02001794
1795void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1796{
1797 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1798 struct iwl_mvm_sta *mvmsta;
1799
1800 rcu_read_lock();
1801
1802 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
1803
1804 if (!WARN_ON(!mvmsta))
1805 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
1806
1807 rcu_read_unlock();
1808}