blob: 075500c41ecbf29dc3d8efe0381b00e47e3fea24 [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 Berg8ca151b2013-01-24 14:25:36 +01009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020025 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010026 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020033 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8ca151b2013-01-24 14:25:36 +010034 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *
62 *****************************************************************************/
63#include <net/mac80211.h>
64
65#include "mvm.h"
66#include "sta.h"
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +030067#include "rs.h"
Johannes Berg8ca151b2013-01-24 14:25:36 +010068
Johannes Berg3e56ead2013-02-15 22:23:18 +010069static void iwl_mvm_add_sta_cmd_v7_to_v5(struct iwl_mvm_add_sta_cmd_v7 *cmd_v7,
Max Stepanov5a258aa2013-04-07 09:11:21 +030070 struct iwl_mvm_add_sta_cmd_v5 *cmd_v5)
71{
72 memset(cmd_v5, 0, sizeof(*cmd_v5));
73
Johannes Berg3e56ead2013-02-15 22:23:18 +010074 cmd_v5->add_modify = cmd_v7->add_modify;
75 cmd_v5->tid_disable_tx = cmd_v7->tid_disable_tx;
76 cmd_v5->mac_id_n_color = cmd_v7->mac_id_n_color;
77 memcpy(cmd_v5->addr, cmd_v7->addr, ETH_ALEN);
78 cmd_v5->sta_id = cmd_v7->sta_id;
79 cmd_v5->modify_mask = cmd_v7->modify_mask;
80 cmd_v5->station_flags = cmd_v7->station_flags;
81 cmd_v5->station_flags_msk = cmd_v7->station_flags_msk;
82 cmd_v5->add_immediate_ba_tid = cmd_v7->add_immediate_ba_tid;
83 cmd_v5->remove_immediate_ba_tid = cmd_v7->remove_immediate_ba_tid;
84 cmd_v5->add_immediate_ba_ssn = cmd_v7->add_immediate_ba_ssn;
85 cmd_v5->sleep_tx_count = cmd_v7->sleep_tx_count;
86 cmd_v5->sleep_state_flags = cmd_v7->sleep_state_flags;
87 cmd_v5->assoc_id = cmd_v7->assoc_id;
88 cmd_v5->beamform_flags = cmd_v7->beamform_flags;
89 cmd_v5->tfd_queue_msk = cmd_v7->tfd_queue_msk;
Max Stepanov5a258aa2013-04-07 09:11:21 +030090}
91
92static void
93iwl_mvm_add_sta_key_to_add_sta_cmd_v5(struct iwl_mvm_add_sta_key_cmd *key_cmd,
94 struct iwl_mvm_add_sta_cmd_v5 *sta_cmd,
95 u32 mac_id_n_color)
96{
97 memset(sta_cmd, 0, sizeof(*sta_cmd));
98
99 sta_cmd->sta_id = key_cmd->sta_id;
100 sta_cmd->add_modify = STA_MODE_MODIFY;
101 sta_cmd->modify_mask = STA_MODIFY_KEY;
102 sta_cmd->mac_id_n_color = cpu_to_le32(mac_id_n_color);
103
104 sta_cmd->key.key_offset = key_cmd->key_offset;
105 sta_cmd->key.key_flags = key_cmd->key_flags;
106 memcpy(sta_cmd->key.key, key_cmd->key, sizeof(sta_cmd->key.key));
107 sta_cmd->key.tkip_rx_tsc_byte2 = key_cmd->tkip_rx_tsc_byte2;
108 memcpy(sta_cmd->key.tkip_rx_ttak, key_cmd->tkip_rx_ttak,
109 sizeof(sta_cmd->key.tkip_rx_ttak));
110}
111
112static int iwl_mvm_send_add_sta_cmd_status(struct iwl_mvm *mvm,
Johannes Berg3e56ead2013-02-15 22:23:18 +0100113 struct iwl_mvm_add_sta_cmd_v7 *cmd,
Max Stepanov5a258aa2013-04-07 09:11:21 +0300114 int *status)
115{
116 struct iwl_mvm_add_sta_cmd_v5 cmd_v5;
117
118 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_STA_KEY_CMD)
119 return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(*cmd),
120 cmd, status);
121
Johannes Berg3e56ead2013-02-15 22:23:18 +0100122 iwl_mvm_add_sta_cmd_v7_to_v5(cmd, &cmd_v5);
Max Stepanov5a258aa2013-04-07 09:11:21 +0300123
124 return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd_v5),
125 &cmd_v5, status);
126}
127
128static int iwl_mvm_send_add_sta_cmd(struct iwl_mvm *mvm, u32 flags,
Johannes Berg3e56ead2013-02-15 22:23:18 +0100129 struct iwl_mvm_add_sta_cmd_v7 *cmd)
Max Stepanov5a258aa2013-04-07 09:11:21 +0300130{
131 struct iwl_mvm_add_sta_cmd_v5 cmd_v5;
132
133 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_STA_KEY_CMD)
134 return iwl_mvm_send_cmd_pdu(mvm, ADD_STA, flags,
135 sizeof(*cmd), cmd);
136
Johannes Berg3e56ead2013-02-15 22:23:18 +0100137 iwl_mvm_add_sta_cmd_v7_to_v5(cmd, &cmd_v5);
Max Stepanov5a258aa2013-04-07 09:11:21 +0300138
139 return iwl_mvm_send_cmd_pdu(mvm, ADD_STA, flags, sizeof(cmd_v5),
140 &cmd_v5);
141}
142
143static int
144iwl_mvm_send_add_sta_key_cmd_status(struct iwl_mvm *mvm,
145 struct iwl_mvm_add_sta_key_cmd *cmd,
146 u32 mac_id_n_color,
147 int *status)
148{
149 struct iwl_mvm_add_sta_cmd_v5 sta_cmd;
150
151 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_STA_KEY_CMD)
152 return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY,
153 sizeof(*cmd), cmd, status);
154
155 iwl_mvm_add_sta_key_to_add_sta_cmd_v5(cmd, &sta_cmd, mac_id_n_color);
156
157 return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(sta_cmd),
158 &sta_cmd, status);
159}
160
161static int iwl_mvm_send_add_sta_key_cmd(struct iwl_mvm *mvm,
162 u32 flags,
163 struct iwl_mvm_add_sta_key_cmd *cmd,
164 u32 mac_id_n_color)
165{
166 struct iwl_mvm_add_sta_cmd_v5 sta_cmd;
167
168 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_STA_KEY_CMD)
169 return iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, flags,
170 sizeof(*cmd), cmd);
171
172 iwl_mvm_add_sta_key_to_add_sta_cmd_v5(cmd, &sta_cmd, mac_id_n_color);
173
174 return iwl_mvm_send_cmd_pdu(mvm, ADD_STA, flags, sizeof(sta_cmd),
175 &sta_cmd);
176}
177
Eliad Pellerb92e6612014-01-23 17:58:23 +0200178static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
179 enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100180{
181 int sta_id;
Eliad Pellerb92e6612014-01-23 17:58:23 +0200182 u32 reserved_ids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100183
Eliad Pellerb92e6612014-01-23 17:58:23 +0200184 BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100185 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
186
187 lockdep_assert_held(&mvm->mutex);
188
Eliad Pellerb92e6612014-01-23 17:58:23 +0200189 /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
190 if (iftype != NL80211_IFTYPE_STATION)
191 reserved_ids = BIT(0);
192
Johannes Berg8ca151b2013-01-24 14:25:36 +0100193 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
Eliad Pellerb92e6612014-01-23 17:58:23 +0200194 for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++) {
195 if (BIT(sta_id) & reserved_ids)
196 continue;
197
Johannes Berg8ca151b2013-01-24 14:25:36 +0100198 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
199 lockdep_is_held(&mvm->mutex)))
200 return sta_id;
Eliad Pellerb92e6612014-01-23 17:58:23 +0200201 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100202 return IWL_MVM_STATION_COUNT;
203}
204
Johannes Berg7a453972013-02-12 13:10:44 +0100205/* send station add/update command to firmware */
206int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
207 bool update)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100208{
209 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
Johannes Berg3e56ead2013-02-15 22:23:18 +0100210 struct iwl_mvm_add_sta_cmd_v7 add_sta_cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100211 int ret;
212 u32 status;
213 u32 agg_size = 0, mpdu_dens = 0;
214
215 memset(&add_sta_cmd, 0, sizeof(add_sta_cmd));
216
217 add_sta_cmd.sta_id = mvm_sta->sta_id;
218 add_sta_cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
Johannes Berg7a453972013-02-12 13:10:44 +0100219 if (!update) {
220 add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
221 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
222 }
223 add_sta_cmd.add_modify = update ? 1 : 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100224
Johannes Berg5bc5aaa2013-02-12 14:35:36 +0100225 add_sta_cmd.station_flags_msk |= cpu_to_le32(STA_FLG_FAT_EN_MSK |
226 STA_FLG_MIMO_EN_MSK);
227
228 switch (sta->bandwidth) {
229 case IEEE80211_STA_RX_BW_160:
230 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
231 /* fall through */
232 case IEEE80211_STA_RX_BW_80:
233 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
234 /* fall through */
235 case IEEE80211_STA_RX_BW_40:
236 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
237 /* fall through */
238 case IEEE80211_STA_RX_BW_20:
239 if (sta->ht_cap.ht_supported)
240 add_sta_cmd.station_flags |=
241 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
242 break;
243 }
244
245 switch (sta->rx_nss) {
246 case 1:
247 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
248 break;
249 case 2:
250 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
251 break;
252 case 3 ... 8:
253 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
254 break;
255 }
256
257 switch (sta->smps_mode) {
258 case IEEE80211_SMPS_AUTOMATIC:
259 case IEEE80211_SMPS_NUM_MODES:
260 WARN_ON(1);
261 break;
262 case IEEE80211_SMPS_STATIC:
263 /* override NSS */
264 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
265 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
266 break;
267 case IEEE80211_SMPS_DYNAMIC:
268 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
269 break;
270 case IEEE80211_SMPS_OFF:
271 /* nothing */
272 break;
273 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100274
275 if (sta->ht_cap.ht_supported) {
276 add_sta_cmd.station_flags_msk |=
277 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
278 STA_FLG_AGG_MPDU_DENS_MSK);
279
280 mpdu_dens = sta->ht_cap.ampdu_density;
281 }
282
283 if (sta->vht_cap.vht_supported) {
284 agg_size = sta->vht_cap.cap &
285 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
286 agg_size >>=
287 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
288 } else if (sta->ht_cap.ht_supported) {
289 agg_size = sta->ht_cap.ampdu_factor;
290 }
291
292 add_sta_cmd.station_flags |=
293 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
294 add_sta_cmd.station_flags |=
295 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
296
297 status = ADD_STA_SUCCESS;
Max Stepanov5a258aa2013-04-07 09:11:21 +0300298 ret = iwl_mvm_send_add_sta_cmd_status(mvm, &add_sta_cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100299 if (ret)
300 return ret;
301
302 switch (status) {
303 case ADD_STA_SUCCESS:
304 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
305 break;
306 default:
307 ret = -EIO;
308 IWL_ERR(mvm, "ADD_STA failed\n");
309 break;
310 }
311
312 return ret;
313}
314
315int iwl_mvm_add_sta(struct iwl_mvm *mvm,
316 struct ieee80211_vif *vif,
317 struct ieee80211_sta *sta)
318{
319 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
320 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
321 int i, ret, sta_id;
322
323 lockdep_assert_held(&mvm->mutex);
324
325 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
Eliad Pellerb92e6612014-01-23 17:58:23 +0200326 sta_id = iwl_mvm_find_free_sta_id(mvm,
327 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100328 else
329 sta_id = mvm_sta->sta_id;
330
331 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
332 return -ENOSPC;
333
334 spin_lock_init(&mvm_sta->lock);
335
336 mvm_sta->sta_id = sta_id;
337 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
338 mvmvif->color);
339 mvm_sta->vif = vif;
340 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300341 mvm_sta->tx_protection = 0;
342 mvm_sta->tt_tx_protection = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100343
344 /* HW restart, don't assume the memory has been zeroed */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300345 atomic_set(&mvm->pending_frames[sta_id], 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100346 mvm_sta->tid_disable_agg = 0;
347 mvm_sta->tfd_queue_msk = 0;
348 for (i = 0; i < IEEE80211_NUM_ACS; i++)
349 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
350 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
351
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200352 /* for HW restart - reset everything but the sequence number */
353 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
354 u16 seq = mvm_sta->tid_data[i].seq_number;
355 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
356 mvm_sta->tid_data[i].seq_number = seq;
357 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100358
Johannes Berg7a453972013-02-12 13:10:44 +0100359 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100360 if (ret)
361 return ret;
362
363 /* The first station added is the AP, the others are TDLS STAs */
364 if (vif->type == NL80211_IFTYPE_STATION &&
365 mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
366 mvmvif->ap_sta_id = sta_id;
367
368 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
369
370 return 0;
371}
372
Johannes Berg7a453972013-02-12 13:10:44 +0100373int iwl_mvm_update_sta(struct iwl_mvm *mvm,
374 struct ieee80211_vif *vif,
375 struct ieee80211_sta *sta)
376{
377 return iwl_mvm_sta_send_to_fw(mvm, sta, true);
378}
379
Johannes Berg8ca151b2013-01-24 14:25:36 +0100380int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
381 bool drain)
382{
Johannes Berg3e56ead2013-02-15 22:23:18 +0100383 struct iwl_mvm_add_sta_cmd_v7 cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100384 int ret;
385 u32 status;
386
387 lockdep_assert_held(&mvm->mutex);
388
389 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
390 cmd.sta_id = mvmsta->sta_id;
391 cmd.add_modify = STA_MODE_MODIFY;
392 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
393 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
394
395 status = ADD_STA_SUCCESS;
Max Stepanov5a258aa2013-04-07 09:11:21 +0300396 ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100397 if (ret)
398 return ret;
399
400 switch (status) {
401 case ADD_STA_SUCCESS:
402 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
403 mvmsta->sta_id);
404 break;
405 default:
406 ret = -EIO;
407 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
408 mvmsta->sta_id);
409 break;
410 }
411
412 return ret;
413}
414
415/*
416 * Remove a station from the FW table. Before sending the command to remove
417 * the station validate that the station is indeed known to the driver (sanity
418 * only).
419 */
420static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
421{
422 struct ieee80211_sta *sta;
423 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
424 .sta_id = sta_id,
425 };
426 int ret;
427
428 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
429 lockdep_is_held(&mvm->mutex));
430
431 /* Note: internal stations are marked as error values */
432 if (!sta) {
433 IWL_ERR(mvm, "Invalid station id\n");
434 return -EINVAL;
435 }
436
437 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, CMD_SYNC,
438 sizeof(rm_sta_cmd), &rm_sta_cmd);
439 if (ret) {
440 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
441 return ret;
442 }
443
444 return 0;
445}
446
447void iwl_mvm_sta_drained_wk(struct work_struct *wk)
448{
449 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
450 u8 sta_id;
451
452 /*
453 * The mutex is needed because of the SYNC cmd, but not only: if the
454 * work would run concurrently with iwl_mvm_rm_sta, it would run before
455 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
456 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
457 * that later.
458 */
459 mutex_lock(&mvm->mutex);
460
461 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
462 int ret;
463 struct ieee80211_sta *sta =
464 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
465 lockdep_is_held(&mvm->mutex));
466
Johannes Berg1ddbbb02013-12-04 22:39:17 +0100467 /*
468 * This station is in use or RCU-removed; the latter happens in
469 * managed mode, where mac80211 removes the station before we
470 * can remove it from firmware (we can only do that after the
471 * MAC is marked unassociated), and possibly while the deauth
472 * frame to disconnect from the AP is still queued. Then, the
473 * station pointer is -ENOENT when the last skb is reclaimed.
474 */
475 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100476 continue;
477
478 if (PTR_ERR(sta) == -EINVAL) {
479 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
480 sta_id);
481 continue;
482 }
483
484 if (!sta) {
485 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
486 sta_id);
487 continue;
488 }
489
490 WARN_ON(PTR_ERR(sta) != -EBUSY);
491 /* This station was removed and we waited until it got drained,
492 * we can now proceed and remove it.
493 */
494 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
495 if (ret) {
496 IWL_ERR(mvm,
497 "Couldn't remove sta %d after it was drained\n",
498 sta_id);
499 continue;
500 }
Monam Agarwalc531c772014-03-24 00:05:56 +0530501 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100502 clear_bit(sta_id, mvm->sta_drained);
503 }
504
505 mutex_unlock(&mvm->mutex);
506}
507
508int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
509 struct ieee80211_vif *vif,
510 struct ieee80211_sta *sta)
511{
512 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
513 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
514 int ret;
515
516 lockdep_assert_held(&mvm->mutex);
517
518 if (vif->type == NL80211_IFTYPE_STATION &&
519 mvmvif->ap_sta_id == mvm_sta->sta_id) {
Emmanuel Grumbach80d85652013-02-19 15:32:42 +0200520 /* flush its queues here since we are freeing mvm_sta */
521 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true);
522
Johannes Berg8ca151b2013-01-24 14:25:36 +0100523 /* if we are associated - we can't remove the AP STA now */
524 if (vif->bss_conf.assoc)
525 return ret;
526
527 /* unassoc - go ahead - remove the AP STA now */
528 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
Eliad Peller37577fe2013-12-05 17:19:39 +0200529
530 /* clear d0i3_ap_sta_id if no longer relevant */
531 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
532 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100533 }
534
535 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300536 * Make sure that the tx response code sees the station as -EBUSY and
537 * calls the drain worker.
538 */
539 spin_lock_bh(&mvm_sta->lock);
540 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +0100541 * There are frames pending on the AC queues for this station.
542 * We need to wait until all the frames are drained...
543 */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300544 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100545 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
546 ERR_PTR(-EBUSY));
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300547 spin_unlock_bh(&mvm_sta->lock);
548 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100549 } else {
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300550 spin_unlock_bh(&mvm_sta->lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100551 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
Monam Agarwalc531c772014-03-24 00:05:56 +0530552 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100553 }
554
555 return ret;
556}
557
558int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
559 struct ieee80211_vif *vif,
560 u8 sta_id)
561{
562 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
563
564 lockdep_assert_held(&mvm->mutex);
565
Monam Agarwalc531c772014-03-24 00:05:56 +0530566 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100567 return ret;
568}
569
570int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta,
Eliad Pellerb92e6612014-01-23 17:58:23 +0200571 u32 qmask, enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100572{
573 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +0200574 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100575 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
576 return -ENOSPC;
577 }
578
579 sta->tfd_queue_msk = qmask;
580
581 /* put a non-NULL value so iterating over the stations won't stop */
582 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
583 return 0;
584}
585
586void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
587{
Monam Agarwalc531c772014-03-24 00:05:56 +0530588 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100589 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
590 sta->sta_id = IWL_MVM_STATION_COUNT;
591}
592
593static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
594 struct iwl_mvm_int_sta *sta,
595 const u8 *addr,
596 u16 mac_id, u16 color)
597{
Johannes Berg3e56ead2013-02-15 22:23:18 +0100598 struct iwl_mvm_add_sta_cmd_v7 cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100599 int ret;
600 u32 status;
601
602 lockdep_assert_held(&mvm->mutex);
603
Johannes Berg3e56ead2013-02-15 22:23:18 +0100604 memset(&cmd, 0, sizeof(struct iwl_mvm_add_sta_cmd_v7));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100605 cmd.sta_id = sta->sta_id;
606 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
607 color));
608
609 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
610
611 if (addr)
612 memcpy(cmd.addr, addr, ETH_ALEN);
613
Max Stepanov5a258aa2013-04-07 09:11:21 +0300614 ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100615 if (ret)
616 return ret;
617
618 switch (status) {
619 case ADD_STA_SUCCESS:
620 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
621 return 0;
622 default:
623 ret = -EIO;
624 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
625 status);
626 break;
627 }
628 return ret;
629}
630
631int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
632{
633 int ret;
634
635 lockdep_assert_held(&mvm->mutex);
636
637 /* Add the aux station, but without any queues */
Eliad Pellerb92e6612014-01-23 17:58:23 +0200638 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, 0,
639 NL80211_IFTYPE_UNSPECIFIED);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100640 if (ret)
641 return ret;
642
643 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
644 MAC_INDEX_AUX, 0);
645
646 if (ret)
647 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
648 return ret;
649}
650
651/*
652 * Send the add station command for the vif's broadcast station.
653 * Assumes that the station was already allocated.
654 *
655 * @mvm: the mvm component
656 * @vif: the interface to which the broadcast station is added
657 * @bsta: the broadcast station to add.
658 */
659int iwl_mvm_send_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
660 struct iwl_mvm_int_sta *bsta)
661{
662 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg5023d962013-07-31 14:07:43 +0200663 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +0100664 const u8 *baddr = _baddr;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100665
666 lockdep_assert_held(&mvm->mutex);
667
Johannes Berg5023d962013-07-31 14:07:43 +0200668 if (vif->type == NL80211_IFTYPE_ADHOC)
669 baddr = vif->bss_conf.bssid;
670
Johannes Berg8ca151b2013-01-24 14:25:36 +0100671 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
672 return -ENOSPC;
673
674 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
675 mvmvif->id, mvmvif->color);
676}
677
678/* Send the FW a request to remove the station from it's internal data
679 * structures, but DO NOT remove the entry from the local data structures. */
680int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm,
681 struct iwl_mvm_int_sta *bsta)
682{
683 int ret;
684
685 lockdep_assert_held(&mvm->mutex);
686
687 ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
688 if (ret)
689 IWL_WARN(mvm, "Failed sending remove station\n");
690 return ret;
691}
692
693/* Allocate a new station entry for the broadcast station to the given vif,
694 * and send it to the FW.
695 * Note that each P2P mac should have its own broadcast station.
696 *
697 * @mvm: the mvm component
698 * @vif: the interface to which the broadcast station is added
699 * @bsta: the broadcast station to add. */
700int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
701 struct iwl_mvm_int_sta *bsta)
702{
703 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
704 static const u8 baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
705 u32 qmask;
706 int ret;
707
708 lockdep_assert_held(&mvm->mutex);
709
710 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
Eliad Pellerb92e6612014-01-23 17:58:23 +0200711 ret = iwl_mvm_allocate_int_sta(mvm, bsta, qmask,
712 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100713 if (ret)
714 return ret;
715
716 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
717 mvmvif->id, mvmvif->color);
718
719 if (ret)
720 iwl_mvm_dealloc_int_sta(mvm, bsta);
721 return ret;
722}
723
724/*
725 * Send the FW a request to remove the station from it's internal data
726 * structures, and in addition remove it from the local data structure.
727 */
728int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *bsta)
729{
730 int ret;
731
732 lockdep_assert_held(&mvm->mutex);
733
734 ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
735 if (ret)
736 return ret;
737
738 iwl_mvm_dealloc_int_sta(mvm, bsta);
739 return ret;
740}
741
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300742#define IWL_MAX_RX_BA_SESSIONS 16
743
Johannes Berg8ca151b2013-01-24 14:25:36 +0100744int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
745 int tid, u16 ssn, bool start)
746{
747 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
Johannes Berg3e56ead2013-02-15 22:23:18 +0100748 struct iwl_mvm_add_sta_cmd_v7 cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100749 int ret;
750 u32 status;
751
752 lockdep_assert_held(&mvm->mutex);
753
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300754 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
755 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
756 return -ENOSPC;
757 }
758
Johannes Berg8ca151b2013-01-24 14:25:36 +0100759 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
760 cmd.sta_id = mvm_sta->sta_id;
761 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +0300762 if (start) {
763 cmd.add_immediate_ba_tid = (u8) tid;
764 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
765 } else {
766 cmd.remove_immediate_ba_tid = (u8) tid;
767 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100768 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
769 STA_MODIFY_REMOVE_BA_TID;
770
771 status = ADD_STA_SUCCESS;
Max Stepanov5a258aa2013-04-07 09:11:21 +0300772 ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100773 if (ret)
774 return ret;
775
776 switch (status) {
777 case ADD_STA_SUCCESS:
778 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
779 start ? "start" : "stopp");
780 break;
781 case ADD_STA_IMMEDIATE_BA_FAILURE:
782 IWL_WARN(mvm, "RX BA Session refused by fw\n");
783 ret = -ENOSPC;
784 break;
785 default:
786 ret = -EIO;
787 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
788 start ? "start" : "stopp", status);
789 break;
790 }
791
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300792 if (!ret) {
793 if (start)
794 mvm->rx_ba_sessions++;
795 else if (mvm->rx_ba_sessions > 0)
796 /* check that restart flow didn't zero the counter */
797 mvm->rx_ba_sessions--;
798 }
799
Johannes Berg8ca151b2013-01-24 14:25:36 +0100800 return ret;
801}
802
803static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
804 int tid, u8 queue, bool start)
805{
806 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
Johannes Berg3e56ead2013-02-15 22:23:18 +0100807 struct iwl_mvm_add_sta_cmd_v7 cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100808 int ret;
809 u32 status;
810
811 lockdep_assert_held(&mvm->mutex);
812
813 if (start) {
814 mvm_sta->tfd_queue_msk |= BIT(queue);
815 mvm_sta->tid_disable_agg &= ~BIT(tid);
816 } else {
817 mvm_sta->tfd_queue_msk &= ~BIT(queue);
818 mvm_sta->tid_disable_agg |= BIT(tid);
819 }
820
821 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
822 cmd.sta_id = mvm_sta->sta_id;
823 cmd.add_modify = STA_MODE_MODIFY;
824 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
825 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
826 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
827
828 status = ADD_STA_SUCCESS;
Max Stepanov5a258aa2013-04-07 09:11:21 +0300829 ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100830 if (ret)
831 return ret;
832
833 switch (status) {
834 case ADD_STA_SUCCESS:
835 break;
836 default:
837 ret = -EIO;
838 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
839 start ? "start" : "stopp", status);
840 break;
841 }
842
843 return ret;
844}
845
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +0200846const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100847 IEEE80211_AC_BE,
848 IEEE80211_AC_BK,
849 IEEE80211_AC_BK,
850 IEEE80211_AC_BE,
851 IEEE80211_AC_VI,
852 IEEE80211_AC_VI,
853 IEEE80211_AC_VO,
854 IEEE80211_AC_VO,
855};
856
Johannes Berg3e56ead2013-02-15 22:23:18 +0100857static const u8 tid_to_ucode_ac[] = {
858 AC_BE,
859 AC_BK,
860 AC_BK,
861 AC_BE,
862 AC_VI,
863 AC_VI,
864 AC_VO,
865 AC_VO,
866};
867
Johannes Berg8ca151b2013-01-24 14:25:36 +0100868int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
869 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
870{
Johannes Berg5b577a92013-11-14 18:20:04 +0100871 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100872 struct iwl_mvm_tid_data *tid_data;
873 int txq_id;
874
875 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
876 return -EINVAL;
877
878 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
879 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
880 mvmsta->tid_data[tid].state);
881 return -ENXIO;
882 }
883
884 lockdep_assert_held(&mvm->mutex);
885
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200886 for (txq_id = mvm->first_agg_queue;
887 txq_id <= mvm->last_agg_queue; txq_id++)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100888 if (mvm->queue_to_mac80211[txq_id] ==
889 IWL_INVALID_MAC80211_QUEUE)
890 break;
891
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200892 if (txq_id > mvm->last_agg_queue) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100893 IWL_ERR(mvm, "Failed to allocate agg queue\n");
894 return -EIO;
895 }
896
Arik Nemtsovb2492502014-03-13 12:21:50 +0200897 spin_lock_bh(&mvmsta->lock);
898
899 /* possible race condition - we entered D0i3 while starting agg */
900 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
901 spin_unlock_bh(&mvmsta->lock);
902 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
903 return -EIO;
904 }
905
Johannes Berg8ca151b2013-01-24 14:25:36 +0100906 /* the new tx queue is still connected to the same mac80211 queue */
Johannes Berg3e56ead2013-02-15 22:23:18 +0100907 mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +0100908
Johannes Berg8ca151b2013-01-24 14:25:36 +0100909 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +0100910 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100911 tid_data->txq_id = txq_id;
912 *ssn = tid_data->ssn;
913
914 IWL_DEBUG_TX_QUEUES(mvm,
915 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
916 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
917 tid_data->next_reclaimed);
918
919 if (tid_data->ssn == tid_data->next_reclaimed) {
920 tid_data->state = IWL_AGG_STARTING;
921 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
922 } else {
923 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
924 }
925
926 spin_unlock_bh(&mvmsta->lock);
927
928 return 0;
929}
930
931int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
932 struct ieee80211_sta *sta, u16 tid, u8 buf_size)
933{
Johannes Berg5b577a92013-11-14 18:20:04 +0100934 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100935 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
936 int queue, fifo, ret;
937 u16 ssn;
938
939 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
940
941 spin_lock_bh(&mvmsta->lock);
942 ssn = tid_data->ssn;
943 queue = tid_data->txq_id;
944 tid_data->state = IWL_AGG_ON;
945 tid_data->ssn = 0xffff;
946 spin_unlock_bh(&mvmsta->lock);
947
Johannes Berg3e56ead2013-02-15 22:23:18 +0100948 fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +0100949
950 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
951 if (ret)
952 return -EIO;
953
954 iwl_trans_txq_enable(mvm->trans, queue, fifo, mvmsta->sta_id, tid,
955 buf_size, ssn);
956
957 /*
958 * Even though in theory the peer could have different
959 * aggregation reorder buffer sizes for different sessions,
960 * our ucode doesn't allow for that and has a global limit
961 * for each station. Therefore, use the minimum of all the
962 * aggregation sessions and our default value.
963 */
964 mvmsta->max_agg_bufsize =
965 min(mvmsta->max_agg_bufsize, buf_size);
966 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
967
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300968 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
969 sta->addr, tid);
970
Eyal Shapira9e680942013-11-09 00:16:16 +0200971 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100972}
973
974int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
975 struct ieee80211_sta *sta, u16 tid)
976{
Johannes Berg5b577a92013-11-14 18:20:04 +0100977 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100978 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
979 u16 txq_id;
980 int err;
981
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +0200982
983 /*
984 * If mac80211 is cleaning its state, then say that we finished since
985 * our state has been cleared anyway.
986 */
987 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
988 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
989 return 0;
990 }
991
Johannes Berg8ca151b2013-01-24 14:25:36 +0100992 spin_lock_bh(&mvmsta->lock);
993
994 txq_id = tid_data->txq_id;
995
996 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
997 mvmsta->sta_id, tid, txq_id, tid_data->state);
998
999 switch (tid_data->state) {
1000 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01001001 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001002
1003 IWL_DEBUG_TX_QUEUES(mvm,
1004 "ssn = %d, next_recl = %d\n",
1005 tid_data->ssn, tid_data->next_reclaimed);
1006
1007 /* There are still packets for this RA / TID in the HW */
1008 if (tid_data->ssn != tid_data->next_reclaimed) {
1009 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
1010 err = 0;
1011 break;
1012 }
1013
1014 tid_data->ssn = 0xffff;
1015 iwl_trans_txq_disable(mvm->trans, txq_id);
1016 /* fall through */
1017 case IWL_AGG_STARTING:
1018 case IWL_EMPTYING_HW_QUEUE_ADDBA:
1019 /*
1020 * The agg session has been stopped before it was set up. This
1021 * can happen when the AddBA timer times out for example.
1022 */
1023
1024 /* No barriers since we are under mutex */
1025 lockdep_assert_held(&mvm->mutex);
1026 mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
1027
1028 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1029 tid_data->state = IWL_AGG_OFF;
1030 err = 0;
1031 break;
1032 default:
1033 IWL_ERR(mvm,
1034 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
1035 mvmsta->sta_id, tid, tid_data->state);
1036 IWL_ERR(mvm,
1037 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
1038 err = -EINVAL;
1039 }
1040
1041 spin_unlock_bh(&mvmsta->lock);
1042
1043 return err;
1044}
1045
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001046int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1047 struct ieee80211_sta *sta, u16 tid)
1048{
Johannes Berg5b577a92013-11-14 18:20:04 +01001049 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001050 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1051 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02001052 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001053
1054 /*
1055 * First set the agg state to OFF to avoid calling
1056 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
1057 */
1058 spin_lock_bh(&mvmsta->lock);
1059 txq_id = tid_data->txq_id;
1060 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
1061 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001062 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001063 tid_data->state = IWL_AGG_OFF;
1064 spin_unlock_bh(&mvmsta->lock);
1065
Johannes Bergb6658ff2013-07-24 13:55:51 +02001066 if (old_state >= IWL_AGG_ON) {
1067 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true))
1068 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001069
Johannes Bergb6658ff2013-07-24 13:55:51 +02001070 iwl_trans_txq_disable(mvm->trans, tid_data->txq_id);
1071 }
1072
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001073 mvm->queue_to_mac80211[tid_data->txq_id] =
1074 IWL_INVALID_MAC80211_QUEUE;
1075
1076 return 0;
1077}
1078
Johannes Berg8ca151b2013-01-24 14:25:36 +01001079static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
1080{
1081 int i;
1082
1083 lockdep_assert_held(&mvm->mutex);
1084
1085 i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM);
1086
1087 if (i == STA_KEY_MAX_NUM)
1088 return STA_KEY_IDX_INVALID;
1089
1090 __set_bit(i, mvm->fw_key_table);
1091
1092 return i;
1093}
1094
1095static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
1096 struct ieee80211_sta *sta)
1097{
1098 struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
1099
1100 if (sta) {
1101 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1102
1103 return mvm_sta->sta_id;
1104 }
1105
1106 /*
1107 * The device expects GTKs for station interfaces to be
1108 * installed as GTKs for the AP station. If we have no
1109 * station ID, then use AP's station ID.
1110 */
1111 if (vif->type == NL80211_IFTYPE_STATION &&
1112 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
1113 return mvmvif->ap_sta_id;
1114
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001115 return IWL_MVM_STATION_COUNT;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001116}
1117
1118static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
1119 struct iwl_mvm_sta *mvm_sta,
1120 struct ieee80211_key_conf *keyconf,
1121 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1122 u32 cmd_flags)
1123{
1124 __le16 key_flags;
Max Stepanov5a258aa2013-04-07 09:11:21 +03001125 struct iwl_mvm_add_sta_key_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001126 int ret, status;
1127 u16 keyidx;
1128 int i;
Max Stepanov5a258aa2013-04-07 09:11:21 +03001129 u32 mac_id_n_color = mvm_sta->mac_id_n_color;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001130
1131 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1132 STA_KEY_FLG_KEYID_MSK;
1133 key_flags = cpu_to_le16(keyidx);
1134 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
1135
1136 switch (keyconf->cipher) {
1137 case WLAN_CIPHER_SUITE_TKIP:
1138 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001139 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001140 for (i = 0; i < 5; i++)
Max Stepanov5a258aa2013-04-07 09:11:21 +03001141 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1142 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001143 break;
1144 case WLAN_CIPHER_SUITE_CCMP:
1145 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001146 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001147 break;
1148 default:
Max Stepanove36e5432013-08-27 19:56:13 +03001149 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
1150 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001151 }
1152
1153 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1154 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1155
Max Stepanov5a258aa2013-04-07 09:11:21 +03001156 cmd.key_offset = keyconf->hw_key_idx;
1157 cmd.key_flags = key_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001158 cmd.sta_id = sta_id;
1159
1160 status = ADD_STA_SUCCESS;
1161 if (cmd_flags == CMD_SYNC)
Max Stepanov5a258aa2013-04-07 09:11:21 +03001162 ret = iwl_mvm_send_add_sta_key_cmd_status(mvm, &cmd,
1163 mac_id_n_color,
1164 &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001165 else
Max Stepanov5a258aa2013-04-07 09:11:21 +03001166 ret = iwl_mvm_send_add_sta_key_cmd(mvm, CMD_ASYNC, &cmd,
1167 mac_id_n_color);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001168
1169 switch (status) {
1170 case ADD_STA_SUCCESS:
1171 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1172 break;
1173 default:
1174 ret = -EIO;
1175 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1176 break;
1177 }
1178
1179 return ret;
1180}
1181
1182static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1183 struct ieee80211_key_conf *keyconf,
1184 u8 sta_id, bool remove_key)
1185{
1186 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1187
1188 /* verify the key details match the required command's expectations */
1189 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1190 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1191 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1192 return -EINVAL;
1193
1194 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1195 igtk_cmd.sta_id = cpu_to_le32(sta_id);
1196
1197 if (remove_key) {
1198 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1199 } else {
1200 struct ieee80211_key_seq seq;
1201 const u8 *pn;
1202
1203 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
1204 ieee80211_aes_cmac_calculate_k1_k2(keyconf,
1205 igtk_cmd.K1, igtk_cmd.K2);
1206 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1207 pn = seq.aes_cmac.pn;
1208 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1209 ((u64) pn[4] << 8) |
1210 ((u64) pn[3] << 16) |
1211 ((u64) pn[2] << 24) |
1212 ((u64) pn[1] << 32) |
1213 ((u64) pn[0] << 40));
1214 }
1215
1216 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1217 remove_key ? "removing" : "installing",
1218 igtk_cmd.sta_id);
1219
1220 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, CMD_SYNC,
1221 sizeof(igtk_cmd), &igtk_cmd);
1222}
1223
1224
1225static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1226 struct ieee80211_vif *vif,
1227 struct ieee80211_sta *sta)
1228{
1229 struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
1230
1231 if (sta)
1232 return sta->addr;
1233
1234 if (vif->type == NL80211_IFTYPE_STATION &&
1235 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1236 u8 sta_id = mvmvif->ap_sta_id;
1237 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1238 lockdep_is_held(&mvm->mutex));
1239 return sta->addr;
1240 }
1241
1242
1243 return NULL;
1244}
1245
1246int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1247 struct ieee80211_vif *vif,
1248 struct ieee80211_sta *sta,
1249 struct ieee80211_key_conf *keyconf,
1250 bool have_key_offset)
1251{
1252 struct iwl_mvm_sta *mvm_sta;
1253 int ret;
1254 u8 *addr, sta_id;
1255 struct ieee80211_key_seq seq;
1256 u16 p1k[5];
1257
1258 lockdep_assert_held(&mvm->mutex);
1259
1260 /* Get the station id from the mvm local station table */
1261 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001262 if (sta_id == IWL_MVM_STATION_COUNT) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001263 IWL_ERR(mvm, "Failed to find station id\n");
1264 return -EINVAL;
1265 }
1266
1267 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1268 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1269 goto end;
1270 }
1271
1272 /*
1273 * It is possible that the 'sta' parameter is NULL, and thus
1274 * there is a need to retrieve the sta from the local station table.
1275 */
1276 if (!sta) {
1277 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1278 lockdep_is_held(&mvm->mutex));
1279 if (IS_ERR_OR_NULL(sta)) {
1280 IWL_ERR(mvm, "Invalid station id\n");
1281 return -EINVAL;
1282 }
1283 }
1284
1285 mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1286 if (WARN_ON_ONCE(mvm_sta->vif != vif))
1287 return -EINVAL;
1288
1289 if (!have_key_offset) {
1290 /*
1291 * The D3 firmware hardcodes the PTK offset to 0, so we have to
1292 * configure it there. As a result, this workaround exists to
1293 * let the caller set the key offset (hw_key_idx), see d3.c.
1294 */
1295 keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
1296 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1297 return -ENOSPC;
1298 }
1299
1300 switch (keyconf->cipher) {
1301 case WLAN_CIPHER_SUITE_TKIP:
1302 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1303 /* get phase 1 key from mac80211 */
1304 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1305 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1306 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1307 seq.tkip.iv32, p1k, CMD_SYNC);
1308 break;
1309 case WLAN_CIPHER_SUITE_CCMP:
1310 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1311 0, NULL, CMD_SYNC);
1312 break;
1313 default:
Max Stepanove36e5432013-08-27 19:56:13 +03001314 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf,
1315 sta_id, 0, NULL, CMD_SYNC);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001316 }
1317
1318 if (ret)
1319 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1320
1321end:
1322 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1323 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1324 sta->addr, ret);
1325 return ret;
1326}
1327
1328int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1329 struct ieee80211_vif *vif,
1330 struct ieee80211_sta *sta,
1331 struct ieee80211_key_conf *keyconf)
1332{
1333 struct iwl_mvm_sta *mvm_sta;
Max Stepanov5a258aa2013-04-07 09:11:21 +03001334 struct iwl_mvm_add_sta_key_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001335 __le16 key_flags;
1336 int ret, status;
1337 u8 sta_id;
1338
1339 lockdep_assert_held(&mvm->mutex);
1340
1341 /* Get the station id from the mvm local station table */
1342 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1343
1344 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1345 keyconf->keyidx, sta_id);
1346
1347 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1348 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1349
1350 ret = __test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1351 if (!ret) {
1352 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1353 keyconf->hw_key_idx);
1354 return -ENOENT;
1355 }
1356
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001357 if (sta_id == IWL_MVM_STATION_COUNT) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001358 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1359 return 0;
1360 }
1361
1362 /*
1363 * It is possible that the 'sta' parameter is NULL, and thus
1364 * there is a need to retrieve the sta from the local station table,
1365 * for example when a GTK is removed (where the sta_id will then be
1366 * the AP ID, and no station was passed by mac80211.)
1367 */
1368 if (!sta) {
1369 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1370 lockdep_is_held(&mvm->mutex));
1371 if (!sta) {
1372 IWL_ERR(mvm, "Invalid station id\n");
1373 return -EINVAL;
1374 }
1375 }
1376
1377 mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1378 if (WARN_ON_ONCE(mvm_sta->vif != vif))
1379 return -EINVAL;
1380
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02001381 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1382 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001383 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1384 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1385
1386 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1387 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1388
Max Stepanov5a258aa2013-04-07 09:11:21 +03001389 cmd.key_flags = key_flags;
1390 cmd.key_offset = keyconf->hw_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001391 cmd.sta_id = sta_id;
1392
Johannes Berg8ca151b2013-01-24 14:25:36 +01001393 status = ADD_STA_SUCCESS;
Max Stepanov5a258aa2013-04-07 09:11:21 +03001394 ret = iwl_mvm_send_add_sta_key_cmd_status(mvm, &cmd,
1395 mvm_sta->mac_id_n_color,
1396 &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001397
1398 switch (status) {
1399 case ADD_STA_SUCCESS:
1400 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1401 break;
1402 default:
1403 ret = -EIO;
1404 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1405 break;
1406 }
1407
1408 return ret;
1409}
1410
1411void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1412 struct ieee80211_vif *vif,
1413 struct ieee80211_key_conf *keyconf,
1414 struct ieee80211_sta *sta, u32 iv32,
1415 u16 *phase1key)
1416{
Beni Levc3eb5362013-02-06 17:22:18 +02001417 struct iwl_mvm_sta *mvm_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001418 u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1419
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001420 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001421 return;
1422
Beni Levc3eb5362013-02-06 17:22:18 +02001423 rcu_read_lock();
1424
1425 if (!sta) {
1426 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1427 if (WARN_ON(IS_ERR_OR_NULL(sta))) {
1428 rcu_read_unlock();
1429 return;
1430 }
1431 }
1432
1433 mvm_sta = (void *)sta->drv_priv;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001434 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1435 iv32, phase1key, CMD_ASYNC);
Beni Levc3eb5362013-02-06 17:22:18 +02001436 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01001437}
1438
Johannes Berg9cc40712013-02-15 22:47:48 +01001439void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
1440 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001441{
Johannes Berg5b577a92013-11-14 18:20:04 +01001442 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg3e56ead2013-02-15 22:23:18 +01001443 struct iwl_mvm_add_sta_cmd_v7 cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001444 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01001445 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03001446 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01001447 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01001448 };
1449 int ret;
1450
Max Stepanov5a258aa2013-04-07 09:11:21 +03001451 ret = iwl_mvm_send_add_sta_cmd(mvm, CMD_ASYNC, &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001452 if (ret)
1453 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1454}
1455
Johannes Berg9cc40712013-02-15 22:47:48 +01001456void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
1457 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001458 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01001459 u16 cnt, u16 tids, bool more_data,
1460 bool agg)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001461{
Johannes Berg5b577a92013-11-14 18:20:04 +01001462 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg3e56ead2013-02-15 22:23:18 +01001463 struct iwl_mvm_add_sta_cmd_v7 cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001464 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01001465 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001466 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1467 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01001468 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01001469 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01001470 int tid, ret;
1471 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001472
Johannes Berg3e56ead2013-02-15 22:23:18 +01001473 /* convert TIDs to ACs - we don't support TSPEC so that's OK
1474 * Note that this field is reserved and unused by firmware not
1475 * supporting GO uAPSD, so it's safe to always do this.
1476 */
1477 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
1478 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
1479
1480 /* If we're releasing frames from aggregation queues then check if the
1481 * all queues combined that we're releasing frames from have
1482 * - more frames than the service period, in which case more_data
1483 * needs to be set
1484 * - fewer than 'cnt' frames, in which case we need to adjust the
1485 * firmware command (but do that unconditionally)
1486 */
1487 if (agg) {
1488 int remaining = cnt;
1489
1490 spin_lock_bh(&mvmsta->lock);
1491 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
1492 struct iwl_mvm_tid_data *tid_data;
1493 u16 n_queued;
1494
1495 tid_data = &mvmsta->tid_data[tid];
1496 if (WARN(tid_data->state != IWL_AGG_ON &&
1497 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
1498 "TID %d state is %d\n",
1499 tid, tid_data->state)) {
1500 spin_unlock_bh(&mvmsta->lock);
1501 ieee80211_sta_eosp(sta);
1502 return;
1503 }
1504
1505 n_queued = iwl_mvm_tid_queued(tid_data);
1506 if (n_queued > remaining) {
1507 more_data = true;
1508 remaining = 0;
1509 break;
1510 }
1511 remaining -= n_queued;
1512 }
1513 spin_unlock_bh(&mvmsta->lock);
1514
1515 cmd.sleep_tx_count = cpu_to_le16(cnt - remaining);
1516 if (WARN_ON(cnt - remaining == 0)) {
1517 ieee80211_sta_eosp(sta);
1518 return;
1519 }
1520 }
1521
1522 /* Note: this is ignored by firmware not supporting GO uAPSD */
1523 if (more_data)
1524 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
1525
1526 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
1527 mvmsta->next_status_eosp = true;
1528 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
1529 } else {
1530 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
1531 }
1532
Max Stepanov5a258aa2013-04-07 09:11:21 +03001533 ret = iwl_mvm_send_add_sta_cmd(mvm, CMD_ASYNC, &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001534 if (ret)
1535 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1536}
Johannes Berg3e56ead2013-02-15 22:23:18 +01001537
1538int iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
1539 struct iwl_rx_cmd_buffer *rxb,
1540 struct iwl_device_cmd *cmd)
1541{
1542 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1543 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
1544 struct ieee80211_sta *sta;
1545 u32 sta_id = le32_to_cpu(notif->sta_id);
1546
1547 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
1548 return 0;
1549
1550 rcu_read_lock();
1551 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1552 if (!IS_ERR_OR_NULL(sta))
1553 ieee80211_sta_eosp(sta);
1554 rcu_read_unlock();
1555
1556 return 0;
1557}