]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/wireless/iwlwifi/mvm/sta.c
iwlwifi: add Intel Mobile Communications copyright
[mirror_ubuntu-artful-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 *
51368bf7 8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
8b4139dc 9 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
8ca151b5
JB
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
23 * USA
24 *
25 * The full GNU General Public License is included in this distribution
410dc5aa 26 * in the file called COPYING.
8ca151b5
JB
27 *
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 *
32 * BSD LICENSE
33 *
51368bf7 34 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
8b4139dc 35 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
8ca151b5
JB
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 *
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in
46 * the documentation and/or other materials provided with the
47 * distribution.
48 * * Neither the name Intel Corporation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *
64 *****************************************************************************/
65#include <net/mac80211.h>
66
67#include "mvm.h"
68#include "sta.h"
9ee718aa 69#include "rs.h"
8ca151b5 70
b92e661b
EP
71static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
72 enum nl80211_iftype iftype)
8ca151b5
JB
73{
74 int sta_id;
b92e661b 75 u32 reserved_ids = 0;
8ca151b5 76
b92e661b 77 BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
8ca151b5
JB
78 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
79
80 lockdep_assert_held(&mvm->mutex);
81
b92e661b
EP
82 /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
83 if (iftype != NL80211_IFTYPE_STATION)
84 reserved_ids = BIT(0);
85
8ca151b5 86 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
b92e661b
EP
87 for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++) {
88 if (BIT(sta_id) & reserved_ids)
89 continue;
90
8ca151b5
JB
91 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
92 lockdep_is_held(&mvm->mutex)))
93 return sta_id;
b92e661b 94 }
8ca151b5
JB
95 return IWL_MVM_STATION_COUNT;
96}
97
7a453973
JB
98/* send station add/update command to firmware */
99int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
100 bool update)
8ca151b5
JB
101{
102 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
4b8265ab
EG
103 struct iwl_mvm_add_sta_cmd add_sta_cmd = {
104 .sta_id = mvm_sta->sta_id,
105 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
106 .add_modify = update ? 1 : 0,
107 .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
108 STA_FLG_MIMO_EN_MSK),
109 };
8ca151b5
JB
110 int ret;
111 u32 status;
112 u32 agg_size = 0, mpdu_dens = 0;
113
7a453973
JB
114 if (!update) {
115 add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
116 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
117 }
5bc5aaad
JB
118
119 switch (sta->bandwidth) {
120 case IEEE80211_STA_RX_BW_160:
121 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
122 /* fall through */
123 case IEEE80211_STA_RX_BW_80:
124 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
125 /* fall through */
126 case IEEE80211_STA_RX_BW_40:
127 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
128 /* fall through */
129 case IEEE80211_STA_RX_BW_20:
130 if (sta->ht_cap.ht_supported)
131 add_sta_cmd.station_flags |=
132 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
133 break;
134 }
135
136 switch (sta->rx_nss) {
137 case 1:
138 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
139 break;
140 case 2:
141 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
142 break;
143 case 3 ... 8:
144 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
145 break;
146 }
147
148 switch (sta->smps_mode) {
149 case IEEE80211_SMPS_AUTOMATIC:
150 case IEEE80211_SMPS_NUM_MODES:
151 WARN_ON(1);
152 break;
153 case IEEE80211_SMPS_STATIC:
154 /* override NSS */
155 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
156 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
157 break;
158 case IEEE80211_SMPS_DYNAMIC:
159 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
160 break;
161 case IEEE80211_SMPS_OFF:
162 /* nothing */
163 break;
164 }
8ca151b5
JB
165
166 if (sta->ht_cap.ht_supported) {
167 add_sta_cmd.station_flags_msk |=
168 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
169 STA_FLG_AGG_MPDU_DENS_MSK);
170
171 mpdu_dens = sta->ht_cap.ampdu_density;
172 }
173
174 if (sta->vht_cap.vht_supported) {
175 agg_size = sta->vht_cap.cap &
176 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
177 agg_size >>=
178 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
179 } else if (sta->ht_cap.ht_supported) {
180 agg_size = sta->ht_cap.ampdu_factor;
181 }
182
183 add_sta_cmd.station_flags |=
184 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
185 add_sta_cmd.station_flags |=
186 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
187
188 status = ADD_STA_SUCCESS;
f9dc0004
EG
189 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(add_sta_cmd),
190 &add_sta_cmd, &status);
8ca151b5
JB
191 if (ret)
192 return ret;
193
194 switch (status) {
195 case ADD_STA_SUCCESS:
196 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
197 break;
198 default:
199 ret = -EIO;
200 IWL_ERR(mvm, "ADD_STA failed\n");
201 break;
202 }
203
204 return ret;
205}
206
207int iwl_mvm_add_sta(struct iwl_mvm *mvm,
208 struct ieee80211_vif *vif,
209 struct ieee80211_sta *sta)
210{
211 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
212 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
213 int i, ret, sta_id;
214
215 lockdep_assert_held(&mvm->mutex);
216
217 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
b92e661b
EP
218 sta_id = iwl_mvm_find_free_sta_id(mvm,
219 ieee80211_vif_type_p2p(vif));
8ca151b5
JB
220 else
221 sta_id = mvm_sta->sta_id;
222
223 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
224 return -ENOSPC;
225
226 spin_lock_init(&mvm_sta->lock);
227
228 mvm_sta->sta_id = sta_id;
229 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
230 mvmvif->color);
231 mvm_sta->vif = vif;
232 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
9ee718aa
EL
233 mvm_sta->tx_protection = 0;
234 mvm_sta->tt_tx_protection = false;
8ca151b5
JB
235
236 /* HW restart, don't assume the memory has been zeroed */
e3d4bc8c 237 atomic_set(&mvm->pending_frames[sta_id], 0);
8ca151b5
JB
238 mvm_sta->tid_disable_agg = 0;
239 mvm_sta->tfd_queue_msk = 0;
240 for (i = 0; i < IEEE80211_NUM_ACS; i++)
241 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
242 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
243
6d9d32b8
JB
244 /* for HW restart - reset everything but the sequence number */
245 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
246 u16 seq = mvm_sta->tid_data[i].seq_number;
247 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
248 mvm_sta->tid_data[i].seq_number = seq;
249 }
8ca151b5 250
7a453973 251 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
8ca151b5
JB
252 if (ret)
253 return ret;
254
255 /* The first station added is the AP, the others are TDLS STAs */
256 if (vif->type == NL80211_IFTYPE_STATION &&
257 mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
258 mvmvif->ap_sta_id = sta_id;
259
260 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
261
262 return 0;
263}
264
7a453973
JB
265int iwl_mvm_update_sta(struct iwl_mvm *mvm,
266 struct ieee80211_vif *vif,
267 struct ieee80211_sta *sta)
268{
269 return iwl_mvm_sta_send_to_fw(mvm, sta, true);
270}
271
8ca151b5
JB
272int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
273 bool drain)
274{
f9dc0004 275 struct iwl_mvm_add_sta_cmd cmd = {};
8ca151b5
JB
276 int ret;
277 u32 status;
278
279 lockdep_assert_held(&mvm->mutex);
280
281 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
282 cmd.sta_id = mvmsta->sta_id;
283 cmd.add_modify = STA_MODE_MODIFY;
284 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
285 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
286
287 status = ADD_STA_SUCCESS;
f9dc0004
EG
288 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
289 &cmd, &status);
8ca151b5
JB
290 if (ret)
291 return ret;
292
293 switch (status) {
294 case ADD_STA_SUCCESS:
295 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
296 mvmsta->sta_id);
297 break;
298 default:
299 ret = -EIO;
300 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
301 mvmsta->sta_id);
302 break;
303 }
304
305 return ret;
306}
307
308/*
309 * Remove a station from the FW table. Before sending the command to remove
310 * the station validate that the station is indeed known to the driver (sanity
311 * only).
312 */
313static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
314{
315 struct ieee80211_sta *sta;
316 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
317 .sta_id = sta_id,
318 };
319 int ret;
320
321 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
322 lockdep_is_held(&mvm->mutex));
323
324 /* Note: internal stations are marked as error values */
325 if (!sta) {
326 IWL_ERR(mvm, "Invalid station id\n");
327 return -EINVAL;
328 }
329
a1022927 330 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
8ca151b5
JB
331 sizeof(rm_sta_cmd), &rm_sta_cmd);
332 if (ret) {
333 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
334 return ret;
335 }
336
337 return 0;
338}
339
340void iwl_mvm_sta_drained_wk(struct work_struct *wk)
341{
342 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
343 u8 sta_id;
344
345 /*
346 * The mutex is needed because of the SYNC cmd, but not only: if the
347 * work would run concurrently with iwl_mvm_rm_sta, it would run before
348 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
349 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
350 * that later.
351 */
352 mutex_lock(&mvm->mutex);
353
354 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
355 int ret;
356 struct ieee80211_sta *sta =
357 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
358 lockdep_is_held(&mvm->mutex));
359
1ddbbb0c
JB
360 /*
361 * This station is in use or RCU-removed; the latter happens in
362 * managed mode, where mac80211 removes the station before we
363 * can remove it from firmware (we can only do that after the
364 * MAC is marked unassociated), and possibly while the deauth
365 * frame to disconnect from the AP is still queued. Then, the
366 * station pointer is -ENOENT when the last skb is reclaimed.
367 */
368 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
8ca151b5
JB
369 continue;
370
371 if (PTR_ERR(sta) == -EINVAL) {
372 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
373 sta_id);
374 continue;
375 }
376
377 if (!sta) {
378 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
379 sta_id);
380 continue;
381 }
382
383 WARN_ON(PTR_ERR(sta) != -EBUSY);
384 /* This station was removed and we waited until it got drained,
385 * we can now proceed and remove it.
386 */
387 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
388 if (ret) {
389 IWL_ERR(mvm,
390 "Couldn't remove sta %d after it was drained\n",
391 sta_id);
392 continue;
393 }
c531c771 394 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
8ca151b5
JB
395 clear_bit(sta_id, mvm->sta_drained);
396 }
397
398 mutex_unlock(&mvm->mutex);
399}
400
401int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
402 struct ieee80211_vif *vif,
403 struct ieee80211_sta *sta)
404{
405 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
406 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
407 int ret;
408
409 lockdep_assert_held(&mvm->mutex);
410
411 if (vif->type == NL80211_IFTYPE_STATION &&
412 mvmvif->ap_sta_id == mvm_sta->sta_id) {
80d85655
EG
413 /* flush its queues here since we are freeing mvm_sta */
414 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true);
415
8ca151b5
JB
416 /* if we are associated - we can't remove the AP STA now */
417 if (vif->bss_conf.assoc)
418 return ret;
419
420 /* unassoc - go ahead - remove the AP STA now */
421 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
37577fe2
EP
422
423 /* clear d0i3_ap_sta_id if no longer relevant */
424 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
425 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
8ca151b5
JB
426 }
427
e3d4bc8c
EG
428 /*
429 * Make sure that the tx response code sees the station as -EBUSY and
430 * calls the drain worker.
431 */
432 spin_lock_bh(&mvm_sta->lock);
8ca151b5
JB
433 /*
434 * There are frames pending on the AC queues for this station.
435 * We need to wait until all the frames are drained...
436 */
e3d4bc8c 437 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
8ca151b5
JB
438 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
439 ERR_PTR(-EBUSY));
e3d4bc8c
EG
440 spin_unlock_bh(&mvm_sta->lock);
441 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
8ca151b5 442 } else {
e3d4bc8c 443 spin_unlock_bh(&mvm_sta->lock);
8ca151b5 444 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
c531c771 445 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
8ca151b5
JB
446 }
447
448 return ret;
449}
450
451int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
452 struct ieee80211_vif *vif,
453 u8 sta_id)
454{
455 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
456
457 lockdep_assert_held(&mvm->mutex);
458
c531c771 459 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
8ca151b5
JB
460 return ret;
461}
462
463int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta,
b92e661b 464 u32 qmask, enum nl80211_iftype iftype)
8ca151b5
JB
465{
466 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
b92e661b 467 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
8ca151b5
JB
468 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
469 return -ENOSPC;
470 }
471
472 sta->tfd_queue_msk = qmask;
473
474 /* put a non-NULL value so iterating over the stations won't stop */
475 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
476 return 0;
477}
478
479void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
480{
c531c771 481 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
8ca151b5
JB
482 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
483 sta->sta_id = IWL_MVM_STATION_COUNT;
484}
485
486static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
487 struct iwl_mvm_int_sta *sta,
488 const u8 *addr,
489 u16 mac_id, u16 color)
490{
f9dc0004 491 struct iwl_mvm_add_sta_cmd cmd;
8ca151b5
JB
492 int ret;
493 u32 status;
494
495 lockdep_assert_held(&mvm->mutex);
496
f9dc0004 497 memset(&cmd, 0, sizeof(cmd));
8ca151b5
JB
498 cmd.sta_id = sta->sta_id;
499 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
500 color));
501
502 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
503
504 if (addr)
505 memcpy(cmd.addr, addr, ETH_ALEN);
506
f9dc0004
EG
507 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
508 &cmd, &status);
8ca151b5
JB
509 if (ret)
510 return ret;
511
512 switch (status) {
513 case ADD_STA_SUCCESS:
514 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
515 return 0;
516 default:
517 ret = -EIO;
518 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
519 status);
520 break;
521 }
522 return ret;
523}
524
525int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
526{
527 int ret;
528
529 lockdep_assert_held(&mvm->mutex);
530
7da91b0e
AM
531 /* Map Aux queue to fifo - needs to happen before adding Aux station */
532 iwl_trans_ac_txq_enable(mvm->trans, mvm->aux_queue,
533 IWL_MVM_TX_FIFO_MCAST);
534
535 /* Allocate aux station and assign to it the aux queue */
536 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
b92e661b 537 NL80211_IFTYPE_UNSPECIFIED);
8ca151b5
JB
538 if (ret)
539 return ret;
540
541 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
542 MAC_INDEX_AUX, 0);
543
544 if (ret)
545 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
546 return ret;
547}
548
549/*
550 * Send the add station command for the vif's broadcast station.
551 * Assumes that the station was already allocated.
552 *
553 * @mvm: the mvm component
554 * @vif: the interface to which the broadcast station is added
555 * @bsta: the broadcast station to add.
556 */
557int iwl_mvm_send_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
558 struct iwl_mvm_int_sta *bsta)
559{
560 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5023d966 561 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
a4243402 562 const u8 *baddr = _baddr;
8ca151b5
JB
563
564 lockdep_assert_held(&mvm->mutex);
565
5023d966
JB
566 if (vif->type == NL80211_IFTYPE_ADHOC)
567 baddr = vif->bss_conf.bssid;
568
8ca151b5
JB
569 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
570 return -ENOSPC;
571
572 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
573 mvmvif->id, mvmvif->color);
574}
575
576/* Send the FW a request to remove the station from it's internal data
577 * structures, but DO NOT remove the entry from the local data structures. */
578int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm,
579 struct iwl_mvm_int_sta *bsta)
580{
581 int ret;
582
583 lockdep_assert_held(&mvm->mutex);
584
585 ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
586 if (ret)
587 IWL_WARN(mvm, "Failed sending remove station\n");
588 return ret;
589}
590
591/* Allocate a new station entry for the broadcast station to the given vif,
592 * and send it to the FW.
593 * Note that each P2P mac should have its own broadcast station.
594 *
595 * @mvm: the mvm component
596 * @vif: the interface to which the broadcast station is added
597 * @bsta: the broadcast station to add. */
598int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
599 struct iwl_mvm_int_sta *bsta)
600{
601 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
602 static const u8 baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
603 u32 qmask;
604 int ret;
605
606 lockdep_assert_held(&mvm->mutex);
607
608 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
b92e661b
EP
609 ret = iwl_mvm_allocate_int_sta(mvm, bsta, qmask,
610 ieee80211_vif_type_p2p(vif));
8ca151b5
JB
611 if (ret)
612 return ret;
613
614 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
615 mvmvif->id, mvmvif->color);
616
617 if (ret)
618 iwl_mvm_dealloc_int_sta(mvm, bsta);
619 return ret;
620}
621
622/*
623 * Send the FW a request to remove the station from it's internal data
624 * structures, and in addition remove it from the local data structure.
625 */
626int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *bsta)
627{
628 int ret;
629
630 lockdep_assert_held(&mvm->mutex);
631
632 ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
633 if (ret)
634 return ret;
635
636 iwl_mvm_dealloc_int_sta(mvm, bsta);
637 return ret;
638}
639
113a0447
EG
640#define IWL_MAX_RX_BA_SESSIONS 16
641
8ca151b5
JB
642int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
643 int tid, u16 ssn, bool start)
644{
645 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
f9dc0004 646 struct iwl_mvm_add_sta_cmd cmd = {};
8ca151b5
JB
647 int ret;
648 u32 status;
649
650 lockdep_assert_held(&mvm->mutex);
651
113a0447
EG
652 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
653 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
654 return -ENOSPC;
655 }
656
8ca151b5
JB
657 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
658 cmd.sta_id = mvm_sta->sta_id;
659 cmd.add_modify = STA_MODE_MODIFY;
93a42667
EG
660 if (start) {
661 cmd.add_immediate_ba_tid = (u8) tid;
662 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
663 } else {
664 cmd.remove_immediate_ba_tid = (u8) tid;
665 }
8ca151b5
JB
666 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
667 STA_MODIFY_REMOVE_BA_TID;
668
669 status = ADD_STA_SUCCESS;
f9dc0004
EG
670 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
671 &cmd, &status);
8ca151b5
JB
672 if (ret)
673 return ret;
674
675 switch (status) {
676 case ADD_STA_SUCCESS:
677 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
678 start ? "start" : "stopp");
679 break;
680 case ADD_STA_IMMEDIATE_BA_FAILURE:
681 IWL_WARN(mvm, "RX BA Session refused by fw\n");
682 ret = -ENOSPC;
683 break;
684 default:
685 ret = -EIO;
686 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
687 start ? "start" : "stopp", status);
688 break;
689 }
690
113a0447
EG
691 if (!ret) {
692 if (start)
693 mvm->rx_ba_sessions++;
694 else if (mvm->rx_ba_sessions > 0)
695 /* check that restart flow didn't zero the counter */
696 mvm->rx_ba_sessions--;
697 }
698
8ca151b5
JB
699 return ret;
700}
701
702static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
703 int tid, u8 queue, bool start)
704{
705 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
f9dc0004 706 struct iwl_mvm_add_sta_cmd cmd = {};
8ca151b5
JB
707 int ret;
708 u32 status;
709
710 lockdep_assert_held(&mvm->mutex);
711
712 if (start) {
713 mvm_sta->tfd_queue_msk |= BIT(queue);
714 mvm_sta->tid_disable_agg &= ~BIT(tid);
715 } else {
716 mvm_sta->tfd_queue_msk &= ~BIT(queue);
717 mvm_sta->tid_disable_agg |= BIT(tid);
718 }
719
720 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
721 cmd.sta_id = mvm_sta->sta_id;
722 cmd.add_modify = STA_MODE_MODIFY;
723 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
724 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
725 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
726
727 status = ADD_STA_SUCCESS;
f9dc0004
EG
728 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
729 &cmd, &status);
8ca151b5
JB
730 if (ret)
731 return ret;
732
733 switch (status) {
734 case ADD_STA_SUCCESS:
735 break;
736 default:
737 ret = -EIO;
738 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
739 start ? "start" : "stopp", status);
740 break;
741 }
742
743 return ret;
744}
745
b797e3fb 746const u8 tid_to_mac80211_ac[] = {
8ca151b5
JB
747 IEEE80211_AC_BE,
748 IEEE80211_AC_BK,
749 IEEE80211_AC_BK,
750 IEEE80211_AC_BE,
751 IEEE80211_AC_VI,
752 IEEE80211_AC_VI,
753 IEEE80211_AC_VO,
754 IEEE80211_AC_VO,
755};
756
3e56eadf
JB
757static const u8 tid_to_ucode_ac[] = {
758 AC_BE,
759 AC_BK,
760 AC_BK,
761 AC_BE,
762 AC_VI,
763 AC_VI,
764 AC_VO,
765 AC_VO,
766};
767
8ca151b5
JB
768int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
769 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
770{
5b577a90 771 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
8ca151b5
JB
772 struct iwl_mvm_tid_data *tid_data;
773 int txq_id;
774
775 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
776 return -EINVAL;
777
778 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
779 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
780 mvmsta->tid_data[tid].state);
781 return -ENXIO;
782 }
783
784 lockdep_assert_held(&mvm->mutex);
785
19e737c9
EL
786 for (txq_id = mvm->first_agg_queue;
787 txq_id <= mvm->last_agg_queue; txq_id++)
8ca151b5
JB
788 if (mvm->queue_to_mac80211[txq_id] ==
789 IWL_INVALID_MAC80211_QUEUE)
790 break;
791
19e737c9 792 if (txq_id > mvm->last_agg_queue) {
8ca151b5
JB
793 IWL_ERR(mvm, "Failed to allocate agg queue\n");
794 return -EIO;
795 }
796
b2492501
AN
797 spin_lock_bh(&mvmsta->lock);
798
799 /* possible race condition - we entered D0i3 while starting agg */
800 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
801 spin_unlock_bh(&mvmsta->lock);
802 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
803 return -EIO;
804 }
805
8ca151b5 806 /* the new tx queue is still connected to the same mac80211 queue */
3e56eadf 807 mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_mac80211_ac[tid]];
8ca151b5 808
8ca151b5 809 tid_data = &mvmsta->tid_data[tid];
9a886586 810 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
8ca151b5
JB
811 tid_data->txq_id = txq_id;
812 *ssn = tid_data->ssn;
813
814 IWL_DEBUG_TX_QUEUES(mvm,
815 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
816 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
817 tid_data->next_reclaimed);
818
819 if (tid_data->ssn == tid_data->next_reclaimed) {
820 tid_data->state = IWL_AGG_STARTING;
821 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
822 } else {
823 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
824 }
825
826 spin_unlock_bh(&mvmsta->lock);
827
828 return 0;
829}
830
831int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
832 struct ieee80211_sta *sta, u16 tid, u8 buf_size)
833{
5b577a90 834 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
8ca151b5
JB
835 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
836 int queue, fifo, ret;
837 u16 ssn;
838
839 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
840
841 spin_lock_bh(&mvmsta->lock);
842 ssn = tid_data->ssn;
843 queue = tid_data->txq_id;
844 tid_data->state = IWL_AGG_ON;
845 tid_data->ssn = 0xffff;
846 spin_unlock_bh(&mvmsta->lock);
847
3e56eadf 848 fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
8ca151b5
JB
849
850 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
851 if (ret)
852 return -EIO;
853
854 iwl_trans_txq_enable(mvm->trans, queue, fifo, mvmsta->sta_id, tid,
855 buf_size, ssn);
856
857 /*
858 * Even though in theory the peer could have different
859 * aggregation reorder buffer sizes for different sessions,
860 * our ucode doesn't allow for that and has a global limit
861 * for each station. Therefore, use the minimum of all the
862 * aggregation sessions and our default value.
863 */
864 mvmsta->max_agg_bufsize =
865 min(mvmsta->max_agg_bufsize, buf_size);
866 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
867
9ee718aa
EL
868 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
869 sta->addr, tid);
870
9e680946 871 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
8ca151b5
JB
872}
873
874int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
875 struct ieee80211_sta *sta, u16 tid)
876{
5b577a90 877 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
8ca151b5
JB
878 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
879 u16 txq_id;
880 int err;
881
f9aa8dd3
EG
882
883 /*
884 * If mac80211 is cleaning its state, then say that we finished since
885 * our state has been cleared anyway.
886 */
887 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
888 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
889 return 0;
890 }
891
8ca151b5
JB
892 spin_lock_bh(&mvmsta->lock);
893
894 txq_id = tid_data->txq_id;
895
896 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
897 mvmsta->sta_id, tid, txq_id, tid_data->state);
898
899 switch (tid_data->state) {
900 case IWL_AGG_ON:
9a886586 901 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
8ca151b5
JB
902
903 IWL_DEBUG_TX_QUEUES(mvm,
904 "ssn = %d, next_recl = %d\n",
905 tid_data->ssn, tid_data->next_reclaimed);
906
907 /* There are still packets for this RA / TID in the HW */
908 if (tid_data->ssn != tid_data->next_reclaimed) {
909 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
910 err = 0;
911 break;
912 }
913
914 tid_data->ssn = 0xffff;
d4578ea8 915 iwl_trans_txq_disable(mvm->trans, txq_id, true);
8ca151b5
JB
916 /* fall through */
917 case IWL_AGG_STARTING:
918 case IWL_EMPTYING_HW_QUEUE_ADDBA:
919 /*
920 * The agg session has been stopped before it was set up. This
921 * can happen when the AddBA timer times out for example.
922 */
923
924 /* No barriers since we are under mutex */
925 lockdep_assert_held(&mvm->mutex);
926 mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
927
928 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
929 tid_data->state = IWL_AGG_OFF;
930 err = 0;
931 break;
932 default:
933 IWL_ERR(mvm,
934 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
935 mvmsta->sta_id, tid, tid_data->state);
936 IWL_ERR(mvm,
937 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
938 err = -EINVAL;
939 }
940
941 spin_unlock_bh(&mvmsta->lock);
942
943 return err;
944}
945
e3d9e7ce
EG
946int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
947 struct ieee80211_sta *sta, u16 tid)
948{
5b577a90 949 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
e3d9e7ce
EG
950 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
951 u16 txq_id;
b6658ff8 952 enum iwl_mvm_agg_state old_state;
e3d9e7ce
EG
953
954 /*
955 * First set the agg state to OFF to avoid calling
956 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
957 */
958 spin_lock_bh(&mvmsta->lock);
959 txq_id = tid_data->txq_id;
960 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
961 mvmsta->sta_id, tid, txq_id, tid_data->state);
b6658ff8 962 old_state = tid_data->state;
e3d9e7ce
EG
963 tid_data->state = IWL_AGG_OFF;
964 spin_unlock_bh(&mvmsta->lock);
965
b6658ff8
JB
966 if (old_state >= IWL_AGG_ON) {
967 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true))
968 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
969
d4578ea8 970 iwl_trans_txq_disable(mvm->trans, tid_data->txq_id, true);
b6658ff8 971 }
e3d9e7ce 972
e3d9e7ce
EG
973 mvm->queue_to_mac80211[tid_data->txq_id] =
974 IWL_INVALID_MAC80211_QUEUE;
975
976 return 0;
977}
978
8ca151b5
JB
979static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
980{
981 int i;
982
983 lockdep_assert_held(&mvm->mutex);
984
985 i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM);
986
987 if (i == STA_KEY_MAX_NUM)
988 return STA_KEY_IDX_INVALID;
989
990 __set_bit(i, mvm->fw_key_table);
991
992 return i;
993}
994
995static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
996 struct ieee80211_sta *sta)
997{
998 struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
999
1000 if (sta) {
1001 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1002
1003 return mvm_sta->sta_id;
1004 }
1005
1006 /*
1007 * The device expects GTKs for station interfaces to be
1008 * installed as GTKs for the AP station. If we have no
1009 * station ID, then use AP's station ID.
1010 */
1011 if (vif->type == NL80211_IFTYPE_STATION &&
1012 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
1013 return mvmvif->ap_sta_id;
1014
881acd89 1015 return IWL_MVM_STATION_COUNT;
8ca151b5
JB
1016}
1017
1018static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
1019 struct iwl_mvm_sta *mvm_sta,
1020 struct ieee80211_key_conf *keyconf,
1021 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1022 u32 cmd_flags)
1023{
5a258aae 1024 struct iwl_mvm_add_sta_key_cmd cmd = {};
f9dc0004 1025 __le16 key_flags;
8ca151b5
JB
1026 int ret, status;
1027 u16 keyidx;
1028 int i;
1029
1030 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1031 STA_KEY_FLG_KEYID_MSK;
1032 key_flags = cpu_to_le16(keyidx);
1033 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
1034
1035 switch (keyconf->cipher) {
1036 case WLAN_CIPHER_SUITE_TKIP:
1037 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
5a258aae 1038 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
8ca151b5 1039 for (i = 0; i < 5; i++)
5a258aae
MS
1040 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1041 memcpy(cmd.key, keyconf->key, keyconf->keylen);
8ca151b5
JB
1042 break;
1043 case WLAN_CIPHER_SUITE_CCMP:
1044 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
5a258aae 1045 memcpy(cmd.key, keyconf->key, keyconf->keylen);
8ca151b5
JB
1046 break;
1047 default:
e36e5433
MS
1048 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
1049 memcpy(cmd.key, keyconf->key, keyconf->keylen);
8ca151b5
JB
1050 }
1051
1052 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1053 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1054
5a258aae
MS
1055 cmd.key_offset = keyconf->hw_key_idx;
1056 cmd.key_flags = key_flags;
8ca151b5
JB
1057 cmd.sta_id = sta_id;
1058
1059 status = ADD_STA_SUCCESS;
a1022927 1060 if (cmd_flags & CMD_ASYNC)
f9dc0004
EG
1061 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
1062 sizeof(cmd), &cmd);
a1022927
EG
1063 else
1064 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1065 &cmd, &status);
8ca151b5
JB
1066
1067 switch (status) {
1068 case ADD_STA_SUCCESS:
1069 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1070 break;
1071 default:
1072 ret = -EIO;
1073 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1074 break;
1075 }
1076
1077 return ret;
1078}
1079
1080static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1081 struct ieee80211_key_conf *keyconf,
1082 u8 sta_id, bool remove_key)
1083{
1084 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1085
1086 /* verify the key details match the required command's expectations */
1087 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1088 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1089 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1090 return -EINVAL;
1091
1092 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1093 igtk_cmd.sta_id = cpu_to_le32(sta_id);
1094
1095 if (remove_key) {
1096 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1097 } else {
1098 struct ieee80211_key_seq seq;
1099 const u8 *pn;
1100
1101 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
1102 ieee80211_aes_cmac_calculate_k1_k2(keyconf,
1103 igtk_cmd.K1, igtk_cmd.K2);
1104 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1105 pn = seq.aes_cmac.pn;
1106 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1107 ((u64) pn[4] << 8) |
1108 ((u64) pn[3] << 16) |
1109 ((u64) pn[2] << 24) |
1110 ((u64) pn[1] << 32) |
1111 ((u64) pn[0] << 40));
1112 }
1113
1114 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1115 remove_key ? "removing" : "installing",
1116 igtk_cmd.sta_id);
1117
a1022927 1118 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
8ca151b5
JB
1119 sizeof(igtk_cmd), &igtk_cmd);
1120}
1121
1122
1123static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1124 struct ieee80211_vif *vif,
1125 struct ieee80211_sta *sta)
1126{
1127 struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
1128
1129 if (sta)
1130 return sta->addr;
1131
1132 if (vif->type == NL80211_IFTYPE_STATION &&
1133 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1134 u8 sta_id = mvmvif->ap_sta_id;
1135 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1136 lockdep_is_held(&mvm->mutex));
1137 return sta->addr;
1138 }
1139
1140
1141 return NULL;
1142}
1143
1144int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1145 struct ieee80211_vif *vif,
1146 struct ieee80211_sta *sta,
1147 struct ieee80211_key_conf *keyconf,
1148 bool have_key_offset)
1149{
1150 struct iwl_mvm_sta *mvm_sta;
1151 int ret;
1152 u8 *addr, sta_id;
1153 struct ieee80211_key_seq seq;
1154 u16 p1k[5];
1155
1156 lockdep_assert_held(&mvm->mutex);
1157
1158 /* Get the station id from the mvm local station table */
1159 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
881acd89 1160 if (sta_id == IWL_MVM_STATION_COUNT) {
8ca151b5
JB
1161 IWL_ERR(mvm, "Failed to find station id\n");
1162 return -EINVAL;
1163 }
1164
1165 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1166 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1167 goto end;
1168 }
1169
1170 /*
1171 * It is possible that the 'sta' parameter is NULL, and thus
1172 * there is a need to retrieve the sta from the local station table.
1173 */
1174 if (!sta) {
1175 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1176 lockdep_is_held(&mvm->mutex));
1177 if (IS_ERR_OR_NULL(sta)) {
1178 IWL_ERR(mvm, "Invalid station id\n");
1179 return -EINVAL;
1180 }
1181 }
1182
1183 mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1184 if (WARN_ON_ONCE(mvm_sta->vif != vif))
1185 return -EINVAL;
1186
1187 if (!have_key_offset) {
1188 /*
1189 * The D3 firmware hardcodes the PTK offset to 0, so we have to
1190 * configure it there. As a result, this workaround exists to
1191 * let the caller set the key offset (hw_key_idx), see d3.c.
1192 */
1193 keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
1194 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1195 return -ENOSPC;
1196 }
1197
1198 switch (keyconf->cipher) {
1199 case WLAN_CIPHER_SUITE_TKIP:
1200 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1201 /* get phase 1 key from mac80211 */
1202 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1203 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1204 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
a1022927 1205 seq.tkip.iv32, p1k, 0);
8ca151b5
JB
1206 break;
1207 case WLAN_CIPHER_SUITE_CCMP:
1208 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
a1022927 1209 0, NULL, 0);
8ca151b5
JB
1210 break;
1211 default:
e36e5433 1212 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf,
a1022927 1213 sta_id, 0, NULL, 0);
8ca151b5
JB
1214 }
1215
1216 if (ret)
1217 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1218
1219end:
1220 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1221 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1222 sta->addr, ret);
1223 return ret;
1224}
1225
1226int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1227 struct ieee80211_vif *vif,
1228 struct ieee80211_sta *sta,
1229 struct ieee80211_key_conf *keyconf)
1230{
1231 struct iwl_mvm_sta *mvm_sta;
5a258aae 1232 struct iwl_mvm_add_sta_key_cmd cmd = {};
8ca151b5
JB
1233 __le16 key_flags;
1234 int ret, status;
1235 u8 sta_id;
1236
1237 lockdep_assert_held(&mvm->mutex);
1238
1239 /* Get the station id from the mvm local station table */
1240 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1241
1242 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1243 keyconf->keyidx, sta_id);
1244
1245 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1246 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1247
1248 ret = __test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1249 if (!ret) {
1250 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1251 keyconf->hw_key_idx);
1252 return -ENOENT;
1253 }
1254
881acd89 1255 if (sta_id == IWL_MVM_STATION_COUNT) {
8ca151b5
JB
1256 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1257 return 0;
1258 }
1259
1260 /*
1261 * It is possible that the 'sta' parameter is NULL, and thus
1262 * there is a need to retrieve the sta from the local station table,
1263 * for example when a GTK is removed (where the sta_id will then be
1264 * the AP ID, and no station was passed by mac80211.)
1265 */
1266 if (!sta) {
1267 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1268 lockdep_is_held(&mvm->mutex));
1269 if (!sta) {
1270 IWL_ERR(mvm, "Invalid station id\n");
1271 return -EINVAL;
1272 }
1273 }
1274
1275 mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1276 if (WARN_ON_ONCE(mvm_sta->vif != vif))
1277 return -EINVAL;
1278
8115efbd
EG
1279 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1280 STA_KEY_FLG_KEYID_MSK);
8ca151b5
JB
1281 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1282 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1283
1284 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1285 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1286
5a258aae
MS
1287 cmd.key_flags = key_flags;
1288 cmd.key_offset = keyconf->hw_key_idx;
8ca151b5
JB
1289 cmd.sta_id = sta_id;
1290
8ca151b5 1291 status = ADD_STA_SUCCESS;
f9dc0004
EG
1292 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1293 &cmd, &status);
8ca151b5
JB
1294
1295 switch (status) {
1296 case ADD_STA_SUCCESS:
1297 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1298 break;
1299 default:
1300 ret = -EIO;
1301 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1302 break;
1303 }
1304
1305 return ret;
1306}
1307
1308void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1309 struct ieee80211_vif *vif,
1310 struct ieee80211_key_conf *keyconf,
1311 struct ieee80211_sta *sta, u32 iv32,
1312 u16 *phase1key)
1313{
c3eb536a 1314 struct iwl_mvm_sta *mvm_sta;
8ca151b5
JB
1315 u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1316
881acd89 1317 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
8ca151b5
JB
1318 return;
1319
c3eb536a
BL
1320 rcu_read_lock();
1321
1322 if (!sta) {
1323 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1324 if (WARN_ON(IS_ERR_OR_NULL(sta))) {
1325 rcu_read_unlock();
1326 return;
1327 }
1328 }
1329
1330 mvm_sta = (void *)sta->drv_priv;
8ca151b5
JB
1331 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1332 iv32, phase1key, CMD_ASYNC);
c3eb536a 1333 rcu_read_unlock();
8ca151b5
JB
1334}
1335
9cc40712
JB
1336void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
1337 struct ieee80211_sta *sta)
8ca151b5 1338{
5b577a90 1339 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
f9dc0004 1340 struct iwl_mvm_add_sta_cmd cmd = {
8ca151b5 1341 .add_modify = STA_MODE_MODIFY,
9cc40712 1342 .sta_id = mvmsta->sta_id,
5af01772 1343 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
9cc40712 1344 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
8ca151b5
JB
1345 };
1346 int ret;
1347
f9dc0004 1348 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
8ca151b5
JB
1349 if (ret)
1350 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1351}
1352
9cc40712
JB
1353void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
1354 struct ieee80211_sta *sta,
8ca151b5 1355 enum ieee80211_frame_release_type reason,
3e56eadf
JB
1356 u16 cnt, u16 tids, bool more_data,
1357 bool agg)
8ca151b5 1358{
5b577a90 1359 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
f9dc0004 1360 struct iwl_mvm_add_sta_cmd cmd = {
8ca151b5 1361 .add_modify = STA_MODE_MODIFY,
9cc40712 1362 .sta_id = mvmsta->sta_id,
8ca151b5
JB
1363 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1364 .sleep_tx_count = cpu_to_le16(cnt),
9cc40712 1365 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
8ca151b5 1366 };
3e56eadf
JB
1367 int tid, ret;
1368 unsigned long _tids = tids;
1369
1370 /* convert TIDs to ACs - we don't support TSPEC so that's OK
1371 * Note that this field is reserved and unused by firmware not
1372 * supporting GO uAPSD, so it's safe to always do this.
1373 */
1374 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
1375 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
1376
1377 /* If we're releasing frames from aggregation queues then check if the
1378 * all queues combined that we're releasing frames from have
1379 * - more frames than the service period, in which case more_data
1380 * needs to be set
1381 * - fewer than 'cnt' frames, in which case we need to adjust the
1382 * firmware command (but do that unconditionally)
1383 */
1384 if (agg) {
1385 int remaining = cnt;
1386
1387 spin_lock_bh(&mvmsta->lock);
1388 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
1389 struct iwl_mvm_tid_data *tid_data;
1390 u16 n_queued;
1391
1392 tid_data = &mvmsta->tid_data[tid];
1393 if (WARN(tid_data->state != IWL_AGG_ON &&
1394 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
1395 "TID %d state is %d\n",
1396 tid, tid_data->state)) {
1397 spin_unlock_bh(&mvmsta->lock);
1398 ieee80211_sta_eosp(sta);
1399 return;
1400 }
1401
1402 n_queued = iwl_mvm_tid_queued(tid_data);
1403 if (n_queued > remaining) {
1404 more_data = true;
1405 remaining = 0;
1406 break;
1407 }
1408 remaining -= n_queued;
1409 }
1410 spin_unlock_bh(&mvmsta->lock);
1411
1412 cmd.sleep_tx_count = cpu_to_le16(cnt - remaining);
1413 if (WARN_ON(cnt - remaining == 0)) {
1414 ieee80211_sta_eosp(sta);
1415 return;
1416 }
1417 }
1418
1419 /* Note: this is ignored by firmware not supporting GO uAPSD */
1420 if (more_data)
1421 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
1422
1423 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
1424 mvmsta->next_status_eosp = true;
1425 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
1426 } else {
1427 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
1428 }
8ca151b5 1429
f9dc0004 1430 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
8ca151b5
JB
1431 if (ret)
1432 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1433}
3e56eadf
JB
1434
1435int iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
1436 struct iwl_rx_cmd_buffer *rxb,
1437 struct iwl_device_cmd *cmd)
1438{
1439 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1440 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
1441 struct ieee80211_sta *sta;
1442 u32 sta_id = le32_to_cpu(notif->sta_id);
1443
1444 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
1445 return 0;
1446
1447 rcu_read_lock();
1448 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1449 if (!IS_ERR_OR_NULL(sta))
1450 ieee80211_sta_eosp(sta);
1451 rcu_read_unlock();
1452
1453 return 0;
1454}
09b0ce1a
AO
1455
1456void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
1457 struct iwl_mvm_sta *mvmsta, bool disable)
1458{
1459 struct iwl_mvm_add_sta_cmd cmd = {
1460 .add_modify = STA_MODE_MODIFY,
1461 .sta_id = mvmsta->sta_id,
1462 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
1463 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
1464 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1465 };
1466 int ret;
1467
1468 if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_DISABLE_STA_TX))
1469 return;
1470
1471 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1472 if (ret)
1473 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1474}
003e5236
AO
1475
1476void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
1477 struct ieee80211_sta *sta,
1478 bool disable)
1479{
1480 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1481
1482 spin_lock_bh(&mvm_sta->lock);
1483
1484 if (mvm_sta->disable_tx == disable) {
1485 spin_unlock_bh(&mvm_sta->lock);
1486 return;
1487 }
1488
1489 mvm_sta->disable_tx = disable;
1490
1491 /*
1492 * Tell mac80211 to start/stop queueing tx for this station,
1493 * but don't stop queueing if there are still pending frames
1494 * for this station.
1495 */
1496 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
1497 ieee80211_sta_block_awake(mvm->hw, sta, disable);
1498
1499 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
1500
1501 spin_unlock_bh(&mvm_sta->lock);
1502}
1503
1504void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
1505 struct iwl_mvm_vif *mvmvif,
1506 bool disable)
1507{
1508 struct ieee80211_sta *sta;
1509 struct iwl_mvm_sta *mvm_sta;
1510 int i;
1511
1512 lockdep_assert_held(&mvm->mutex);
1513
1514 /* Block/unblock all the stations of the given mvmvif */
1515 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
1516 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
1517 lockdep_is_held(&mvm->mutex));
1518 if (IS_ERR_OR_NULL(sta))
1519 continue;
1520
1521 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1522 if (mvm_sta->mac_id_n_color !=
1523 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
1524 continue;
1525
1526 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
1527 }
1528}