blob: fa900c75c509c245c8ade85c1ec1facec41be636 [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 *
8 * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
9 *
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 *
33 * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
34 * 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
Max Stepanov5a258aa2013-04-07 09:11:21 +030069static void iwl_mvm_add_sta_cmd_v6_to_v5(struct iwl_mvm_add_sta_cmd_v6 *cmd_v6,
70 struct iwl_mvm_add_sta_cmd_v5 *cmd_v5)
71{
72 memset(cmd_v5, 0, sizeof(*cmd_v5));
73
74 cmd_v5->add_modify = cmd_v6->add_modify;
75 cmd_v5->tid_disable_tx = cmd_v6->tid_disable_tx;
76 cmd_v5->mac_id_n_color = cmd_v6->mac_id_n_color;
77 memcpy(cmd_v5->addr, cmd_v6->addr, ETH_ALEN);
78 cmd_v5->sta_id = cmd_v6->sta_id;
79 cmd_v5->modify_mask = cmd_v6->modify_mask;
80 cmd_v5->station_flags = cmd_v6->station_flags;
81 cmd_v5->station_flags_msk = cmd_v6->station_flags_msk;
82 cmd_v5->add_immediate_ba_tid = cmd_v6->add_immediate_ba_tid;
83 cmd_v5->remove_immediate_ba_tid = cmd_v6->remove_immediate_ba_tid;
84 cmd_v5->add_immediate_ba_ssn = cmd_v6->add_immediate_ba_ssn;
85 cmd_v5->sleep_tx_count = cmd_v6->sleep_tx_count;
86 cmd_v5->sleep_state_flags = cmd_v6->sleep_state_flags;
87 cmd_v5->assoc_id = cmd_v6->assoc_id;
88 cmd_v5->beamform_flags = cmd_v6->beamform_flags;
89 cmd_v5->tfd_queue_msk = cmd_v6->tfd_queue_msk;
90}
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,
113 struct iwl_mvm_add_sta_cmd_v6 *cmd,
114 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
122 iwl_mvm_add_sta_cmd_v6_to_v5(cmd, &cmd_v5);
123
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,
129 struct iwl_mvm_add_sta_cmd_v6 *cmd)
130{
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
137 iwl_mvm_add_sta_cmd_v6_to_v5(cmd, &cmd_v5);
138
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
Johannes Berg8ca151b2013-01-24 14:25:36 +0100178static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm)
179{
180 int sta_id;
181
182 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
183
184 lockdep_assert_held(&mvm->mutex);
185
186 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
187 for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++)
188 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
189 lockdep_is_held(&mvm->mutex)))
190 return sta_id;
191 return IWL_MVM_STATION_COUNT;
192}
193
Johannes Berg7a453972013-02-12 13:10:44 +0100194/* send station add/update command to firmware */
195int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
196 bool update)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100197{
198 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
Max Stepanov5a258aa2013-04-07 09:11:21 +0300199 struct iwl_mvm_add_sta_cmd_v6 add_sta_cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100200 int ret;
201 u32 status;
202 u32 agg_size = 0, mpdu_dens = 0;
203
204 memset(&add_sta_cmd, 0, sizeof(add_sta_cmd));
205
206 add_sta_cmd.sta_id = mvm_sta->sta_id;
207 add_sta_cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
Johannes Berg7a453972013-02-12 13:10:44 +0100208 if (!update) {
209 add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
210 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
211 }
212 add_sta_cmd.add_modify = update ? 1 : 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100213
Johannes Berg5bc5aaa2013-02-12 14:35:36 +0100214 add_sta_cmd.station_flags_msk |= cpu_to_le32(STA_FLG_FAT_EN_MSK |
215 STA_FLG_MIMO_EN_MSK);
216
217 switch (sta->bandwidth) {
218 case IEEE80211_STA_RX_BW_160:
219 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
220 /* fall through */
221 case IEEE80211_STA_RX_BW_80:
222 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
223 /* fall through */
224 case IEEE80211_STA_RX_BW_40:
225 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
226 /* fall through */
227 case IEEE80211_STA_RX_BW_20:
228 if (sta->ht_cap.ht_supported)
229 add_sta_cmd.station_flags |=
230 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
231 break;
232 }
233
234 switch (sta->rx_nss) {
235 case 1:
236 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
237 break;
238 case 2:
239 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
240 break;
241 case 3 ... 8:
242 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
243 break;
244 }
245
246 switch (sta->smps_mode) {
247 case IEEE80211_SMPS_AUTOMATIC:
248 case IEEE80211_SMPS_NUM_MODES:
249 WARN_ON(1);
250 break;
251 case IEEE80211_SMPS_STATIC:
252 /* override NSS */
253 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
254 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
255 break;
256 case IEEE80211_SMPS_DYNAMIC:
257 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
258 break;
259 case IEEE80211_SMPS_OFF:
260 /* nothing */
261 break;
262 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100263
264 if (sta->ht_cap.ht_supported) {
265 add_sta_cmd.station_flags_msk |=
266 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
267 STA_FLG_AGG_MPDU_DENS_MSK);
268
269 mpdu_dens = sta->ht_cap.ampdu_density;
270 }
271
272 if (sta->vht_cap.vht_supported) {
273 agg_size = sta->vht_cap.cap &
274 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
275 agg_size >>=
276 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
277 } else if (sta->ht_cap.ht_supported) {
278 agg_size = sta->ht_cap.ampdu_factor;
279 }
280
281 add_sta_cmd.station_flags |=
282 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
283 add_sta_cmd.station_flags |=
284 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
285
286 status = ADD_STA_SUCCESS;
Max Stepanov5a258aa2013-04-07 09:11:21 +0300287 ret = iwl_mvm_send_add_sta_cmd_status(mvm, &add_sta_cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100288 if (ret)
289 return ret;
290
291 switch (status) {
292 case ADD_STA_SUCCESS:
293 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
294 break;
295 default:
296 ret = -EIO;
297 IWL_ERR(mvm, "ADD_STA failed\n");
298 break;
299 }
300
301 return ret;
302}
303
304int iwl_mvm_add_sta(struct iwl_mvm *mvm,
305 struct ieee80211_vif *vif,
306 struct ieee80211_sta *sta)
307{
308 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
309 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
310 int i, ret, sta_id;
311
312 lockdep_assert_held(&mvm->mutex);
313
314 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
315 sta_id = iwl_mvm_find_free_sta_id(mvm);
316 else
317 sta_id = mvm_sta->sta_id;
318
319 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
320 return -ENOSPC;
321
322 spin_lock_init(&mvm_sta->lock);
323
324 mvm_sta->sta_id = sta_id;
325 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
326 mvmvif->color);
327 mvm_sta->vif = vif;
328 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300329 mvm_sta->tx_protection = 0;
330 mvm_sta->tt_tx_protection = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100331
332 /* HW restart, don't assume the memory has been zeroed */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300333 atomic_set(&mvm->pending_frames[sta_id], 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100334 mvm_sta->tid_disable_agg = 0;
335 mvm_sta->tfd_queue_msk = 0;
336 for (i = 0; i < IEEE80211_NUM_ACS; i++)
337 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
338 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
339
Johannes Berg8ca151b2013-01-24 14:25:36 +0100340 /* for HW restart - need to reset the seq_number etc... */
341 memset(mvm_sta->tid_data, 0, sizeof(mvm_sta->tid_data));
342
Johannes Berg7a453972013-02-12 13:10:44 +0100343 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100344 if (ret)
345 return ret;
346
347 /* The first station added is the AP, the others are TDLS STAs */
348 if (vif->type == NL80211_IFTYPE_STATION &&
349 mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
350 mvmvif->ap_sta_id = sta_id;
351
352 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
353
354 return 0;
355}
356
Johannes Berg7a453972013-02-12 13:10:44 +0100357int iwl_mvm_update_sta(struct iwl_mvm *mvm,
358 struct ieee80211_vif *vif,
359 struct ieee80211_sta *sta)
360{
361 return iwl_mvm_sta_send_to_fw(mvm, sta, true);
362}
363
Johannes Berg8ca151b2013-01-24 14:25:36 +0100364int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
365 bool drain)
366{
Max Stepanov5a258aa2013-04-07 09:11:21 +0300367 struct iwl_mvm_add_sta_cmd_v6 cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100368 int ret;
369 u32 status;
370
371 lockdep_assert_held(&mvm->mutex);
372
373 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
374 cmd.sta_id = mvmsta->sta_id;
375 cmd.add_modify = STA_MODE_MODIFY;
376 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
377 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
378
379 status = ADD_STA_SUCCESS;
Max Stepanov5a258aa2013-04-07 09:11:21 +0300380 ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100381 if (ret)
382 return ret;
383
384 switch (status) {
385 case ADD_STA_SUCCESS:
386 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
387 mvmsta->sta_id);
388 break;
389 default:
390 ret = -EIO;
391 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
392 mvmsta->sta_id);
393 break;
394 }
395
396 return ret;
397}
398
399/*
400 * Remove a station from the FW table. Before sending the command to remove
401 * the station validate that the station is indeed known to the driver (sanity
402 * only).
403 */
404static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
405{
406 struct ieee80211_sta *sta;
407 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
408 .sta_id = sta_id,
409 };
410 int ret;
411
412 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
413 lockdep_is_held(&mvm->mutex));
414
415 /* Note: internal stations are marked as error values */
416 if (!sta) {
417 IWL_ERR(mvm, "Invalid station id\n");
418 return -EINVAL;
419 }
420
421 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, CMD_SYNC,
422 sizeof(rm_sta_cmd), &rm_sta_cmd);
423 if (ret) {
424 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
425 return ret;
426 }
427
428 return 0;
429}
430
431void iwl_mvm_sta_drained_wk(struct work_struct *wk)
432{
433 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
434 u8 sta_id;
435
436 /*
437 * The mutex is needed because of the SYNC cmd, but not only: if the
438 * work would run concurrently with iwl_mvm_rm_sta, it would run before
439 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
440 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
441 * that later.
442 */
443 mutex_lock(&mvm->mutex);
444
445 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
446 int ret;
447 struct ieee80211_sta *sta =
448 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
449 lockdep_is_held(&mvm->mutex));
450
451 /* This station is in use */
452 if (!IS_ERR(sta))
453 continue;
454
455 if (PTR_ERR(sta) == -EINVAL) {
456 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
457 sta_id);
458 continue;
459 }
460
461 if (!sta) {
462 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
463 sta_id);
464 continue;
465 }
466
467 WARN_ON(PTR_ERR(sta) != -EBUSY);
468 /* This station was removed and we waited until it got drained,
469 * we can now proceed and remove it.
470 */
471 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
472 if (ret) {
473 IWL_ERR(mvm,
474 "Couldn't remove sta %d after it was drained\n",
475 sta_id);
476 continue;
477 }
478 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], NULL);
479 clear_bit(sta_id, mvm->sta_drained);
480 }
481
482 mutex_unlock(&mvm->mutex);
483}
484
485int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
486 struct ieee80211_vif *vif,
487 struct ieee80211_sta *sta)
488{
489 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
490 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
491 int ret;
492
493 lockdep_assert_held(&mvm->mutex);
494
495 if (vif->type == NL80211_IFTYPE_STATION &&
496 mvmvif->ap_sta_id == mvm_sta->sta_id) {
Emmanuel Grumbach80d85652013-02-19 15:32:42 +0200497 /* flush its queues here since we are freeing mvm_sta */
498 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true);
499
Johannes Berg8ca151b2013-01-24 14:25:36 +0100500 /*
501 * Put a non-NULL since the fw station isn't removed.
502 * It will be removed after the MAC will be set as
503 * unassoc.
504 */
505 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
506 ERR_PTR(-EINVAL));
507
Johannes Berg8ca151b2013-01-24 14:25:36 +0100508 /* if we are associated - we can't remove the AP STA now */
509 if (vif->bss_conf.assoc)
510 return ret;
511
512 /* unassoc - go ahead - remove the AP STA now */
513 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
514 }
515
516 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300517 * Make sure that the tx response code sees the station as -EBUSY and
518 * calls the drain worker.
519 */
520 spin_lock_bh(&mvm_sta->lock);
521 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +0100522 * There are frames pending on the AC queues for this station.
523 * We need to wait until all the frames are drained...
524 */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300525 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100526 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
527 ERR_PTR(-EBUSY));
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300528 spin_unlock_bh(&mvm_sta->lock);
529 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100530 } else {
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300531 spin_unlock_bh(&mvm_sta->lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100532 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
533 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
534 }
535
536 return ret;
537}
538
539int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
540 struct ieee80211_vif *vif,
541 u8 sta_id)
542{
543 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
544
545 lockdep_assert_held(&mvm->mutex);
546
547 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], NULL);
548 return ret;
549}
550
551int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta,
552 u32 qmask)
553{
554 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
555 sta->sta_id = iwl_mvm_find_free_sta_id(mvm);
556 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
557 return -ENOSPC;
558 }
559
560 sta->tfd_queue_msk = qmask;
561
562 /* put a non-NULL value so iterating over the stations won't stop */
563 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
564 return 0;
565}
566
567void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
568{
569 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
570 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
571 sta->sta_id = IWL_MVM_STATION_COUNT;
572}
573
574static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
575 struct iwl_mvm_int_sta *sta,
576 const u8 *addr,
577 u16 mac_id, u16 color)
578{
Max Stepanov5a258aa2013-04-07 09:11:21 +0300579 struct iwl_mvm_add_sta_cmd_v6 cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100580 int ret;
581 u32 status;
582
583 lockdep_assert_held(&mvm->mutex);
584
Max Stepanov5a258aa2013-04-07 09:11:21 +0300585 memset(&cmd, 0, sizeof(struct iwl_mvm_add_sta_cmd_v6));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100586 cmd.sta_id = sta->sta_id;
587 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
588 color));
589
590 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
591
592 if (addr)
593 memcpy(cmd.addr, addr, ETH_ALEN);
594
Max Stepanov5a258aa2013-04-07 09:11:21 +0300595 ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100596 if (ret)
597 return ret;
598
599 switch (status) {
600 case ADD_STA_SUCCESS:
601 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
602 return 0;
603 default:
604 ret = -EIO;
605 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
606 status);
607 break;
608 }
609 return ret;
610}
611
612int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
613{
614 int ret;
615
616 lockdep_assert_held(&mvm->mutex);
617
618 /* Add the aux station, but without any queues */
619 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, 0);
620 if (ret)
621 return ret;
622
623 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
624 MAC_INDEX_AUX, 0);
625
626 if (ret)
627 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
628 return ret;
629}
630
631/*
632 * Send the add station command for the vif's broadcast station.
633 * Assumes that the station was already allocated.
634 *
635 * @mvm: the mvm component
636 * @vif: the interface to which the broadcast station is added
637 * @bsta: the broadcast station to add.
638 */
639int iwl_mvm_send_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
640 struct iwl_mvm_int_sta *bsta)
641{
642 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
643 static const u8 baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
644
645 lockdep_assert_held(&mvm->mutex);
646
647 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
648 return -ENOSPC;
649
650 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
651 mvmvif->id, mvmvif->color);
652}
653
654/* Send the FW a request to remove the station from it's internal data
655 * structures, but DO NOT remove the entry from the local data structures. */
656int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm,
657 struct iwl_mvm_int_sta *bsta)
658{
659 int ret;
660
661 lockdep_assert_held(&mvm->mutex);
662
663 ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
664 if (ret)
665 IWL_WARN(mvm, "Failed sending remove station\n");
666 return ret;
667}
668
669/* Allocate a new station entry for the broadcast station to the given vif,
670 * and send it to the FW.
671 * Note that each P2P mac should have its own broadcast station.
672 *
673 * @mvm: the mvm component
674 * @vif: the interface to which the broadcast station is added
675 * @bsta: the broadcast station to add. */
676int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
677 struct iwl_mvm_int_sta *bsta)
678{
679 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
680 static const u8 baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
681 u32 qmask;
682 int ret;
683
684 lockdep_assert_held(&mvm->mutex);
685
686 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
687 ret = iwl_mvm_allocate_int_sta(mvm, bsta, qmask);
688 if (ret)
689 return ret;
690
691 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
692 mvmvif->id, mvmvif->color);
693
694 if (ret)
695 iwl_mvm_dealloc_int_sta(mvm, bsta);
696 return ret;
697}
698
699/*
700 * Send the FW a request to remove the station from it's internal data
701 * structures, and in addition remove it from the local data structure.
702 */
703int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *bsta)
704{
705 int ret;
706
707 lockdep_assert_held(&mvm->mutex);
708
709 ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
710 if (ret)
711 return ret;
712
713 iwl_mvm_dealloc_int_sta(mvm, bsta);
714 return ret;
715}
716
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300717#define IWL_MAX_RX_BA_SESSIONS 16
718
Johannes Berg8ca151b2013-01-24 14:25:36 +0100719int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
720 int tid, u16 ssn, bool start)
721{
722 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
Max Stepanov5a258aa2013-04-07 09:11:21 +0300723 struct iwl_mvm_add_sta_cmd_v6 cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100724 int ret;
725 u32 status;
726
727 lockdep_assert_held(&mvm->mutex);
728
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300729 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
730 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
731 return -ENOSPC;
732 }
733
Johannes Berg8ca151b2013-01-24 14:25:36 +0100734 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
735 cmd.sta_id = mvm_sta->sta_id;
736 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +0300737 if (start) {
738 cmd.add_immediate_ba_tid = (u8) tid;
739 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
740 } else {
741 cmd.remove_immediate_ba_tid = (u8) tid;
742 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100743 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
744 STA_MODIFY_REMOVE_BA_TID;
745
746 status = ADD_STA_SUCCESS;
Max Stepanov5a258aa2013-04-07 09:11:21 +0300747 ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100748 if (ret)
749 return ret;
750
751 switch (status) {
752 case ADD_STA_SUCCESS:
753 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
754 start ? "start" : "stopp");
755 break;
756 case ADD_STA_IMMEDIATE_BA_FAILURE:
757 IWL_WARN(mvm, "RX BA Session refused by fw\n");
758 ret = -ENOSPC;
759 break;
760 default:
761 ret = -EIO;
762 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
763 start ? "start" : "stopp", status);
764 break;
765 }
766
Emmanuel Grumbach113a0442013-07-02 14:16:38 +0300767 if (!ret) {
768 if (start)
769 mvm->rx_ba_sessions++;
770 else if (mvm->rx_ba_sessions > 0)
771 /* check that restart flow didn't zero the counter */
772 mvm->rx_ba_sessions--;
773 }
774
Johannes Berg8ca151b2013-01-24 14:25:36 +0100775 return ret;
776}
777
778static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
779 int tid, u8 queue, bool start)
780{
781 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
Max Stepanov5a258aa2013-04-07 09:11:21 +0300782 struct iwl_mvm_add_sta_cmd_v6 cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100783 int ret;
784 u32 status;
785
786 lockdep_assert_held(&mvm->mutex);
787
788 if (start) {
789 mvm_sta->tfd_queue_msk |= BIT(queue);
790 mvm_sta->tid_disable_agg &= ~BIT(tid);
791 } else {
792 mvm_sta->tfd_queue_msk &= ~BIT(queue);
793 mvm_sta->tid_disable_agg |= BIT(tid);
794 }
795
796 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
797 cmd.sta_id = mvm_sta->sta_id;
798 cmd.add_modify = STA_MODE_MODIFY;
799 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
800 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
801 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
802
803 status = ADD_STA_SUCCESS;
Max Stepanov5a258aa2013-04-07 09:11:21 +0300804 ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100805 if (ret)
806 return ret;
807
808 switch (status) {
809 case ADD_STA_SUCCESS:
810 break;
811 default:
812 ret = -EIO;
813 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
814 start ? "start" : "stopp", status);
815 break;
816 }
817
818 return ret;
819}
820
821static const u8 tid_to_ac[] = {
822 IEEE80211_AC_BE,
823 IEEE80211_AC_BK,
824 IEEE80211_AC_BK,
825 IEEE80211_AC_BE,
826 IEEE80211_AC_VI,
827 IEEE80211_AC_VI,
828 IEEE80211_AC_VO,
829 IEEE80211_AC_VO,
830};
831
832int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
833 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
834{
835 struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
836 struct iwl_mvm_tid_data *tid_data;
837 int txq_id;
838
839 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
840 return -EINVAL;
841
842 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
843 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
844 mvmsta->tid_data[tid].state);
845 return -ENXIO;
846 }
847
848 lockdep_assert_held(&mvm->mutex);
849
850 for (txq_id = IWL_MVM_FIRST_AGG_QUEUE;
851 txq_id <= IWL_MVM_LAST_AGG_QUEUE; txq_id++)
852 if (mvm->queue_to_mac80211[txq_id] ==
853 IWL_INVALID_MAC80211_QUEUE)
854 break;
855
856 if (txq_id > IWL_MVM_LAST_AGG_QUEUE) {
857 IWL_ERR(mvm, "Failed to allocate agg queue\n");
858 return -EIO;
859 }
860
861 /* the new tx queue is still connected to the same mac80211 queue */
862 mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_ac[tid]];
863
864 spin_lock_bh(&mvmsta->lock);
865 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +0100866 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100867 tid_data->txq_id = txq_id;
868 *ssn = tid_data->ssn;
869
870 IWL_DEBUG_TX_QUEUES(mvm,
871 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
872 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
873 tid_data->next_reclaimed);
874
875 if (tid_data->ssn == tid_data->next_reclaimed) {
876 tid_data->state = IWL_AGG_STARTING;
877 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
878 } else {
879 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
880 }
881
882 spin_unlock_bh(&mvmsta->lock);
883
884 return 0;
885}
886
887int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
888 struct ieee80211_sta *sta, u16 tid, u8 buf_size)
889{
890 struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
891 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
892 int queue, fifo, ret;
893 u16 ssn;
894
895 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
896
897 spin_lock_bh(&mvmsta->lock);
898 ssn = tid_data->ssn;
899 queue = tid_data->txq_id;
900 tid_data->state = IWL_AGG_ON;
901 tid_data->ssn = 0xffff;
902 spin_unlock_bh(&mvmsta->lock);
903
904 fifo = iwl_mvm_ac_to_tx_fifo[tid_to_ac[tid]];
905
906 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
907 if (ret)
908 return -EIO;
909
910 iwl_trans_txq_enable(mvm->trans, queue, fifo, mvmsta->sta_id, tid,
911 buf_size, ssn);
912
913 /*
914 * Even though in theory the peer could have different
915 * aggregation reorder buffer sizes for different sessions,
916 * our ucode doesn't allow for that and has a global limit
917 * for each station. Therefore, use the minimum of all the
918 * aggregation sessions and our default value.
919 */
920 mvmsta->max_agg_bufsize =
921 min(mvmsta->max_agg_bufsize, buf_size);
922 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
923
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300924 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
925 sta->addr, tid);
926
Johannes Berg8ca151b2013-01-24 14:25:36 +0100927 if (mvm->cfg->ht_params->use_rts_for_aggregation) {
928 /*
929 * switch to RTS/CTS if it is the prefer protection
930 * method for HT traffic
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300931 * this function also sends the LQ command
Johannes Berg8ca151b2013-01-24 14:25:36 +0100932 */
Johannes Berge126b5d2013-06-28 13:39:18 +0200933 return iwl_mvm_tx_protection(mvm, mvmsta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100934 /*
935 * TODO: remove the TLC_RTS flag when we tear down the last
936 * AGG session (agg_tids_count in DVM)
937 */
938 }
939
Johannes Berg8ca151b2013-01-24 14:25:36 +0100940 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, CMD_ASYNC, false);
941}
942
943int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
944 struct ieee80211_sta *sta, u16 tid)
945{
946 struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
947 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
948 u16 txq_id;
949 int err;
950
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +0200951
952 /*
953 * If mac80211 is cleaning its state, then say that we finished since
954 * our state has been cleared anyway.
955 */
956 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
957 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
958 return 0;
959 }
960
Johannes Berg8ca151b2013-01-24 14:25:36 +0100961 spin_lock_bh(&mvmsta->lock);
962
963 txq_id = tid_data->txq_id;
964
965 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
966 mvmsta->sta_id, tid, txq_id, tid_data->state);
967
968 switch (tid_data->state) {
969 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +0100970 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100971
972 IWL_DEBUG_TX_QUEUES(mvm,
973 "ssn = %d, next_recl = %d\n",
974 tid_data->ssn, tid_data->next_reclaimed);
975
976 /* There are still packets for this RA / TID in the HW */
977 if (tid_data->ssn != tid_data->next_reclaimed) {
978 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
979 err = 0;
980 break;
981 }
982
983 tid_data->ssn = 0xffff;
984 iwl_trans_txq_disable(mvm->trans, txq_id);
985 /* fall through */
986 case IWL_AGG_STARTING:
987 case IWL_EMPTYING_HW_QUEUE_ADDBA:
988 /*
989 * The agg session has been stopped before it was set up. This
990 * can happen when the AddBA timer times out for example.
991 */
992
993 /* No barriers since we are under mutex */
994 lockdep_assert_held(&mvm->mutex);
995 mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
996
997 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
998 tid_data->state = IWL_AGG_OFF;
999 err = 0;
1000 break;
1001 default:
1002 IWL_ERR(mvm,
1003 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
1004 mvmsta->sta_id, tid, tid_data->state);
1005 IWL_ERR(mvm,
1006 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
1007 err = -EINVAL;
1008 }
1009
1010 spin_unlock_bh(&mvmsta->lock);
1011
1012 return err;
1013}
1014
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001015int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1016 struct ieee80211_sta *sta, u16 tid)
1017{
1018 struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
1019 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1020 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02001021 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001022
1023 /*
1024 * First set the agg state to OFF to avoid calling
1025 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
1026 */
1027 spin_lock_bh(&mvmsta->lock);
1028 txq_id = tid_data->txq_id;
1029 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
1030 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001031 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001032 tid_data->state = IWL_AGG_OFF;
1033 spin_unlock_bh(&mvmsta->lock);
1034
Johannes Bergb6658ff2013-07-24 13:55:51 +02001035 if (old_state >= IWL_AGG_ON) {
1036 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true))
1037 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001038
Johannes Bergb6658ff2013-07-24 13:55:51 +02001039 iwl_trans_txq_disable(mvm->trans, tid_data->txq_id);
1040 }
1041
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001042 mvm->queue_to_mac80211[tid_data->txq_id] =
1043 IWL_INVALID_MAC80211_QUEUE;
1044
1045 return 0;
1046}
1047
Johannes Berg8ca151b2013-01-24 14:25:36 +01001048static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
1049{
1050 int i;
1051
1052 lockdep_assert_held(&mvm->mutex);
1053
1054 i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM);
1055
1056 if (i == STA_KEY_MAX_NUM)
1057 return STA_KEY_IDX_INVALID;
1058
1059 __set_bit(i, mvm->fw_key_table);
1060
1061 return i;
1062}
1063
1064static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
1065 struct ieee80211_sta *sta)
1066{
1067 struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
1068
1069 if (sta) {
1070 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1071
1072 return mvm_sta->sta_id;
1073 }
1074
1075 /*
1076 * The device expects GTKs for station interfaces to be
1077 * installed as GTKs for the AP station. If we have no
1078 * station ID, then use AP's station ID.
1079 */
1080 if (vif->type == NL80211_IFTYPE_STATION &&
1081 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
1082 return mvmvif->ap_sta_id;
1083
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001084 return IWL_MVM_STATION_COUNT;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001085}
1086
1087static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
1088 struct iwl_mvm_sta *mvm_sta,
1089 struct ieee80211_key_conf *keyconf,
1090 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1091 u32 cmd_flags)
1092{
1093 __le16 key_flags;
Max Stepanov5a258aa2013-04-07 09:11:21 +03001094 struct iwl_mvm_add_sta_key_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001095 int ret, status;
1096 u16 keyidx;
1097 int i;
Max Stepanov5a258aa2013-04-07 09:11:21 +03001098 u32 mac_id_n_color = mvm_sta->mac_id_n_color;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001099
1100 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1101 STA_KEY_FLG_KEYID_MSK;
1102 key_flags = cpu_to_le16(keyidx);
1103 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
1104
1105 switch (keyconf->cipher) {
1106 case WLAN_CIPHER_SUITE_TKIP:
1107 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001108 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001109 for (i = 0; i < 5; i++)
Max Stepanov5a258aa2013-04-07 09:11:21 +03001110 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1111 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001112 break;
1113 case WLAN_CIPHER_SUITE_CCMP:
1114 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001115 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001116 break;
1117 default:
1118 WARN_ON(1);
1119 return -EINVAL;
1120 }
1121
1122 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1123 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1124
Max Stepanov5a258aa2013-04-07 09:11:21 +03001125 cmd.key_offset = keyconf->hw_key_idx;
1126 cmd.key_flags = key_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001127 cmd.sta_id = sta_id;
1128
1129 status = ADD_STA_SUCCESS;
1130 if (cmd_flags == CMD_SYNC)
Max Stepanov5a258aa2013-04-07 09:11:21 +03001131 ret = iwl_mvm_send_add_sta_key_cmd_status(mvm, &cmd,
1132 mac_id_n_color,
1133 &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001134 else
Max Stepanov5a258aa2013-04-07 09:11:21 +03001135 ret = iwl_mvm_send_add_sta_key_cmd(mvm, CMD_ASYNC, &cmd,
1136 mac_id_n_color);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001137
1138 switch (status) {
1139 case ADD_STA_SUCCESS:
1140 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1141 break;
1142 default:
1143 ret = -EIO;
1144 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1145 break;
1146 }
1147
1148 return ret;
1149}
1150
1151static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1152 struct ieee80211_key_conf *keyconf,
1153 u8 sta_id, bool remove_key)
1154{
1155 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1156
1157 /* verify the key details match the required command's expectations */
1158 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1159 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1160 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1161 return -EINVAL;
1162
1163 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1164 igtk_cmd.sta_id = cpu_to_le32(sta_id);
1165
1166 if (remove_key) {
1167 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1168 } else {
1169 struct ieee80211_key_seq seq;
1170 const u8 *pn;
1171
1172 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
1173 ieee80211_aes_cmac_calculate_k1_k2(keyconf,
1174 igtk_cmd.K1, igtk_cmd.K2);
1175 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1176 pn = seq.aes_cmac.pn;
1177 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1178 ((u64) pn[4] << 8) |
1179 ((u64) pn[3] << 16) |
1180 ((u64) pn[2] << 24) |
1181 ((u64) pn[1] << 32) |
1182 ((u64) pn[0] << 40));
1183 }
1184
1185 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1186 remove_key ? "removing" : "installing",
1187 igtk_cmd.sta_id);
1188
1189 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, CMD_SYNC,
1190 sizeof(igtk_cmd), &igtk_cmd);
1191}
1192
1193
1194static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1195 struct ieee80211_vif *vif,
1196 struct ieee80211_sta *sta)
1197{
1198 struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
1199
1200 if (sta)
1201 return sta->addr;
1202
1203 if (vif->type == NL80211_IFTYPE_STATION &&
1204 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1205 u8 sta_id = mvmvif->ap_sta_id;
1206 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1207 lockdep_is_held(&mvm->mutex));
1208 return sta->addr;
1209 }
1210
1211
1212 return NULL;
1213}
1214
1215int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1216 struct ieee80211_vif *vif,
1217 struct ieee80211_sta *sta,
1218 struct ieee80211_key_conf *keyconf,
1219 bool have_key_offset)
1220{
1221 struct iwl_mvm_sta *mvm_sta;
1222 int ret;
1223 u8 *addr, sta_id;
1224 struct ieee80211_key_seq seq;
1225 u16 p1k[5];
1226
1227 lockdep_assert_held(&mvm->mutex);
1228
1229 /* Get the station id from the mvm local station table */
1230 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001231 if (sta_id == IWL_MVM_STATION_COUNT) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001232 IWL_ERR(mvm, "Failed to find station id\n");
1233 return -EINVAL;
1234 }
1235
1236 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1237 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1238 goto end;
1239 }
1240
1241 /*
1242 * It is possible that the 'sta' parameter is NULL, and thus
1243 * there is a need to retrieve the sta from the local station table.
1244 */
1245 if (!sta) {
1246 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1247 lockdep_is_held(&mvm->mutex));
1248 if (IS_ERR_OR_NULL(sta)) {
1249 IWL_ERR(mvm, "Invalid station id\n");
1250 return -EINVAL;
1251 }
1252 }
1253
1254 mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1255 if (WARN_ON_ONCE(mvm_sta->vif != vif))
1256 return -EINVAL;
1257
1258 if (!have_key_offset) {
1259 /*
1260 * The D3 firmware hardcodes the PTK offset to 0, so we have to
1261 * configure it there. As a result, this workaround exists to
1262 * let the caller set the key offset (hw_key_idx), see d3.c.
1263 */
1264 keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
1265 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1266 return -ENOSPC;
1267 }
1268
1269 switch (keyconf->cipher) {
1270 case WLAN_CIPHER_SUITE_TKIP:
1271 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1272 /* get phase 1 key from mac80211 */
1273 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1274 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1275 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1276 seq.tkip.iv32, p1k, CMD_SYNC);
1277 break;
1278 case WLAN_CIPHER_SUITE_CCMP:
1279 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1280 0, NULL, CMD_SYNC);
1281 break;
1282 default:
1283 IWL_ERR(mvm, "Unknown cipher %x\n", keyconf->cipher);
1284 ret = -EINVAL;
1285 }
1286
1287 if (ret)
1288 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1289
1290end:
1291 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1292 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1293 sta->addr, ret);
1294 return ret;
1295}
1296
1297int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1298 struct ieee80211_vif *vif,
1299 struct ieee80211_sta *sta,
1300 struct ieee80211_key_conf *keyconf)
1301{
1302 struct iwl_mvm_sta *mvm_sta;
Max Stepanov5a258aa2013-04-07 09:11:21 +03001303 struct iwl_mvm_add_sta_key_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001304 __le16 key_flags;
1305 int ret, status;
1306 u8 sta_id;
1307
1308 lockdep_assert_held(&mvm->mutex);
1309
1310 /* Get the station id from the mvm local station table */
1311 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1312
1313 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1314 keyconf->keyidx, sta_id);
1315
1316 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1317 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1318
1319 ret = __test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1320 if (!ret) {
1321 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1322 keyconf->hw_key_idx);
1323 return -ENOENT;
1324 }
1325
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001326 if (sta_id == IWL_MVM_STATION_COUNT) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001327 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1328 return 0;
1329 }
1330
1331 /*
1332 * It is possible that the 'sta' parameter is NULL, and thus
1333 * there is a need to retrieve the sta from the local station table,
1334 * for example when a GTK is removed (where the sta_id will then be
1335 * the AP ID, and no station was passed by mac80211.)
1336 */
1337 if (!sta) {
1338 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1339 lockdep_is_held(&mvm->mutex));
1340 if (!sta) {
1341 IWL_ERR(mvm, "Invalid station id\n");
1342 return -EINVAL;
1343 }
1344 }
1345
1346 mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1347 if (WARN_ON_ONCE(mvm_sta->vif != vif))
1348 return -EINVAL;
1349
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02001350 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1351 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001352 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1353 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1354
1355 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1356 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1357
Max Stepanov5a258aa2013-04-07 09:11:21 +03001358 cmd.key_flags = key_flags;
1359 cmd.key_offset = keyconf->hw_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001360 cmd.sta_id = sta_id;
1361
Johannes Berg8ca151b2013-01-24 14:25:36 +01001362 status = ADD_STA_SUCCESS;
Max Stepanov5a258aa2013-04-07 09:11:21 +03001363 ret = iwl_mvm_send_add_sta_key_cmd_status(mvm, &cmd,
1364 mvm_sta->mac_id_n_color,
1365 &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001366
1367 switch (status) {
1368 case ADD_STA_SUCCESS:
1369 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1370 break;
1371 default:
1372 ret = -EIO;
1373 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1374 break;
1375 }
1376
1377 return ret;
1378}
1379
1380void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1381 struct ieee80211_vif *vif,
1382 struct ieee80211_key_conf *keyconf,
1383 struct ieee80211_sta *sta, u32 iv32,
1384 u16 *phase1key)
1385{
Beni Levc3eb5362013-02-06 17:22:18 +02001386 struct iwl_mvm_sta *mvm_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001387 u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1388
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001389 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001390 return;
1391
Beni Levc3eb5362013-02-06 17:22:18 +02001392 rcu_read_lock();
1393
1394 if (!sta) {
1395 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1396 if (WARN_ON(IS_ERR_OR_NULL(sta))) {
1397 rcu_read_unlock();
1398 return;
1399 }
1400 }
1401
1402 mvm_sta = (void *)sta->drv_priv;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001403 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1404 iv32, phase1key, CMD_ASYNC);
Beni Levc3eb5362013-02-06 17:22:18 +02001405 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01001406}
1407
Johannes Berg9cc40712013-02-15 22:47:48 +01001408void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
1409 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001410{
Johannes Berg9cc40712013-02-15 22:47:48 +01001411 struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
Max Stepanov5a258aa2013-04-07 09:11:21 +03001412 struct iwl_mvm_add_sta_cmd_v6 cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001413 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01001414 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03001415 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01001416 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01001417 };
1418 int ret;
1419
Max Stepanov5a258aa2013-04-07 09:11:21 +03001420 ret = iwl_mvm_send_add_sta_cmd(mvm, CMD_ASYNC, &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001421 if (ret)
1422 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1423}
1424
Johannes Berg9cc40712013-02-15 22:47:48 +01001425void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
1426 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001427 enum ieee80211_frame_release_type reason,
1428 u16 cnt)
1429{
1430 u16 sleep_state_flags =
1431 (reason == IEEE80211_FRAME_RELEASE_UAPSD) ?
1432 STA_SLEEP_STATE_UAPSD : STA_SLEEP_STATE_PS_POLL;
Johannes Berg9cc40712013-02-15 22:47:48 +01001433 struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
Max Stepanov5a258aa2013-04-07 09:11:21 +03001434 struct iwl_mvm_add_sta_cmd_v6 cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001435 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01001436 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001437 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1438 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01001439 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01001440 /*
1441 * Same modify mask for sleep_tx_count and sleep_state_flags so
1442 * we must set the sleep_state_flags too.
1443 */
1444 .sleep_state_flags = cpu_to_le16(sleep_state_flags),
1445 };
1446 int ret;
1447
1448 /* TODO: somehow the fw doesn't seem to take PS_POLL into account */
Max Stepanov5a258aa2013-04-07 09:11:21 +03001449 ret = iwl_mvm_send_add_sta_cmd(mvm, CMD_ASYNC, &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001450 if (ret)
1451 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1452}