blob: 666f16b4bed9e5da9a2a943c44c9873faf5b0729 [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 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02008 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +02009 * Copyright(c) 2013 - 2014 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 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020034 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +020035 * Copyright(c) 2013 - 2014 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{
102 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
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
207int iwl_mvm_add_sta(struct iwl_mvm *mvm,
208 struct ieee80211_vif *vif,
209 struct ieee80211_sta *sta)
210{
211 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
212 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
213 int i, ret, sta_id;
214
215 lockdep_assert_held(&mvm->mutex);
216
217 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
Eliad Pellerb92e6612014-01-23 17:58:23 +0200218 sta_id = iwl_mvm_find_free_sta_id(mvm,
219 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100220 else
221 sta_id = mvm_sta->sta_id;
222
223 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
224 return -ENOSPC;
225
226 spin_lock_init(&mvm_sta->lock);
227
228 mvm_sta->sta_id = sta_id;
229 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
230 mvmvif->color);
231 mvm_sta->vif = vif;
232 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300233 mvm_sta->tx_protection = 0;
234 mvm_sta->tt_tx_protection = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100235
236 /* HW restart, don't assume the memory has been zeroed */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300237 atomic_set(&mvm->pending_frames[sta_id], 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100238 mvm_sta->tid_disable_agg = 0;
239 mvm_sta->tfd_queue_msk = 0;
240 for (i = 0; i < IEEE80211_NUM_ACS; i++)
241 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
242 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
243
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200244 /* for HW restart - reset everything but the sequence number */
245 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
246 u16 seq = mvm_sta->tid_data[i].seq_number;
247 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
248 mvm_sta->tid_data[i].seq_number = seq;
249 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100250
Johannes Berg7a453972013-02-12 13:10:44 +0100251 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100252 if (ret)
253 return ret;
254
Johannes Berg9e848012014-08-04 14:33:42 +0200255 if (vif->type == NL80211_IFTYPE_STATION) {
256 if (!sta->tdls) {
257 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
258 mvmvif->ap_sta_id = sta_id;
259 } else {
260 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
261 }
262 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100263
264 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
265
266 return 0;
267}
268
Johannes Berg7a453972013-02-12 13:10:44 +0100269int iwl_mvm_update_sta(struct iwl_mvm *mvm,
270 struct ieee80211_vif *vif,
271 struct ieee80211_sta *sta)
272{
273 return iwl_mvm_sta_send_to_fw(mvm, sta, true);
274}
275
Johannes Berg8ca151b2013-01-24 14:25:36 +0100276int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
277 bool drain)
278{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300279 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100280 int ret;
281 u32 status;
282
283 lockdep_assert_held(&mvm->mutex);
284
285 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
286 cmd.sta_id = mvmsta->sta_id;
287 cmd.add_modify = STA_MODE_MODIFY;
288 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
289 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
290
291 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300292 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
293 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100294 if (ret)
295 return ret;
296
297 switch (status) {
298 case ADD_STA_SUCCESS:
299 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
300 mvmsta->sta_id);
301 break;
302 default:
303 ret = -EIO;
304 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
305 mvmsta->sta_id);
306 break;
307 }
308
309 return ret;
310}
311
312/*
313 * Remove a station from the FW table. Before sending the command to remove
314 * the station validate that the station is indeed known to the driver (sanity
315 * only).
316 */
317static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
318{
319 struct ieee80211_sta *sta;
320 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
321 .sta_id = sta_id,
322 };
323 int ret;
324
325 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
326 lockdep_is_held(&mvm->mutex));
327
328 /* Note: internal stations are marked as error values */
329 if (!sta) {
330 IWL_ERR(mvm, "Invalid station id\n");
331 return -EINVAL;
332 }
333
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300334 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100335 sizeof(rm_sta_cmd), &rm_sta_cmd);
336 if (ret) {
337 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
338 return ret;
339 }
340
341 return 0;
342}
343
344void iwl_mvm_sta_drained_wk(struct work_struct *wk)
345{
346 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
347 u8 sta_id;
348
349 /*
350 * The mutex is needed because of the SYNC cmd, but not only: if the
351 * work would run concurrently with iwl_mvm_rm_sta, it would run before
352 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
353 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
354 * that later.
355 */
356 mutex_lock(&mvm->mutex);
357
358 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
359 int ret;
360 struct ieee80211_sta *sta =
361 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
362 lockdep_is_held(&mvm->mutex));
363
Johannes Berg1ddbbb02013-12-04 22:39:17 +0100364 /*
365 * This station is in use or RCU-removed; the latter happens in
366 * managed mode, where mac80211 removes the station before we
367 * can remove it from firmware (we can only do that after the
368 * MAC is marked unassociated), and possibly while the deauth
369 * frame to disconnect from the AP is still queued. Then, the
370 * station pointer is -ENOENT when the last skb is reclaimed.
371 */
372 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100373 continue;
374
375 if (PTR_ERR(sta) == -EINVAL) {
376 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
377 sta_id);
378 continue;
379 }
380
381 if (!sta) {
382 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
383 sta_id);
384 continue;
385 }
386
387 WARN_ON(PTR_ERR(sta) != -EBUSY);
388 /* This station was removed and we waited until it got drained,
389 * we can now proceed and remove it.
390 */
391 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
392 if (ret) {
393 IWL_ERR(mvm,
394 "Couldn't remove sta %d after it was drained\n",
395 sta_id);
396 continue;
397 }
Monam Agarwalc531c772014-03-24 00:05:56 +0530398 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100399 clear_bit(sta_id, mvm->sta_drained);
400 }
401
402 mutex_unlock(&mvm->mutex);
403}
404
405int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
406 struct ieee80211_vif *vif,
407 struct ieee80211_sta *sta)
408{
409 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
410 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
411 int ret;
412
413 lockdep_assert_held(&mvm->mutex);
414
415 if (vif->type == NL80211_IFTYPE_STATION &&
416 mvmvif->ap_sta_id == mvm_sta->sta_id) {
Emmanuel Grumbach80d85652013-02-19 15:32:42 +0200417 /* flush its queues here since we are freeing mvm_sta */
418 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true);
419
Johannes Berg8ca151b2013-01-24 14:25:36 +0100420 /* if we are associated - we can't remove the AP STA now */
421 if (vif->bss_conf.assoc)
422 return ret;
423
424 /* unassoc - go ahead - remove the AP STA now */
425 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
Eliad Peller37577fe2013-12-05 17:19:39 +0200426
427 /* clear d0i3_ap_sta_id if no longer relevant */
428 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
429 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100430 }
431
432 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300433 * Make sure that the tx response code sees the station as -EBUSY and
434 * calls the drain worker.
435 */
436 spin_lock_bh(&mvm_sta->lock);
437 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +0100438 * There are frames pending on the AC queues for this station.
439 * We need to wait until all the frames are drained...
440 */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300441 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100442 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
443 ERR_PTR(-EBUSY));
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300444 spin_unlock_bh(&mvm_sta->lock);
445 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100446 } else {
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300447 spin_unlock_bh(&mvm_sta->lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100448 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
Monam Agarwalc531c772014-03-24 00:05:56 +0530449 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100450 }
451
452 return ret;
453}
454
455int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
456 struct ieee80211_vif *vif,
457 u8 sta_id)
458{
459 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
460
461 lockdep_assert_held(&mvm->mutex);
462
Monam Agarwalc531c772014-03-24 00:05:56 +0530463 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100464 return ret;
465}
466
Johannes Berg712b24a2014-08-04 14:14:14 +0200467static int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
468 struct iwl_mvm_int_sta *sta,
469 u32 qmask, enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100470{
471 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +0200472 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100473 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
474 return -ENOSPC;
475 }
476
477 sta->tfd_queue_msk = qmask;
478
479 /* put a non-NULL value so iterating over the stations won't stop */
480 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
481 return 0;
482}
483
Johannes Berg712b24a2014-08-04 14:14:14 +0200484static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
485 struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100486{
Monam Agarwalc531c772014-03-24 00:05:56 +0530487 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100488 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
489 sta->sta_id = IWL_MVM_STATION_COUNT;
490}
491
492static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
493 struct iwl_mvm_int_sta *sta,
494 const u8 *addr,
495 u16 mac_id, u16 color)
496{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300497 struct iwl_mvm_add_sta_cmd cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100498 int ret;
499 u32 status;
500
501 lockdep_assert_held(&mvm->mutex);
502
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300503 memset(&cmd, 0, sizeof(cmd));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100504 cmd.sta_id = sta->sta_id;
505 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
506 color));
507
508 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
509
510 if (addr)
511 memcpy(cmd.addr, addr, ETH_ALEN);
512
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300513 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
514 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100515 if (ret)
516 return ret;
517
518 switch (status) {
519 case ADD_STA_SUCCESS:
520 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
521 return 0;
522 default:
523 ret = -EIO;
524 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
525 status);
526 break;
527 }
528 return ret;
529}
530
531int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
532{
533 int ret;
534
535 lockdep_assert_held(&mvm->mutex);
536
Ariej Marjieh7da91b02014-07-07 12:09:40 +0300537 /* Map Aux queue to fifo - needs to happen before adding Aux station */
538 iwl_trans_ac_txq_enable(mvm->trans, mvm->aux_queue,
539 IWL_MVM_TX_FIFO_MCAST);
540
541 /* Allocate aux station and assign to it the aux queue */
542 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
Eliad Pellerb92e6612014-01-23 17:58:23 +0200543 NL80211_IFTYPE_UNSPECIFIED);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100544 if (ret)
545 return ret;
546
547 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
548 MAC_INDEX_AUX, 0);
549
550 if (ret)
551 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
552 return ret;
553}
554
Johannes Berg712b24a2014-08-04 14:14:14 +0200555void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
556{
557 lockdep_assert_held(&mvm->mutex);
558
559 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
560}
561
Johannes Berg8ca151b2013-01-24 14:25:36 +0100562/*
563 * Send the add station command for the vif's broadcast station.
564 * Assumes that the station was already allocated.
565 *
566 * @mvm: the mvm component
567 * @vif: the interface to which the broadcast station is added
568 * @bsta: the broadcast station to add.
569 */
Johannes Berg013290a2014-08-04 13:38:48 +0200570int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100571{
572 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +0200573 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +0200574 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +0100575 const u8 *baddr = _baddr;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100576
577 lockdep_assert_held(&mvm->mutex);
578
Johannes Berg5023d962013-07-31 14:07:43 +0200579 if (vif->type == NL80211_IFTYPE_ADHOC)
580 baddr = vif->bss_conf.bssid;
581
Johannes Berg8ca151b2013-01-24 14:25:36 +0100582 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
583 return -ENOSPC;
584
585 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
586 mvmvif->id, mvmvif->color);
587}
588
589/* Send the FW a request to remove the station from it's internal data
590 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +0200591int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100592{
Johannes Berg013290a2014-08-04 13:38:48 +0200593 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100594 int ret;
595
596 lockdep_assert_held(&mvm->mutex);
597
Johannes Berg013290a2014-08-04 13:38:48 +0200598 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100599 if (ret)
600 IWL_WARN(mvm, "Failed sending remove station\n");
601 return ret;
602}
603
Johannes Berg013290a2014-08-04 13:38:48 +0200604int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
605{
606 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
607 u32 qmask;
608
609 lockdep_assert_held(&mvm->mutex);
610
611 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
612
613 /*
614 * The firmware defines the TFD queue mask to only be relevant
615 * for *unicast* queues, so the multicast (CAB) queue shouldn't
616 * be included.
617 */
618 if (vif->type == NL80211_IFTYPE_AP)
619 qmask &= ~BIT(vif->cab_queue);
620
621 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
622 ieee80211_vif_type_p2p(vif));
623}
624
Johannes Berg8ca151b2013-01-24 14:25:36 +0100625/* Allocate a new station entry for the broadcast station to the given vif,
626 * and send it to the FW.
627 * Note that each P2P mac should have its own broadcast station.
628 *
629 * @mvm: the mvm component
630 * @vif: the interface to which the broadcast station is added
631 * @bsta: the broadcast station to add. */
Johannes Berg013290a2014-08-04 13:38:48 +0200632int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100633{
634 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +0200635 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100636 int ret;
637
638 lockdep_assert_held(&mvm->mutex);
639
Johannes Berg013290a2014-08-04 13:38:48 +0200640 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100641 if (ret)
642 return ret;
643
Johannes Berg013290a2014-08-04 13:38:48 +0200644 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100645
646 if (ret)
647 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +0200648
Johannes Berg8ca151b2013-01-24 14:25:36 +0100649 return ret;
650}
651
Johannes Berg013290a2014-08-04 13:38:48 +0200652void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
653{
654 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
655
656 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
657}
658
Johannes Berg8ca151b2013-01-24 14:25:36 +0100659/*
660 * Send the FW a request to remove the station from it's internal data
661 * structures, and in addition remove it from the local data structure.
662 */
Johannes Berg013290a2014-08-04 13:38:48 +0200663int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100664{
665 int ret;
666
667 lockdep_assert_held(&mvm->mutex);
668
Johannes Berg013290a2014-08-04 13:38:48 +0200669 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100670
Johannes Berg013290a2014-08-04 13:38:48 +0200671 iwl_mvm_dealloc_bcast_sta(mvm, vif);
672
Johannes Berg8ca151b2013-01-24 14:25:36 +0100673 return ret;
674}
675
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300676#define IWL_MAX_RX_BA_SESSIONS 16
677
Johannes Berg8ca151b2013-01-24 14:25:36 +0100678int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
679 int tid, u16 ssn, bool start)
680{
681 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300682 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100683 int ret;
684 u32 status;
685
686 lockdep_assert_held(&mvm->mutex);
687
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300688 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
689 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
690 return -ENOSPC;
691 }
692
Johannes Berg8ca151b2013-01-24 14:25:36 +0100693 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
694 cmd.sta_id = mvm_sta->sta_id;
695 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +0300696 if (start) {
697 cmd.add_immediate_ba_tid = (u8) tid;
698 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
699 } else {
700 cmd.remove_immediate_ba_tid = (u8) tid;
701 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100702 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
703 STA_MODIFY_REMOVE_BA_TID;
704
705 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300706 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
707 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100708 if (ret)
709 return ret;
710
711 switch (status) {
712 case ADD_STA_SUCCESS:
713 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
714 start ? "start" : "stopp");
715 break;
716 case ADD_STA_IMMEDIATE_BA_FAILURE:
717 IWL_WARN(mvm, "RX BA Session refused by fw\n");
718 ret = -ENOSPC;
719 break;
720 default:
721 ret = -EIO;
722 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
723 start ? "start" : "stopp", status);
724 break;
725 }
726
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300727 if (!ret) {
728 if (start)
729 mvm->rx_ba_sessions++;
730 else if (mvm->rx_ba_sessions > 0)
731 /* check that restart flow didn't zero the counter */
732 mvm->rx_ba_sessions--;
733 }
734
Johannes Berg8ca151b2013-01-24 14:25:36 +0100735 return ret;
736}
737
738static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
739 int tid, u8 queue, bool start)
740{
741 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300742 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100743 int ret;
744 u32 status;
745
746 lockdep_assert_held(&mvm->mutex);
747
748 if (start) {
749 mvm_sta->tfd_queue_msk |= BIT(queue);
750 mvm_sta->tid_disable_agg &= ~BIT(tid);
751 } else {
752 mvm_sta->tfd_queue_msk &= ~BIT(queue);
753 mvm_sta->tid_disable_agg |= BIT(tid);
754 }
755
756 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
757 cmd.sta_id = mvm_sta->sta_id;
758 cmd.add_modify = STA_MODE_MODIFY;
759 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
760 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
761 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
762
763 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300764 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
765 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100766 if (ret)
767 return ret;
768
769 switch (status) {
770 case ADD_STA_SUCCESS:
771 break;
772 default:
773 ret = -EIO;
774 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
775 start ? "start" : "stopp", status);
776 break;
777 }
778
779 return ret;
780}
781
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +0200782const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100783 IEEE80211_AC_BE,
784 IEEE80211_AC_BK,
785 IEEE80211_AC_BK,
786 IEEE80211_AC_BE,
787 IEEE80211_AC_VI,
788 IEEE80211_AC_VI,
789 IEEE80211_AC_VO,
790 IEEE80211_AC_VO,
791};
792
Johannes Berg3e56ead2013-02-15 22:23:18 +0100793static const u8 tid_to_ucode_ac[] = {
794 AC_BE,
795 AC_BK,
796 AC_BK,
797 AC_BE,
798 AC_VI,
799 AC_VI,
800 AC_VO,
801 AC_VO,
802};
803
Johannes Berg8ca151b2013-01-24 14:25:36 +0100804int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
805 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
806{
Johannes Berg5b577a92013-11-14 18:20:04 +0100807 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100808 struct iwl_mvm_tid_data *tid_data;
809 int txq_id;
810
811 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
812 return -EINVAL;
813
814 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
815 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
816 mvmsta->tid_data[tid].state);
817 return -ENXIO;
818 }
819
820 lockdep_assert_held(&mvm->mutex);
821
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200822 for (txq_id = mvm->first_agg_queue;
823 txq_id <= mvm->last_agg_queue; txq_id++)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100824 if (mvm->queue_to_mac80211[txq_id] ==
825 IWL_INVALID_MAC80211_QUEUE)
826 break;
827
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200828 if (txq_id > mvm->last_agg_queue) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100829 IWL_ERR(mvm, "Failed to allocate agg queue\n");
830 return -EIO;
831 }
832
Arik Nemtsovb2492502014-03-13 12:21:50 +0200833 spin_lock_bh(&mvmsta->lock);
834
835 /* possible race condition - we entered D0i3 while starting agg */
836 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
837 spin_unlock_bh(&mvmsta->lock);
838 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
839 return -EIO;
840 }
841
Johannes Berg8ca151b2013-01-24 14:25:36 +0100842 /* the new tx queue is still connected to the same mac80211 queue */
Johannes Berg3e56ead2013-02-15 22:23:18 +0100843 mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +0100844
Johannes Berg8ca151b2013-01-24 14:25:36 +0100845 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +0100846 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100847 tid_data->txq_id = txq_id;
848 *ssn = tid_data->ssn;
849
850 IWL_DEBUG_TX_QUEUES(mvm,
851 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
852 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
853 tid_data->next_reclaimed);
854
855 if (tid_data->ssn == tid_data->next_reclaimed) {
856 tid_data->state = IWL_AGG_STARTING;
857 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
858 } else {
859 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
860 }
861
862 spin_unlock_bh(&mvmsta->lock);
863
864 return 0;
865}
866
867int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
868 struct ieee80211_sta *sta, u16 tid, u8 buf_size)
869{
Johannes Berg5b577a92013-11-14 18:20:04 +0100870 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100871 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
872 int queue, fifo, ret;
873 u16 ssn;
874
875 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
876
877 spin_lock_bh(&mvmsta->lock);
878 ssn = tid_data->ssn;
879 queue = tid_data->txq_id;
880 tid_data->state = IWL_AGG_ON;
881 tid_data->ssn = 0xffff;
882 spin_unlock_bh(&mvmsta->lock);
883
Johannes Berg3e56ead2013-02-15 22:23:18 +0100884 fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +0100885
886 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
887 if (ret)
888 return -EIO;
889
890 iwl_trans_txq_enable(mvm->trans, queue, fifo, mvmsta->sta_id, tid,
891 buf_size, ssn);
892
893 /*
894 * Even though in theory the peer could have different
895 * aggregation reorder buffer sizes for different sessions,
896 * our ucode doesn't allow for that and has a global limit
897 * for each station. Therefore, use the minimum of all the
898 * aggregation sessions and our default value.
899 */
900 mvmsta->max_agg_bufsize =
901 min(mvmsta->max_agg_bufsize, buf_size);
902 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
903
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300904 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
905 sta->addr, tid);
906
Eyal Shapira9e680942013-11-09 00:16:16 +0200907 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100908}
909
910int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
911 struct ieee80211_sta *sta, u16 tid)
912{
Johannes Berg5b577a92013-11-14 18:20:04 +0100913 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100914 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
915 u16 txq_id;
916 int err;
917
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +0200918
919 /*
920 * If mac80211 is cleaning its state, then say that we finished since
921 * our state has been cleared anyway.
922 */
923 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
924 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
925 return 0;
926 }
927
Johannes Berg8ca151b2013-01-24 14:25:36 +0100928 spin_lock_bh(&mvmsta->lock);
929
930 txq_id = tid_data->txq_id;
931
932 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
933 mvmsta->sta_id, tid, txq_id, tid_data->state);
934
935 switch (tid_data->state) {
936 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +0100937 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100938
939 IWL_DEBUG_TX_QUEUES(mvm,
940 "ssn = %d, next_recl = %d\n",
941 tid_data->ssn, tid_data->next_reclaimed);
942
943 /* There are still packets for this RA / TID in the HW */
944 if (tid_data->ssn != tid_data->next_reclaimed) {
945 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
946 err = 0;
947 break;
948 }
949
950 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +0200951 tid_data->state = IWL_AGG_OFF;
952 mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
953 spin_unlock_bh(&mvmsta->lock);
954
955 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
956
957 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
958
Johannes Bergd4578ea2014-08-01 12:17:40 +0200959 iwl_trans_txq_disable(mvm->trans, txq_id, true);
Johannes Bergf7f89e72014-08-05 15:24:44 +0200960 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100961 case IWL_AGG_STARTING:
962 case IWL_EMPTYING_HW_QUEUE_ADDBA:
963 /*
964 * The agg session has been stopped before it was set up. This
965 * can happen when the AddBA timer times out for example.
966 */
967
968 /* No barriers since we are under mutex */
969 lockdep_assert_held(&mvm->mutex);
970 mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
971
972 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
973 tid_data->state = IWL_AGG_OFF;
974 err = 0;
975 break;
976 default:
977 IWL_ERR(mvm,
978 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
979 mvmsta->sta_id, tid, tid_data->state);
980 IWL_ERR(mvm,
981 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
982 err = -EINVAL;
983 }
984
985 spin_unlock_bh(&mvmsta->lock);
986
987 return err;
988}
989
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +0200990int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
991 struct ieee80211_sta *sta, u16 tid)
992{
Johannes Berg5b577a92013-11-14 18:20:04 +0100993 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +0200994 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
995 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +0200996 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +0200997
998 /*
999 * First set the agg state to OFF to avoid calling
1000 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
1001 */
1002 spin_lock_bh(&mvmsta->lock);
1003 txq_id = tid_data->txq_id;
1004 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
1005 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001006 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001007 tid_data->state = IWL_AGG_OFF;
1008 spin_unlock_bh(&mvmsta->lock);
1009
Johannes Bergb6658ff2013-07-24 13:55:51 +02001010 if (old_state >= IWL_AGG_ON) {
1011 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true))
1012 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001013
Johannes Bergf7f89e72014-08-05 15:24:44 +02001014 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1015
Johannes Bergd4578ea2014-08-01 12:17:40 +02001016 iwl_trans_txq_disable(mvm->trans, tid_data->txq_id, true);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001017 }
1018
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001019 mvm->queue_to_mac80211[tid_data->txq_id] =
1020 IWL_INVALID_MAC80211_QUEUE;
1021
1022 return 0;
1023}
1024
Johannes Berg8ca151b2013-01-24 14:25:36 +01001025static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
1026{
1027 int i;
1028
1029 lockdep_assert_held(&mvm->mutex);
1030
1031 i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM);
1032
1033 if (i == STA_KEY_MAX_NUM)
1034 return STA_KEY_IDX_INVALID;
1035
1036 __set_bit(i, mvm->fw_key_table);
1037
1038 return i;
1039}
1040
1041static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
1042 struct ieee80211_sta *sta)
1043{
1044 struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
1045
1046 if (sta) {
1047 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1048
1049 return mvm_sta->sta_id;
1050 }
1051
1052 /*
1053 * The device expects GTKs for station interfaces to be
1054 * installed as GTKs for the AP station. If we have no
1055 * station ID, then use AP's station ID.
1056 */
1057 if (vif->type == NL80211_IFTYPE_STATION &&
1058 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
1059 return mvmvif->ap_sta_id;
1060
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001061 return IWL_MVM_STATION_COUNT;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001062}
1063
1064static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
1065 struct iwl_mvm_sta *mvm_sta,
1066 struct ieee80211_key_conf *keyconf,
1067 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1068 u32 cmd_flags)
1069{
Max Stepanov5a258aa2013-04-07 09:11:21 +03001070 struct iwl_mvm_add_sta_key_cmd cmd = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001071 __le16 key_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001072 int ret, status;
1073 u16 keyidx;
1074 int i;
1075
1076 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1077 STA_KEY_FLG_KEYID_MSK;
1078 key_flags = cpu_to_le16(keyidx);
1079 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
1080
1081 switch (keyconf->cipher) {
1082 case WLAN_CIPHER_SUITE_TKIP:
1083 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001084 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001085 for (i = 0; i < 5; i++)
Max Stepanov5a258aa2013-04-07 09:11:21 +03001086 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1087 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001088 break;
1089 case WLAN_CIPHER_SUITE_CCMP:
1090 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001091 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001092 break;
1093 default:
Max Stepanove36e5432013-08-27 19:56:13 +03001094 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
1095 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001096 }
1097
1098 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1099 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1100
Max Stepanov5a258aa2013-04-07 09:11:21 +03001101 cmd.key_offset = keyconf->hw_key_idx;
1102 cmd.key_flags = key_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001103 cmd.sta_id = sta_id;
1104
1105 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001106 if (cmd_flags & CMD_ASYNC)
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001107 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
1108 sizeof(cmd), &cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001109 else
1110 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1111 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001112
1113 switch (status) {
1114 case ADD_STA_SUCCESS:
1115 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1116 break;
1117 default:
1118 ret = -EIO;
1119 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1120 break;
1121 }
1122
1123 return ret;
1124}
1125
1126static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1127 struct ieee80211_key_conf *keyconf,
1128 u8 sta_id, bool remove_key)
1129{
1130 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1131
1132 /* verify the key details match the required command's expectations */
1133 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1134 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1135 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1136 return -EINVAL;
1137
1138 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1139 igtk_cmd.sta_id = cpu_to_le32(sta_id);
1140
1141 if (remove_key) {
1142 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1143 } else {
1144 struct ieee80211_key_seq seq;
1145 const u8 *pn;
1146
1147 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
1148 ieee80211_aes_cmac_calculate_k1_k2(keyconf,
1149 igtk_cmd.K1, igtk_cmd.K2);
1150 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1151 pn = seq.aes_cmac.pn;
1152 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1153 ((u64) pn[4] << 8) |
1154 ((u64) pn[3] << 16) |
1155 ((u64) pn[2] << 24) |
1156 ((u64) pn[1] << 32) |
1157 ((u64) pn[0] << 40));
1158 }
1159
1160 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1161 remove_key ? "removing" : "installing",
1162 igtk_cmd.sta_id);
1163
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001164 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001165 sizeof(igtk_cmd), &igtk_cmd);
1166}
1167
1168
1169static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1170 struct ieee80211_vif *vif,
1171 struct ieee80211_sta *sta)
1172{
1173 struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
1174
1175 if (sta)
1176 return sta->addr;
1177
1178 if (vif->type == NL80211_IFTYPE_STATION &&
1179 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1180 u8 sta_id = mvmvif->ap_sta_id;
1181 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1182 lockdep_is_held(&mvm->mutex));
1183 return sta->addr;
1184 }
1185
1186
1187 return NULL;
1188}
1189
1190int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1191 struct ieee80211_vif *vif,
1192 struct ieee80211_sta *sta,
1193 struct ieee80211_key_conf *keyconf,
1194 bool have_key_offset)
1195{
1196 struct iwl_mvm_sta *mvm_sta;
1197 int ret;
1198 u8 *addr, sta_id;
1199 struct ieee80211_key_seq seq;
1200 u16 p1k[5];
1201
1202 lockdep_assert_held(&mvm->mutex);
1203
1204 /* Get the station id from the mvm local station table */
1205 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001206 if (sta_id == IWL_MVM_STATION_COUNT) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001207 IWL_ERR(mvm, "Failed to find station id\n");
1208 return -EINVAL;
1209 }
1210
1211 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1212 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1213 goto end;
1214 }
1215
1216 /*
1217 * It is possible that the 'sta' parameter is NULL, and thus
1218 * there is a need to retrieve the sta from the local station table.
1219 */
1220 if (!sta) {
1221 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1222 lockdep_is_held(&mvm->mutex));
1223 if (IS_ERR_OR_NULL(sta)) {
1224 IWL_ERR(mvm, "Invalid station id\n");
1225 return -EINVAL;
1226 }
1227 }
1228
1229 mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1230 if (WARN_ON_ONCE(mvm_sta->vif != vif))
1231 return -EINVAL;
1232
1233 if (!have_key_offset) {
1234 /*
1235 * The D3 firmware hardcodes the PTK offset to 0, so we have to
1236 * configure it there. As a result, this workaround exists to
1237 * let the caller set the key offset (hw_key_idx), see d3.c.
1238 */
1239 keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
1240 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1241 return -ENOSPC;
1242 }
1243
1244 switch (keyconf->cipher) {
1245 case WLAN_CIPHER_SUITE_TKIP:
1246 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1247 /* get phase 1 key from mac80211 */
1248 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1249 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1250 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001251 seq.tkip.iv32, p1k, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001252 break;
1253 case WLAN_CIPHER_SUITE_CCMP:
1254 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001255 0, NULL, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001256 break;
1257 default:
Max Stepanove36e5432013-08-27 19:56:13 +03001258 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001259 sta_id, 0, NULL, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001260 }
1261
1262 if (ret)
1263 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1264
1265end:
1266 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1267 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1268 sta->addr, ret);
1269 return ret;
1270}
1271
1272int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1273 struct ieee80211_vif *vif,
1274 struct ieee80211_sta *sta,
1275 struct ieee80211_key_conf *keyconf)
1276{
1277 struct iwl_mvm_sta *mvm_sta;
Max Stepanov5a258aa2013-04-07 09:11:21 +03001278 struct iwl_mvm_add_sta_key_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001279 __le16 key_flags;
1280 int ret, status;
1281 u8 sta_id;
1282
1283 lockdep_assert_held(&mvm->mutex);
1284
1285 /* Get the station id from the mvm local station table */
1286 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1287
1288 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1289 keyconf->keyidx, sta_id);
1290
1291 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1292 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1293
1294 ret = __test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1295 if (!ret) {
1296 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1297 keyconf->hw_key_idx);
1298 return -ENOENT;
1299 }
1300
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001301 if (sta_id == IWL_MVM_STATION_COUNT) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001302 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1303 return 0;
1304 }
1305
1306 /*
1307 * It is possible that the 'sta' parameter is NULL, and thus
1308 * there is a need to retrieve the sta from the local station table,
1309 * for example when a GTK is removed (where the sta_id will then be
1310 * the AP ID, and no station was passed by mac80211.)
1311 */
1312 if (!sta) {
1313 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1314 lockdep_is_held(&mvm->mutex));
1315 if (!sta) {
1316 IWL_ERR(mvm, "Invalid station id\n");
1317 return -EINVAL;
1318 }
1319 }
1320
1321 mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1322 if (WARN_ON_ONCE(mvm_sta->vif != vif))
1323 return -EINVAL;
1324
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02001325 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1326 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001327 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1328 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1329
1330 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1331 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1332
Max Stepanov5a258aa2013-04-07 09:11:21 +03001333 cmd.key_flags = key_flags;
1334 cmd.key_offset = keyconf->hw_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001335 cmd.sta_id = sta_id;
1336
Johannes Berg8ca151b2013-01-24 14:25:36 +01001337 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001338 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1339 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001340
1341 switch (status) {
1342 case ADD_STA_SUCCESS:
1343 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1344 break;
1345 default:
1346 ret = -EIO;
1347 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1348 break;
1349 }
1350
1351 return ret;
1352}
1353
1354void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1355 struct ieee80211_vif *vif,
1356 struct ieee80211_key_conf *keyconf,
1357 struct ieee80211_sta *sta, u32 iv32,
1358 u16 *phase1key)
1359{
Beni Levc3eb5362013-02-06 17:22:18 +02001360 struct iwl_mvm_sta *mvm_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001361 u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1362
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001363 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001364 return;
1365
Beni Levc3eb5362013-02-06 17:22:18 +02001366 rcu_read_lock();
1367
1368 if (!sta) {
1369 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1370 if (WARN_ON(IS_ERR_OR_NULL(sta))) {
1371 rcu_read_unlock();
1372 return;
1373 }
1374 }
1375
1376 mvm_sta = (void *)sta->drv_priv;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001377 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1378 iv32, phase1key, CMD_ASYNC);
Beni Levc3eb5362013-02-06 17:22:18 +02001379 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01001380}
1381
Johannes Berg9cc40712013-02-15 22:47:48 +01001382void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
1383 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001384{
Johannes Berg5b577a92013-11-14 18:20:04 +01001385 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001386 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001387 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01001388 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03001389 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01001390 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01001391 };
1392 int ret;
1393
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001394 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001395 if (ret)
1396 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1397}
1398
Johannes Berg9cc40712013-02-15 22:47:48 +01001399void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
1400 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001401 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01001402 u16 cnt, u16 tids, bool more_data,
1403 bool agg)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001404{
Johannes Berg5b577a92013-11-14 18:20:04 +01001405 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001406 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001407 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01001408 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001409 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1410 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01001411 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01001412 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01001413 int tid, ret;
1414 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001415
Johannes Berg3e56ead2013-02-15 22:23:18 +01001416 /* convert TIDs to ACs - we don't support TSPEC so that's OK
1417 * Note that this field is reserved and unused by firmware not
1418 * supporting GO uAPSD, so it's safe to always do this.
1419 */
1420 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
1421 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
1422
1423 /* If we're releasing frames from aggregation queues then check if the
1424 * all queues combined that we're releasing frames from have
1425 * - more frames than the service period, in which case more_data
1426 * needs to be set
1427 * - fewer than 'cnt' frames, in which case we need to adjust the
1428 * firmware command (but do that unconditionally)
1429 */
1430 if (agg) {
1431 int remaining = cnt;
1432
1433 spin_lock_bh(&mvmsta->lock);
1434 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
1435 struct iwl_mvm_tid_data *tid_data;
1436 u16 n_queued;
1437
1438 tid_data = &mvmsta->tid_data[tid];
1439 if (WARN(tid_data->state != IWL_AGG_ON &&
1440 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
1441 "TID %d state is %d\n",
1442 tid, tid_data->state)) {
1443 spin_unlock_bh(&mvmsta->lock);
1444 ieee80211_sta_eosp(sta);
1445 return;
1446 }
1447
1448 n_queued = iwl_mvm_tid_queued(tid_data);
1449 if (n_queued > remaining) {
1450 more_data = true;
1451 remaining = 0;
1452 break;
1453 }
1454 remaining -= n_queued;
1455 }
1456 spin_unlock_bh(&mvmsta->lock);
1457
1458 cmd.sleep_tx_count = cpu_to_le16(cnt - remaining);
1459 if (WARN_ON(cnt - remaining == 0)) {
1460 ieee80211_sta_eosp(sta);
1461 return;
1462 }
1463 }
1464
1465 /* Note: this is ignored by firmware not supporting GO uAPSD */
1466 if (more_data)
1467 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
1468
1469 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
1470 mvmsta->next_status_eosp = true;
1471 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
1472 } else {
1473 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
1474 }
1475
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001476 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001477 if (ret)
1478 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1479}
Johannes Berg3e56ead2013-02-15 22:23:18 +01001480
1481int iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
1482 struct iwl_rx_cmd_buffer *rxb,
1483 struct iwl_device_cmd *cmd)
1484{
1485 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1486 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
1487 struct ieee80211_sta *sta;
1488 u32 sta_id = le32_to_cpu(notif->sta_id);
1489
1490 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
1491 return 0;
1492
1493 rcu_read_lock();
1494 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1495 if (!IS_ERR_OR_NULL(sta))
1496 ieee80211_sta_eosp(sta);
1497 rcu_read_unlock();
1498
1499 return 0;
1500}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03001501
1502void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
1503 struct iwl_mvm_sta *mvmsta, bool disable)
1504{
1505 struct iwl_mvm_add_sta_cmd cmd = {
1506 .add_modify = STA_MODE_MODIFY,
1507 .sta_id = mvmsta->sta_id,
1508 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
1509 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
1510 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1511 };
1512 int ret;
1513
1514 if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_DISABLE_STA_TX))
1515 return;
1516
1517 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1518 if (ret)
1519 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1520}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001521
1522void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
1523 struct ieee80211_sta *sta,
1524 bool disable)
1525{
1526 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1527
1528 spin_lock_bh(&mvm_sta->lock);
1529
1530 if (mvm_sta->disable_tx == disable) {
1531 spin_unlock_bh(&mvm_sta->lock);
1532 return;
1533 }
1534
1535 mvm_sta->disable_tx = disable;
1536
1537 /*
1538 * Tell mac80211 to start/stop queueing tx for this station,
1539 * but don't stop queueing if there are still pending frames
1540 * for this station.
1541 */
1542 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
1543 ieee80211_sta_block_awake(mvm->hw, sta, disable);
1544
1545 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
1546
1547 spin_unlock_bh(&mvm_sta->lock);
1548}
1549
1550void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
1551 struct iwl_mvm_vif *mvmvif,
1552 bool disable)
1553{
1554 struct ieee80211_sta *sta;
1555 struct iwl_mvm_sta *mvm_sta;
1556 int i;
1557
1558 lockdep_assert_held(&mvm->mutex);
1559
1560 /* Block/unblock all the stations of the given mvmvif */
1561 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
1562 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
1563 lockdep_is_held(&mvm->mutex));
1564 if (IS_ERR_OR_NULL(sta))
1565 continue;
1566
1567 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1568 if (mvm_sta->mac_id_n_color !=
1569 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
1570 continue;
1571
1572 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
1573 }
1574}