]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/net/wireless/iwlwifi/mvm/sta.c
Merge remote-tracking branch 'wireless-next/master' into iwlwifi-next
[mirror_ubuntu-eoan-kernel.git] / drivers / net / wireless / iwlwifi / mvm / sta.c
CommitLineData
8ca151b5
JB
1/******************************************************************************
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
410dc5aa 25 * in the file called COPYING.
8ca151b5
JB
26 *
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"
9ee718aa 67#include "rs.h"
8ca151b5
JB
68
69static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm)
70{
71 int sta_id;
72
73 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
74
75 lockdep_assert_held(&mvm->mutex);
76
77 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
78 for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++)
79 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
80 lockdep_is_held(&mvm->mutex)))
81 return sta_id;
82 return IWL_MVM_STATION_COUNT;
83}
84
7a453973
JB
85/* send station add/update command to firmware */
86int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
87 bool update)
8ca151b5
JB
88{
89 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
90 struct iwl_mvm_add_sta_cmd add_sta_cmd;
91 int ret;
92 u32 status;
93 u32 agg_size = 0, mpdu_dens = 0;
94
95 memset(&add_sta_cmd, 0, sizeof(add_sta_cmd));
96
97 add_sta_cmd.sta_id = mvm_sta->sta_id;
98 add_sta_cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
7a453973
JB
99 if (!update) {
100 add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
101 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
102 }
103 add_sta_cmd.add_modify = update ? 1 : 0;
8ca151b5 104
5bc5aaad
JB
105 add_sta_cmd.station_flags_msk |= cpu_to_le32(STA_FLG_FAT_EN_MSK |
106 STA_FLG_MIMO_EN_MSK);
107
108 switch (sta->bandwidth) {
109 case IEEE80211_STA_RX_BW_160:
110 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
111 /* fall through */
112 case IEEE80211_STA_RX_BW_80:
113 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
114 /* fall through */
115 case IEEE80211_STA_RX_BW_40:
116 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
117 /* fall through */
118 case IEEE80211_STA_RX_BW_20:
119 if (sta->ht_cap.ht_supported)
120 add_sta_cmd.station_flags |=
121 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
122 break;
123 }
124
125 switch (sta->rx_nss) {
126 case 1:
127 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
128 break;
129 case 2:
130 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
131 break;
132 case 3 ... 8:
133 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
134 break;
135 }
136
137 switch (sta->smps_mode) {
138 case IEEE80211_SMPS_AUTOMATIC:
139 case IEEE80211_SMPS_NUM_MODES:
140 WARN_ON(1);
141 break;
142 case IEEE80211_SMPS_STATIC:
143 /* override NSS */
144 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
145 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
146 break;
147 case IEEE80211_SMPS_DYNAMIC:
148 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
149 break;
150 case IEEE80211_SMPS_OFF:
151 /* nothing */
152 break;
153 }
8ca151b5
JB
154
155 if (sta->ht_cap.ht_supported) {
156 add_sta_cmd.station_flags_msk |=
157 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
158 STA_FLG_AGG_MPDU_DENS_MSK);
159
160 mpdu_dens = sta->ht_cap.ampdu_density;
161 }
162
163 if (sta->vht_cap.vht_supported) {
164 agg_size = sta->vht_cap.cap &
165 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
166 agg_size >>=
167 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
168 } else if (sta->ht_cap.ht_supported) {
169 agg_size = sta->ht_cap.ampdu_factor;
170 }
171
172 add_sta_cmd.station_flags |=
173 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
174 add_sta_cmd.station_flags |=
175 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
176
177 status = ADD_STA_SUCCESS;
178 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(add_sta_cmd),
179 &add_sta_cmd, &status);
180 if (ret)
181 return ret;
182
183 switch (status) {
184 case ADD_STA_SUCCESS:
185 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
186 break;
187 default:
188 ret = -EIO;
189 IWL_ERR(mvm, "ADD_STA failed\n");
190 break;
191 }
192
193 return ret;
194}
195
196int iwl_mvm_add_sta(struct iwl_mvm *mvm,
197 struct ieee80211_vif *vif,
198 struct ieee80211_sta *sta)
199{
200 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
201 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
202 int i, ret, sta_id;
203
204 lockdep_assert_held(&mvm->mutex);
205
206 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
207 sta_id = iwl_mvm_find_free_sta_id(mvm);
208 else
209 sta_id = mvm_sta->sta_id;
210
211 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
212 return -ENOSPC;
213
214 spin_lock_init(&mvm_sta->lock);
215
216 mvm_sta->sta_id = sta_id;
217 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
218 mvmvif->color);
219 mvm_sta->vif = vif;
220 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
9ee718aa
EL
221 mvm_sta->tx_protection = 0;
222 mvm_sta->tt_tx_protection = false;
8ca151b5
JB
223
224 /* HW restart, don't assume the memory has been zeroed */
e3d4bc8c 225 atomic_set(&mvm->pending_frames[sta_id], 0);
8ca151b5
JB
226 mvm_sta->tid_disable_agg = 0;
227 mvm_sta->tfd_queue_msk = 0;
228 for (i = 0; i < IEEE80211_NUM_ACS; i++)
229 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
230 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
231
232 if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
233 mvm_sta->tfd_queue_msk |= BIT(vif->cab_queue);
234
235 /* for HW restart - need to reset the seq_number etc... */
236 memset(mvm_sta->tid_data, 0, sizeof(mvm_sta->tid_data));
237
7a453973 238 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
8ca151b5
JB
239 if (ret)
240 return ret;
241
242 /* The first station added is the AP, the others are TDLS STAs */
243 if (vif->type == NL80211_IFTYPE_STATION &&
244 mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
245 mvmvif->ap_sta_id = sta_id;
246
247 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
248
249 return 0;
250}
251
7a453973
JB
252int iwl_mvm_update_sta(struct iwl_mvm *mvm,
253 struct ieee80211_vif *vif,
254 struct ieee80211_sta *sta)
255{
256 return iwl_mvm_sta_send_to_fw(mvm, sta, true);
257}
258
8ca151b5
JB
259int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
260 bool drain)
261{
262 struct iwl_mvm_add_sta_cmd cmd = {};
263 int ret;
264 u32 status;
265
266 lockdep_assert_held(&mvm->mutex);
267
268 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
269 cmd.sta_id = mvmsta->sta_id;
270 cmd.add_modify = STA_MODE_MODIFY;
271 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
272 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
273
274 status = ADD_STA_SUCCESS;
275 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
276 &cmd, &status);
277 if (ret)
278 return ret;
279
280 switch (status) {
281 case ADD_STA_SUCCESS:
282 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
283 mvmsta->sta_id);
284 break;
285 default:
286 ret = -EIO;
287 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
288 mvmsta->sta_id);
289 break;
290 }
291
292 return ret;
293}
294
295/*
296 * Remove a station from the FW table. Before sending the command to remove
297 * the station validate that the station is indeed known to the driver (sanity
298 * only).
299 */
300static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
301{
302 struct ieee80211_sta *sta;
303 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
304 .sta_id = sta_id,
305 };
306 int ret;
307
308 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
309 lockdep_is_held(&mvm->mutex));
310
311 /* Note: internal stations are marked as error values */
312 if (!sta) {
313 IWL_ERR(mvm, "Invalid station id\n");
314 return -EINVAL;
315 }
316
317 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, CMD_SYNC,
318 sizeof(rm_sta_cmd), &rm_sta_cmd);
319 if (ret) {
320 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
321 return ret;
322 }
323
324 return 0;
325}
326
327void iwl_mvm_sta_drained_wk(struct work_struct *wk)
328{
329 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
330 u8 sta_id;
331
332 /*
333 * The mutex is needed because of the SYNC cmd, but not only: if the
334 * work would run concurrently with iwl_mvm_rm_sta, it would run before
335 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
336 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
337 * that later.
338 */
339 mutex_lock(&mvm->mutex);
340
341 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
342 int ret;
343 struct ieee80211_sta *sta =
344 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
345 lockdep_is_held(&mvm->mutex));
346
347 /* This station is in use */
348 if (!IS_ERR(sta))
349 continue;
350
351 if (PTR_ERR(sta) == -EINVAL) {
352 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
353 sta_id);
354 continue;
355 }
356
357 if (!sta) {
358 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
359 sta_id);
360 continue;
361 }
362
363 WARN_ON(PTR_ERR(sta) != -EBUSY);
364 /* This station was removed and we waited until it got drained,
365 * we can now proceed and remove it.
366 */
367 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
368 if (ret) {
369 IWL_ERR(mvm,
370 "Couldn't remove sta %d after it was drained\n",
371 sta_id);
372 continue;
373 }
374 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], NULL);
375 clear_bit(sta_id, mvm->sta_drained);
376 }
377
378 mutex_unlock(&mvm->mutex);
379}
380
381int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
382 struct ieee80211_vif *vif,
383 struct ieee80211_sta *sta)
384{
385 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
386 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
387 int ret;
388
389 lockdep_assert_held(&mvm->mutex);
390
391 if (vif->type == NL80211_IFTYPE_STATION &&
392 mvmvif->ap_sta_id == mvm_sta->sta_id) {
80d85655
EG
393 /* flush its queues here since we are freeing mvm_sta */
394 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true);
395
8ca151b5
JB
396 /*
397 * Put a non-NULL since the fw station isn't removed.
398 * It will be removed after the MAC will be set as
399 * unassoc.
400 */
401 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
402 ERR_PTR(-EINVAL));
403
8ca151b5
JB
404 /* if we are associated - we can't remove the AP STA now */
405 if (vif->bss_conf.assoc)
406 return ret;
407
408 /* unassoc - go ahead - remove the AP STA now */
409 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
410 }
411
e3d4bc8c
EG
412 /*
413 * Make sure that the tx response code sees the station as -EBUSY and
414 * calls the drain worker.
415 */
416 spin_lock_bh(&mvm_sta->lock);
8ca151b5
JB
417 /*
418 * There are frames pending on the AC queues for this station.
419 * We need to wait until all the frames are drained...
420 */
e3d4bc8c 421 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
8ca151b5
JB
422 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
423 ERR_PTR(-EBUSY));
e3d4bc8c
EG
424 spin_unlock_bh(&mvm_sta->lock);
425 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
8ca151b5 426 } else {
e3d4bc8c 427 spin_unlock_bh(&mvm_sta->lock);
8ca151b5
JB
428 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
429 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
430 }
431
432 return ret;
433}
434
435int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
436 struct ieee80211_vif *vif,
437 u8 sta_id)
438{
439 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
440
441 lockdep_assert_held(&mvm->mutex);
442
443 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], NULL);
444 return ret;
445}
446
447int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta,
448 u32 qmask)
449{
450 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
451 sta->sta_id = iwl_mvm_find_free_sta_id(mvm);
452 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
453 return -ENOSPC;
454 }
455
456 sta->tfd_queue_msk = qmask;
457
458 /* put a non-NULL value so iterating over the stations won't stop */
459 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
460 return 0;
461}
462
463void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
464{
465 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
466 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
467 sta->sta_id = IWL_MVM_STATION_COUNT;
468}
469
470static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
471 struct iwl_mvm_int_sta *sta,
472 const u8 *addr,
473 u16 mac_id, u16 color)
474{
475 struct iwl_mvm_add_sta_cmd cmd;
476 int ret;
477 u32 status;
478
479 lockdep_assert_held(&mvm->mutex);
480
481 memset(&cmd, 0, sizeof(struct iwl_mvm_add_sta_cmd));
482 cmd.sta_id = sta->sta_id;
483 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
484 color));
485
486 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
487
488 if (addr)
489 memcpy(cmd.addr, addr, ETH_ALEN);
490
491 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
492 &cmd, &status);
493 if (ret)
494 return ret;
495
496 switch (status) {
497 case ADD_STA_SUCCESS:
498 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
499 return 0;
500 default:
501 ret = -EIO;
502 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
503 status);
504 break;
505 }
506 return ret;
507}
508
509int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
510{
511 int ret;
512
513 lockdep_assert_held(&mvm->mutex);
514
515 /* Add the aux station, but without any queues */
516 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, 0);
517 if (ret)
518 return ret;
519
520 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
521 MAC_INDEX_AUX, 0);
522
523 if (ret)
524 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
525 return ret;
526}
527
528/*
529 * Send the add station command for the vif's broadcast station.
530 * Assumes that the station was already allocated.
531 *
532 * @mvm: the mvm component
533 * @vif: the interface to which the broadcast station is added
534 * @bsta: the broadcast station to add.
535 */
536int iwl_mvm_send_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
537 struct iwl_mvm_int_sta *bsta)
538{
539 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
540 static const u8 baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
541
542 lockdep_assert_held(&mvm->mutex);
543
544 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
545 return -ENOSPC;
546
547 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
548 mvmvif->id, mvmvif->color);
549}
550
551/* Send the FW a request to remove the station from it's internal data
552 * structures, but DO NOT remove the entry from the local data structures. */
553int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm,
554 struct iwl_mvm_int_sta *bsta)
555{
556 int ret;
557
558 lockdep_assert_held(&mvm->mutex);
559
560 ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
561 if (ret)
562 IWL_WARN(mvm, "Failed sending remove station\n");
563 return ret;
564}
565
566/* Allocate a new station entry for the broadcast station to the given vif,
567 * and send it to the FW.
568 * Note that each P2P mac should have its own broadcast station.
569 *
570 * @mvm: the mvm component
571 * @vif: the interface to which the broadcast station is added
572 * @bsta: the broadcast station to add. */
573int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
574 struct iwl_mvm_int_sta *bsta)
575{
576 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
577 static const u8 baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
578 u32 qmask;
579 int ret;
580
581 lockdep_assert_held(&mvm->mutex);
582
583 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
584 ret = iwl_mvm_allocate_int_sta(mvm, bsta, qmask);
585 if (ret)
586 return ret;
587
588 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
589 mvmvif->id, mvmvif->color);
590
591 if (ret)
592 iwl_mvm_dealloc_int_sta(mvm, bsta);
593 return ret;
594}
595
596/*
597 * Send the FW a request to remove the station from it's internal data
598 * structures, and in addition remove it from the local data structure.
599 */
600int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *bsta)
601{
602 int ret;
603
604 lockdep_assert_held(&mvm->mutex);
605
606 ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
607 if (ret)
608 return ret;
609
610 iwl_mvm_dealloc_int_sta(mvm, bsta);
611 return ret;
612}
613
614int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
615 int tid, u16 ssn, bool start)
616{
617 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
618 struct iwl_mvm_add_sta_cmd cmd = {};
619 int ret;
620 u32 status;
621
622 lockdep_assert_held(&mvm->mutex);
623
624 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
625 cmd.sta_id = mvm_sta->sta_id;
626 cmd.add_modify = STA_MODE_MODIFY;
627 cmd.add_immediate_ba_tid = (u8) tid;
628 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
629 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
630 STA_MODIFY_REMOVE_BA_TID;
631
632 status = ADD_STA_SUCCESS;
633 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
634 &cmd, &status);
635 if (ret)
636 return ret;
637
638 switch (status) {
639 case ADD_STA_SUCCESS:
640 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
641 start ? "start" : "stopp");
642 break;
643 case ADD_STA_IMMEDIATE_BA_FAILURE:
644 IWL_WARN(mvm, "RX BA Session refused by fw\n");
645 ret = -ENOSPC;
646 break;
647 default:
648 ret = -EIO;
649 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
650 start ? "start" : "stopp", status);
651 break;
652 }
653
654 return ret;
655}
656
657static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
658 int tid, u8 queue, bool start)
659{
660 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
661 struct iwl_mvm_add_sta_cmd cmd = {};
662 int ret;
663 u32 status;
664
665 lockdep_assert_held(&mvm->mutex);
666
667 if (start) {
668 mvm_sta->tfd_queue_msk |= BIT(queue);
669 mvm_sta->tid_disable_agg &= ~BIT(tid);
670 } else {
671 mvm_sta->tfd_queue_msk &= ~BIT(queue);
672 mvm_sta->tid_disable_agg |= BIT(tid);
673 }
674
675 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
676 cmd.sta_id = mvm_sta->sta_id;
677 cmd.add_modify = STA_MODE_MODIFY;
678 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
679 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
680 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
681
682 status = ADD_STA_SUCCESS;
683 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
684 &cmd, &status);
685 if (ret)
686 return ret;
687
688 switch (status) {
689 case ADD_STA_SUCCESS:
690 break;
691 default:
692 ret = -EIO;
693 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
694 start ? "start" : "stopp", status);
695 break;
696 }
697
698 return ret;
699}
700
701static const u8 tid_to_ac[] = {
702 IEEE80211_AC_BE,
703 IEEE80211_AC_BK,
704 IEEE80211_AC_BK,
705 IEEE80211_AC_BE,
706 IEEE80211_AC_VI,
707 IEEE80211_AC_VI,
708 IEEE80211_AC_VO,
709 IEEE80211_AC_VO,
710};
711
712int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
713 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
714{
715 struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
716 struct iwl_mvm_tid_data *tid_data;
717 int txq_id;
718
719 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
720 return -EINVAL;
721
722 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
723 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
724 mvmsta->tid_data[tid].state);
725 return -ENXIO;
726 }
727
728 lockdep_assert_held(&mvm->mutex);
729
730 for (txq_id = IWL_MVM_FIRST_AGG_QUEUE;
731 txq_id <= IWL_MVM_LAST_AGG_QUEUE; txq_id++)
732 if (mvm->queue_to_mac80211[txq_id] ==
733 IWL_INVALID_MAC80211_QUEUE)
734 break;
735
736 if (txq_id > IWL_MVM_LAST_AGG_QUEUE) {
737 IWL_ERR(mvm, "Failed to allocate agg queue\n");
738 return -EIO;
739 }
740
741 /* the new tx queue is still connected to the same mac80211 queue */
742 mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_ac[tid]];
743
744 spin_lock_bh(&mvmsta->lock);
745 tid_data = &mvmsta->tid_data[tid];
9a886586 746 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
8ca151b5
JB
747 tid_data->txq_id = txq_id;
748 *ssn = tid_data->ssn;
749
750 IWL_DEBUG_TX_QUEUES(mvm,
751 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
752 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
753 tid_data->next_reclaimed);
754
755 if (tid_data->ssn == tid_data->next_reclaimed) {
756 tid_data->state = IWL_AGG_STARTING;
757 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
758 } else {
759 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
760 }
761
762 spin_unlock_bh(&mvmsta->lock);
763
764 return 0;
765}
766
767int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
768 struct ieee80211_sta *sta, u16 tid, u8 buf_size)
769{
770 struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
771 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
772 int queue, fifo, ret;
773 u16 ssn;
774
775 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
776
777 spin_lock_bh(&mvmsta->lock);
778 ssn = tid_data->ssn;
779 queue = tid_data->txq_id;
780 tid_data->state = IWL_AGG_ON;
781 tid_data->ssn = 0xffff;
782 spin_unlock_bh(&mvmsta->lock);
783
784 fifo = iwl_mvm_ac_to_tx_fifo[tid_to_ac[tid]];
785
786 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
787 if (ret)
788 return -EIO;
789
790 iwl_trans_txq_enable(mvm->trans, queue, fifo, mvmsta->sta_id, tid,
791 buf_size, ssn);
792
793 /*
794 * Even though in theory the peer could have different
795 * aggregation reorder buffer sizes for different sessions,
796 * our ucode doesn't allow for that and has a global limit
797 * for each station. Therefore, use the minimum of all the
798 * aggregation sessions and our default value.
799 */
800 mvmsta->max_agg_bufsize =
801 min(mvmsta->max_agg_bufsize, buf_size);
802 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
803
9ee718aa
EL
804 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
805 sta->addr, tid);
806
8ca151b5
JB
807 if (mvm->cfg->ht_params->use_rts_for_aggregation) {
808 /*
809 * switch to RTS/CTS if it is the prefer protection
810 * method for HT traffic
9ee718aa 811 * this function also sends the LQ command
8ca151b5 812 */
9ee718aa
EL
813 return iwl_mvm_tx_protection(mvm, &mvmsta->lq_sta.lq,
814 mvmsta, true);
8ca151b5
JB
815 /*
816 * TODO: remove the TLC_RTS flag when we tear down the last
817 * AGG session (agg_tids_count in DVM)
818 */
819 }
820
8ca151b5
JB
821 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, CMD_ASYNC, false);
822}
823
824int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
825 struct ieee80211_sta *sta, u16 tid)
826{
827 struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
828 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
829 u16 txq_id;
830 int err;
831
f9aa8dd3
EG
832
833 /*
834 * If mac80211 is cleaning its state, then say that we finished since
835 * our state has been cleared anyway.
836 */
837 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
838 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
839 return 0;
840 }
841
8ca151b5
JB
842 spin_lock_bh(&mvmsta->lock);
843
844 txq_id = tid_data->txq_id;
845
846 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
847 mvmsta->sta_id, tid, txq_id, tid_data->state);
848
849 switch (tid_data->state) {
850 case IWL_AGG_ON:
9a886586 851 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
8ca151b5
JB
852
853 IWL_DEBUG_TX_QUEUES(mvm,
854 "ssn = %d, next_recl = %d\n",
855 tid_data->ssn, tid_data->next_reclaimed);
856
857 /* There are still packets for this RA / TID in the HW */
858 if (tid_data->ssn != tid_data->next_reclaimed) {
859 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
860 err = 0;
861 break;
862 }
863
864 tid_data->ssn = 0xffff;
865 iwl_trans_txq_disable(mvm->trans, txq_id);
866 /* fall through */
867 case IWL_AGG_STARTING:
868 case IWL_EMPTYING_HW_QUEUE_ADDBA:
869 /*
870 * The agg session has been stopped before it was set up. This
871 * can happen when the AddBA timer times out for example.
872 */
873
874 /* No barriers since we are under mutex */
875 lockdep_assert_held(&mvm->mutex);
876 mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
877
878 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
879 tid_data->state = IWL_AGG_OFF;
880 err = 0;
881 break;
882 default:
883 IWL_ERR(mvm,
884 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
885 mvmsta->sta_id, tid, tid_data->state);
886 IWL_ERR(mvm,
887 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
888 err = -EINVAL;
889 }
890
891 spin_unlock_bh(&mvmsta->lock);
892
893 return err;
894}
895
e3d9e7ce
EG
896int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
897 struct ieee80211_sta *sta, u16 tid)
898{
899 struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
900 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
901 u16 txq_id;
902
903 /*
904 * First set the agg state to OFF to avoid calling
905 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
906 */
907 spin_lock_bh(&mvmsta->lock);
908 txq_id = tid_data->txq_id;
909 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
910 mvmsta->sta_id, tid, txq_id, tid_data->state);
911 tid_data->state = IWL_AGG_OFF;
912 spin_unlock_bh(&mvmsta->lock);
913
914 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true))
915 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
916
917 iwl_trans_txq_disable(mvm->trans, tid_data->txq_id);
918 mvm->queue_to_mac80211[tid_data->txq_id] =
919 IWL_INVALID_MAC80211_QUEUE;
920
921 return 0;
922}
923
8ca151b5
JB
924static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
925{
926 int i;
927
928 lockdep_assert_held(&mvm->mutex);
929
930 i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM);
931
932 if (i == STA_KEY_MAX_NUM)
933 return STA_KEY_IDX_INVALID;
934
935 __set_bit(i, mvm->fw_key_table);
936
937 return i;
938}
939
940static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
941 struct ieee80211_sta *sta)
942{
943 struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
944
945 if (sta) {
946 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
947
948 return mvm_sta->sta_id;
949 }
950
951 /*
952 * The device expects GTKs for station interfaces to be
953 * installed as GTKs for the AP station. If we have no
954 * station ID, then use AP's station ID.
955 */
956 if (vif->type == NL80211_IFTYPE_STATION &&
957 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
958 return mvmvif->ap_sta_id;
959
881acd89 960 return IWL_MVM_STATION_COUNT;
8ca151b5
JB
961}
962
963static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
964 struct iwl_mvm_sta *mvm_sta,
965 struct ieee80211_key_conf *keyconf,
966 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
967 u32 cmd_flags)
968{
969 __le16 key_flags;
970 struct iwl_mvm_add_sta_cmd cmd = {};
971 int ret, status;
972 u16 keyidx;
973 int i;
974
975 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
976 STA_KEY_FLG_KEYID_MSK;
977 key_flags = cpu_to_le16(keyidx);
978 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
979
980 switch (keyconf->cipher) {
981 case WLAN_CIPHER_SUITE_TKIP:
982 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
983 cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
984 for (i = 0; i < 5; i++)
985 cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
986 memcpy(cmd.key.key, keyconf->key, keyconf->keylen);
987 break;
988 case WLAN_CIPHER_SUITE_CCMP:
989 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
990 memcpy(cmd.key.key, keyconf->key, keyconf->keylen);
991 break;
992 default:
993 WARN_ON(1);
994 return -EINVAL;
995 }
996
997 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
998 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
999
1000 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1001 cmd.key.key_offset = keyconf->hw_key_idx;
1002 cmd.key.key_flags = key_flags;
1003 cmd.add_modify = STA_MODE_MODIFY;
1004 cmd.modify_mask = STA_MODIFY_KEY;
1005 cmd.sta_id = sta_id;
1006
1007 status = ADD_STA_SUCCESS;
1008 if (cmd_flags == CMD_SYNC)
1009 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
1010 &cmd, &status);
1011 else
1012 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
1013 sizeof(cmd), &cmd);
1014
1015 switch (status) {
1016 case ADD_STA_SUCCESS:
1017 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1018 break;
1019 default:
1020 ret = -EIO;
1021 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1022 break;
1023 }
1024
1025 return ret;
1026}
1027
1028static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1029 struct ieee80211_key_conf *keyconf,
1030 u8 sta_id, bool remove_key)
1031{
1032 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1033
1034 /* verify the key details match the required command's expectations */
1035 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1036 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1037 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1038 return -EINVAL;
1039
1040 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1041 igtk_cmd.sta_id = cpu_to_le32(sta_id);
1042
1043 if (remove_key) {
1044 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1045 } else {
1046 struct ieee80211_key_seq seq;
1047 const u8 *pn;
1048
1049 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
1050 ieee80211_aes_cmac_calculate_k1_k2(keyconf,
1051 igtk_cmd.K1, igtk_cmd.K2);
1052 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1053 pn = seq.aes_cmac.pn;
1054 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1055 ((u64) pn[4] << 8) |
1056 ((u64) pn[3] << 16) |
1057 ((u64) pn[2] << 24) |
1058 ((u64) pn[1] << 32) |
1059 ((u64) pn[0] << 40));
1060 }
1061
1062 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1063 remove_key ? "removing" : "installing",
1064 igtk_cmd.sta_id);
1065
1066 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, CMD_SYNC,
1067 sizeof(igtk_cmd), &igtk_cmd);
1068}
1069
1070
1071static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1072 struct ieee80211_vif *vif,
1073 struct ieee80211_sta *sta)
1074{
1075 struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
1076
1077 if (sta)
1078 return sta->addr;
1079
1080 if (vif->type == NL80211_IFTYPE_STATION &&
1081 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1082 u8 sta_id = mvmvif->ap_sta_id;
1083 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1084 lockdep_is_held(&mvm->mutex));
1085 return sta->addr;
1086 }
1087
1088
1089 return NULL;
1090}
1091
1092int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1093 struct ieee80211_vif *vif,
1094 struct ieee80211_sta *sta,
1095 struct ieee80211_key_conf *keyconf,
1096 bool have_key_offset)
1097{
1098 struct iwl_mvm_sta *mvm_sta;
1099 int ret;
1100 u8 *addr, sta_id;
1101 struct ieee80211_key_seq seq;
1102 u16 p1k[5];
1103
1104 lockdep_assert_held(&mvm->mutex);
1105
1106 /* Get the station id from the mvm local station table */
1107 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
881acd89 1108 if (sta_id == IWL_MVM_STATION_COUNT) {
8ca151b5
JB
1109 IWL_ERR(mvm, "Failed to find station id\n");
1110 return -EINVAL;
1111 }
1112
1113 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1114 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1115 goto end;
1116 }
1117
1118 /*
1119 * It is possible that the 'sta' parameter is NULL, and thus
1120 * there is a need to retrieve the sta from the local station table.
1121 */
1122 if (!sta) {
1123 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1124 lockdep_is_held(&mvm->mutex));
1125 if (IS_ERR_OR_NULL(sta)) {
1126 IWL_ERR(mvm, "Invalid station id\n");
1127 return -EINVAL;
1128 }
1129 }
1130
1131 mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1132 if (WARN_ON_ONCE(mvm_sta->vif != vif))
1133 return -EINVAL;
1134
1135 if (!have_key_offset) {
1136 /*
1137 * The D3 firmware hardcodes the PTK offset to 0, so we have to
1138 * configure it there. As a result, this workaround exists to
1139 * let the caller set the key offset (hw_key_idx), see d3.c.
1140 */
1141 keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
1142 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1143 return -ENOSPC;
1144 }
1145
1146 switch (keyconf->cipher) {
1147 case WLAN_CIPHER_SUITE_TKIP:
1148 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1149 /* get phase 1 key from mac80211 */
1150 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1151 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1152 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1153 seq.tkip.iv32, p1k, CMD_SYNC);
1154 break;
1155 case WLAN_CIPHER_SUITE_CCMP:
1156 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1157 0, NULL, CMD_SYNC);
1158 break;
1159 default:
1160 IWL_ERR(mvm, "Unknown cipher %x\n", keyconf->cipher);
1161 ret = -EINVAL;
1162 }
1163
1164 if (ret)
1165 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1166
1167end:
1168 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1169 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1170 sta->addr, ret);
1171 return ret;
1172}
1173
1174int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1175 struct ieee80211_vif *vif,
1176 struct ieee80211_sta *sta,
1177 struct ieee80211_key_conf *keyconf)
1178{
1179 struct iwl_mvm_sta *mvm_sta;
1180 struct iwl_mvm_add_sta_cmd cmd = {};
1181 __le16 key_flags;
1182 int ret, status;
1183 u8 sta_id;
1184
1185 lockdep_assert_held(&mvm->mutex);
1186
1187 /* Get the station id from the mvm local station table */
1188 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1189
1190 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1191 keyconf->keyidx, sta_id);
1192
1193 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1194 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1195
1196 ret = __test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1197 if (!ret) {
1198 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1199 keyconf->hw_key_idx);
1200 return -ENOENT;
1201 }
1202
881acd89 1203 if (sta_id == IWL_MVM_STATION_COUNT) {
8ca151b5
JB
1204 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1205 return 0;
1206 }
1207
1208 /*
1209 * It is possible that the 'sta' parameter is NULL, and thus
1210 * there is a need to retrieve the sta from the local station table,
1211 * for example when a GTK is removed (where the sta_id will then be
1212 * the AP ID, and no station was passed by mac80211.)
1213 */
1214 if (!sta) {
1215 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1216 lockdep_is_held(&mvm->mutex));
1217 if (!sta) {
1218 IWL_ERR(mvm, "Invalid station id\n");
1219 return -EINVAL;
1220 }
1221 }
1222
1223 mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1224 if (WARN_ON_ONCE(mvm_sta->vif != vif))
1225 return -EINVAL;
1226
8115efbd
EG
1227 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1228 STA_KEY_FLG_KEYID_MSK);
8ca151b5
JB
1229 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1230 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1231
1232 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1233 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1234
1235 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1236 cmd.key.key_flags = key_flags;
1237 cmd.key.key_offset = keyconf->hw_key_idx;
1238 cmd.sta_id = sta_id;
1239
1240 cmd.modify_mask = STA_MODIFY_KEY;
1241 cmd.add_modify = STA_MODE_MODIFY;
1242
1243 status = ADD_STA_SUCCESS;
1244 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
1245 &cmd, &status);
1246
1247 switch (status) {
1248 case ADD_STA_SUCCESS:
1249 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1250 break;
1251 default:
1252 ret = -EIO;
1253 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1254 break;
1255 }
1256
1257 return ret;
1258}
1259
1260void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1261 struct ieee80211_vif *vif,
1262 struct ieee80211_key_conf *keyconf,
1263 struct ieee80211_sta *sta, u32 iv32,
1264 u16 *phase1key)
1265{
c3eb536a 1266 struct iwl_mvm_sta *mvm_sta;
8ca151b5
JB
1267 u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1268
881acd89 1269 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
8ca151b5
JB
1270 return;
1271
c3eb536a
BL
1272 rcu_read_lock();
1273
1274 if (!sta) {
1275 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1276 if (WARN_ON(IS_ERR_OR_NULL(sta))) {
1277 rcu_read_unlock();
1278 return;
1279 }
1280 }
1281
1282 mvm_sta = (void *)sta->drv_priv;
8ca151b5
JB
1283 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1284 iv32, phase1key, CMD_ASYNC);
c3eb536a 1285 rcu_read_unlock();
8ca151b5
JB
1286}
1287
9cc40712
JB
1288void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
1289 struct ieee80211_sta *sta)
8ca151b5 1290{
9cc40712 1291 struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
8ca151b5
JB
1292 struct iwl_mvm_add_sta_cmd cmd = {
1293 .add_modify = STA_MODE_MODIFY,
9cc40712 1294 .sta_id = mvmsta->sta_id,
8ca151b5
JB
1295 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1296 .sleep_state_flags = cpu_to_le16(STA_SLEEP_STATE_AWAKE),
9cc40712 1297 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
8ca151b5
JB
1298 };
1299 int ret;
1300
1301 /*
1302 * Same modify mask for sleep_tx_count and sleep_state_flags but this
1303 * should be fine since if we set the STA as "awake", then
1304 * sleep_tx_count is not relevant.
1305 */
1306 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1307 if (ret)
1308 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1309}
1310
9cc40712
JB
1311void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
1312 struct ieee80211_sta *sta,
8ca151b5
JB
1313 enum ieee80211_frame_release_type reason,
1314 u16 cnt)
1315{
1316 u16 sleep_state_flags =
1317 (reason == IEEE80211_FRAME_RELEASE_UAPSD) ?
1318 STA_SLEEP_STATE_UAPSD : STA_SLEEP_STATE_PS_POLL;
9cc40712 1319 struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
8ca151b5
JB
1320 struct iwl_mvm_add_sta_cmd cmd = {
1321 .add_modify = STA_MODE_MODIFY,
9cc40712 1322 .sta_id = mvmsta->sta_id,
8ca151b5
JB
1323 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1324 .sleep_tx_count = cpu_to_le16(cnt),
9cc40712 1325 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
8ca151b5
JB
1326 /*
1327 * Same modify mask for sleep_tx_count and sleep_state_flags so
1328 * we must set the sleep_state_flags too.
1329 */
1330 .sleep_state_flags = cpu_to_le16(sleep_state_flags),
1331 };
1332 int ret;
1333
1334 /* TODO: somehow the fw doesn't seem to take PS_POLL into account */
1335 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1336 if (ret)
1337 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1338}