1 /******************************************************************************
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.
8 * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
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.
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.
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,
25 * The full GNU General Public License is included in this distribution
26 * in the file called COPYING.
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
34 * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
35 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
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
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.
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.
64 *****************************************************************************/
65 #include <net/mac80211.h>
71 static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
72 enum nl80211_iftype iftype)
77 BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
78 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
80 lockdep_assert_held(&mvm->mutex);
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);
86 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
87 for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++) {
88 if (BIT(sta_id) & reserved_ids)
91 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
92 lockdep_is_held(&mvm->mutex)))
95 return IWL_MVM_STATION_COUNT;
98 /* send station add/update command to firmware */
99 int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
102 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
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),
112 u32 agg_size = 0, mpdu_dens = 0;
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);
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);
123 case IEEE80211_STA_RX_BW_80:
124 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
126 case IEEE80211_STA_RX_BW_40:
127 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
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);
136 switch (sta->rx_nss) {
138 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
141 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
144 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
148 switch (sta->smps_mode) {
149 case IEEE80211_SMPS_AUTOMATIC:
150 case IEEE80211_SMPS_NUM_MODES:
153 case IEEE80211_SMPS_STATIC:
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);
158 case IEEE80211_SMPS_DYNAMIC:
159 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
161 case IEEE80211_SMPS_OFF:
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);
171 mpdu_dens = sta->ht_cap.ampdu_density;
174 if (sta->vht_cap.vht_supported) {
175 agg_size = sta->vht_cap.cap &
176 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
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;
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);
188 status = ADD_STA_SUCCESS;
189 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(add_sta_cmd),
190 &add_sta_cmd, &status);
195 case ADD_STA_SUCCESS:
196 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
200 IWL_ERR(mvm, "ADD_STA failed\n");
207 static int iwl_mvm_tdls_sta_init(struct iwl_mvm *mvm,
208 struct ieee80211_sta *sta)
210 unsigned long used_hw_queues;
211 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
212 unsigned int wdg_timeout =
213 iwl_mvm_get_wd_timeout(mvm, NULL, true, false);
216 lockdep_assert_held(&mvm->mutex);
218 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, NULL);
220 /* Find available queues, and allocate them to the ACs */
221 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
222 u8 queue = find_first_zero_bit(&used_hw_queues,
223 mvm->first_agg_queue);
225 if (queue >= mvm->first_agg_queue) {
226 IWL_ERR(mvm, "Failed to allocate STA queue\n");
230 __set_bit(queue, &used_hw_queues);
231 mvmsta->hw_queue[ac] = queue;
234 /* Found a place for all queues - enable them */
235 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
236 iwl_mvm_enable_ac_txq(mvm, mvmsta->hw_queue[ac],
237 mvmsta->hw_queue[ac],
238 iwl_mvm_ac_to_tx_fifo[ac], 0,
240 mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
246 static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
247 struct ieee80211_sta *sta)
249 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
250 unsigned long sta_msk;
253 lockdep_assert_held(&mvm->mutex);
255 /* disable the TDLS STA-specific queues */
256 sta_msk = mvmsta->tfd_queue_msk;
257 for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
258 iwl_mvm_disable_txq(mvm, i, i, IWL_MAX_TID_COUNT, 0);
261 int iwl_mvm_add_sta(struct iwl_mvm *mvm,
262 struct ieee80211_vif *vif,
263 struct ieee80211_sta *sta)
265 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
266 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
269 lockdep_assert_held(&mvm->mutex);
271 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
272 sta_id = iwl_mvm_find_free_sta_id(mvm,
273 ieee80211_vif_type_p2p(vif));
275 sta_id = mvm_sta->sta_id;
277 if (sta_id == IWL_MVM_STATION_COUNT)
280 if (vif->type == NL80211_IFTYPE_AP) {
281 mvmvif->ap_assoc_sta_count++;
282 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
285 spin_lock_init(&mvm_sta->lock);
287 mvm_sta->sta_id = sta_id;
288 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
291 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
292 mvm_sta->tx_protection = 0;
293 mvm_sta->tt_tx_protection = false;
295 /* HW restart, don't assume the memory has been zeroed */
296 atomic_set(&mvm->pending_frames[sta_id], 0);
297 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
298 mvm_sta->tfd_queue_msk = 0;
300 /* allocate new queues for a TDLS station */
302 ret = iwl_mvm_tdls_sta_init(mvm, sta);
306 for (i = 0; i < IEEE80211_NUM_ACS; i++)
307 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
308 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
311 /* for HW restart - reset everything but the sequence number */
312 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
313 u16 seq = mvm_sta->tid_data[i].seq_number;
314 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
315 mvm_sta->tid_data[i].seq_number = seq;
317 mvm_sta->agg_tids = 0;
319 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
323 if (vif->type == NL80211_IFTYPE_STATION) {
325 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
326 mvmvif->ap_sta_id = sta_id;
328 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
332 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
337 iwl_mvm_tdls_sta_deinit(mvm, sta);
341 int iwl_mvm_update_sta(struct iwl_mvm *mvm,
342 struct ieee80211_vif *vif,
343 struct ieee80211_sta *sta)
345 return iwl_mvm_sta_send_to_fw(mvm, sta, true);
348 int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
351 struct iwl_mvm_add_sta_cmd cmd = {};
355 lockdep_assert_held(&mvm->mutex);
357 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
358 cmd.sta_id = mvmsta->sta_id;
359 cmd.add_modify = STA_MODE_MODIFY;
360 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
361 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
363 status = ADD_STA_SUCCESS;
364 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
370 case ADD_STA_SUCCESS:
371 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
376 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
385 * Remove a station from the FW table. Before sending the command to remove
386 * the station validate that the station is indeed known to the driver (sanity
389 static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
391 struct ieee80211_sta *sta;
392 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
397 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
398 lockdep_is_held(&mvm->mutex));
400 /* Note: internal stations are marked as error values */
402 IWL_ERR(mvm, "Invalid station id\n");
406 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
407 sizeof(rm_sta_cmd), &rm_sta_cmd);
409 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
416 void iwl_mvm_sta_drained_wk(struct work_struct *wk)
418 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
422 * The mutex is needed because of the SYNC cmd, but not only: if the
423 * work would run concurrently with iwl_mvm_rm_sta, it would run before
424 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
425 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
428 mutex_lock(&mvm->mutex);
430 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
432 struct ieee80211_sta *sta =
433 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
434 lockdep_is_held(&mvm->mutex));
437 * This station is in use or RCU-removed; the latter happens in
438 * managed mode, where mac80211 removes the station before we
439 * can remove it from firmware (we can only do that after the
440 * MAC is marked unassociated), and possibly while the deauth
441 * frame to disconnect from the AP is still queued. Then, the
442 * station pointer is -ENOENT when the last skb is reclaimed.
444 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
447 if (PTR_ERR(sta) == -EINVAL) {
448 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
454 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
459 WARN_ON(PTR_ERR(sta) != -EBUSY);
460 /* This station was removed and we waited until it got drained,
461 * we can now proceed and remove it.
463 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
466 "Couldn't remove sta %d after it was drained\n",
470 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
471 clear_bit(sta_id, mvm->sta_drained);
473 if (mvm->tfd_drained[sta_id]) {
474 unsigned long i, msk = mvm->tfd_drained[sta_id];
476 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
477 iwl_mvm_disable_txq(mvm, i, i,
478 IWL_MAX_TID_COUNT, 0);
480 mvm->tfd_drained[sta_id] = 0;
481 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
486 mutex_unlock(&mvm->mutex);
489 int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
490 struct ieee80211_vif *vif,
491 struct ieee80211_sta *sta)
493 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
494 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
497 lockdep_assert_held(&mvm->mutex);
499 if (vif->type == NL80211_IFTYPE_STATION &&
500 mvmvif->ap_sta_id == mvm_sta->sta_id) {
501 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
504 /* flush its queues here since we are freeing mvm_sta */
505 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
508 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
509 mvm_sta->tfd_queue_msk);
512 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
514 /* if we are associated - we can't remove the AP STA now */
515 if (vif->bss_conf.assoc)
518 /* unassoc - go ahead - remove the AP STA now */
519 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
521 /* clear d0i3_ap_sta_id if no longer relevant */
522 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
523 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
527 * This shouldn't happen - the TDLS channel switch should be canceled
528 * before the STA is removed.
530 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
531 mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
532 cancel_delayed_work(&mvm->tdls_cs.dwork);
536 * Make sure that the tx response code sees the station as -EBUSY and
537 * calls the drain worker.
539 spin_lock_bh(&mvm_sta->lock);
541 * There are frames pending on the AC queues for this station.
542 * We need to wait until all the frames are drained...
544 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
545 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
547 spin_unlock_bh(&mvm_sta->lock);
549 /* disable TDLS sta queues on drain complete */
551 mvm->tfd_drained[mvm_sta->sta_id] =
552 mvm_sta->tfd_queue_msk;
553 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
557 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
559 spin_unlock_bh(&mvm_sta->lock);
562 iwl_mvm_tdls_sta_deinit(mvm, sta);
564 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
565 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
571 int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
572 struct ieee80211_vif *vif,
575 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
577 lockdep_assert_held(&mvm->mutex);
579 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
583 static int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
584 struct iwl_mvm_int_sta *sta,
585 u32 qmask, enum nl80211_iftype iftype)
587 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
588 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
589 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
593 sta->tfd_queue_msk = qmask;
595 /* put a non-NULL value so iterating over the stations won't stop */
596 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
600 static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
601 struct iwl_mvm_int_sta *sta)
603 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
604 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
605 sta->sta_id = IWL_MVM_STATION_COUNT;
608 static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
609 struct iwl_mvm_int_sta *sta,
611 u16 mac_id, u16 color)
613 struct iwl_mvm_add_sta_cmd cmd;
617 lockdep_assert_held(&mvm->mutex);
619 memset(&cmd, 0, sizeof(cmd));
620 cmd.sta_id = sta->sta_id;
621 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
624 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
627 memcpy(cmd.addr, addr, ETH_ALEN);
629 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
635 case ADD_STA_SUCCESS:
636 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
640 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
647 int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
649 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
650 mvm->cfg->base_params->wd_timeout :
651 IWL_WATCHDOG_DISABLED;
654 lockdep_assert_held(&mvm->mutex);
656 /* Map Aux queue to fifo - needs to happen before adding Aux station */
657 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
658 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
660 /* Allocate aux station and assign to it the aux queue */
661 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
662 NL80211_IFTYPE_UNSPECIFIED);
666 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
670 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
674 void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
676 lockdep_assert_held(&mvm->mutex);
678 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
682 * Send the add station command for the vif's broadcast station.
683 * Assumes that the station was already allocated.
685 * @mvm: the mvm component
686 * @vif: the interface to which the broadcast station is added
687 * @bsta: the broadcast station to add.
689 int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
691 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
692 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
693 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
694 const u8 *baddr = _baddr;
696 lockdep_assert_held(&mvm->mutex);
698 if (vif->type == NL80211_IFTYPE_ADHOC)
699 baddr = vif->bss_conf.bssid;
701 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
704 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
705 mvmvif->id, mvmvif->color);
708 /* Send the FW a request to remove the station from it's internal data
709 * structures, but DO NOT remove the entry from the local data structures. */
710 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
712 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
715 lockdep_assert_held(&mvm->mutex);
717 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
719 IWL_WARN(mvm, "Failed sending remove station\n");
723 int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
725 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
728 lockdep_assert_held(&mvm->mutex);
730 qmask = iwl_mvm_mac_get_queues_mask(vif);
733 * The firmware defines the TFD queue mask to only be relevant
734 * for *unicast* queues, so the multicast (CAB) queue shouldn't
737 if (vif->type == NL80211_IFTYPE_AP)
738 qmask &= ~BIT(vif->cab_queue);
740 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
741 ieee80211_vif_type_p2p(vif));
744 /* Allocate a new station entry for the broadcast station to the given vif,
745 * and send it to the FW.
746 * Note that each P2P mac should have its own broadcast station.
748 * @mvm: the mvm component
749 * @vif: the interface to which the broadcast station is added
750 * @bsta: the broadcast station to add. */
751 int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
753 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
754 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
757 lockdep_assert_held(&mvm->mutex);
759 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
763 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
766 iwl_mvm_dealloc_int_sta(mvm, bsta);
771 void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
773 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
775 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
779 * Send the FW a request to remove the station from it's internal data
780 * structures, and in addition remove it from the local data structure.
782 int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
786 lockdep_assert_held(&mvm->mutex);
788 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
790 iwl_mvm_dealloc_bcast_sta(mvm, vif);
795 #define IWL_MAX_RX_BA_SESSIONS 16
797 int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
798 int tid, u16 ssn, bool start)
800 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
801 struct iwl_mvm_add_sta_cmd cmd = {};
805 lockdep_assert_held(&mvm->mutex);
807 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
808 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
812 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
813 cmd.sta_id = mvm_sta->sta_id;
814 cmd.add_modify = STA_MODE_MODIFY;
816 cmd.add_immediate_ba_tid = (u8) tid;
817 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
819 cmd.remove_immediate_ba_tid = (u8) tid;
821 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
822 STA_MODIFY_REMOVE_BA_TID;
824 status = ADD_STA_SUCCESS;
825 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
831 case ADD_STA_SUCCESS:
832 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
833 start ? "start" : "stopp");
835 case ADD_STA_IMMEDIATE_BA_FAILURE:
836 IWL_WARN(mvm, "RX BA Session refused by fw\n");
841 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
842 start ? "start" : "stopp", status);
848 mvm->rx_ba_sessions++;
849 else if (mvm->rx_ba_sessions > 0)
850 /* check that restart flow didn't zero the counter */
851 mvm->rx_ba_sessions--;
857 static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
858 int tid, u8 queue, bool start)
860 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
861 struct iwl_mvm_add_sta_cmd cmd = {};
865 lockdep_assert_held(&mvm->mutex);
868 mvm_sta->tfd_queue_msk |= BIT(queue);
869 mvm_sta->tid_disable_agg &= ~BIT(tid);
871 mvm_sta->tfd_queue_msk &= ~BIT(queue);
872 mvm_sta->tid_disable_agg |= BIT(tid);
875 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
876 cmd.sta_id = mvm_sta->sta_id;
877 cmd.add_modify = STA_MODE_MODIFY;
878 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
879 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
880 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
882 status = ADD_STA_SUCCESS;
883 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
889 case ADD_STA_SUCCESS:
893 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
894 start ? "start" : "stopp", status);
901 const u8 tid_to_mac80211_ac[] = {
912 static const u8 tid_to_ucode_ac[] = {
923 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
924 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
926 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
927 struct iwl_mvm_tid_data *tid_data;
931 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
934 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
935 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
936 mvmsta->tid_data[tid].state);
940 lockdep_assert_held(&mvm->mutex);
942 spin_lock_bh(&mvmsta->lock);
944 /* possible race condition - we entered D0i3 while starting agg */
945 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
946 spin_unlock_bh(&mvmsta->lock);
947 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
951 spin_lock_bh(&mvm->queue_info_lock);
953 txq_id = iwl_mvm_find_free_queue(mvm, mvm->first_agg_queue,
954 mvm->last_agg_queue);
957 spin_unlock_bh(&mvm->queue_info_lock);
958 IWL_ERR(mvm, "Failed to allocate agg queue\n");
961 mvm->queue_info[txq_id].setup_reserved = true;
962 spin_unlock_bh(&mvm->queue_info_lock);
964 tid_data = &mvmsta->tid_data[tid];
965 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
966 tid_data->txq_id = txq_id;
967 *ssn = tid_data->ssn;
969 IWL_DEBUG_TX_QUEUES(mvm,
970 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
971 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
972 tid_data->next_reclaimed);
974 if (tid_data->ssn == tid_data->next_reclaimed) {
975 tid_data->state = IWL_AGG_STARTING;
976 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
978 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
984 spin_unlock_bh(&mvmsta->lock);
989 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
990 struct ieee80211_sta *sta, u16 tid, u8 buf_size)
992 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
993 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
994 unsigned int wdg_timeout =
995 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
996 int queue, fifo, ret;
999 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
1000 != IWL_MAX_TID_COUNT);
1002 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
1004 spin_lock_bh(&mvmsta->lock);
1005 ssn = tid_data->ssn;
1006 queue = tid_data->txq_id;
1007 tid_data->state = IWL_AGG_ON;
1008 mvmsta->agg_tids |= BIT(tid);
1009 tid_data->ssn = 0xffff;
1010 spin_unlock_bh(&mvmsta->lock);
1012 fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
1014 iwl_mvm_enable_agg_txq(mvm, queue,
1015 vif->hw_queue[tid_to_mac80211_ac[tid]], fifo,
1016 mvmsta->sta_id, tid, buf_size, ssn, wdg_timeout);
1018 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1022 /* No need to mark as reserved */
1023 spin_lock_bh(&mvm->queue_info_lock);
1024 mvm->queue_info[queue].setup_reserved = false;
1025 spin_unlock_bh(&mvm->queue_info_lock);
1028 * Even though in theory the peer could have different
1029 * aggregation reorder buffer sizes for different sessions,
1030 * our ucode doesn't allow for that and has a global limit
1031 * for each station. Therefore, use the minimum of all the
1032 * aggregation sessions and our default value.
1034 mvmsta->max_agg_bufsize =
1035 min(mvmsta->max_agg_bufsize, buf_size);
1036 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
1038 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
1041 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
1044 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1045 struct ieee80211_sta *sta, u16 tid)
1047 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1048 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1054 * If mac80211 is cleaning its state, then say that we finished since
1055 * our state has been cleared anyway.
1057 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1058 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1062 spin_lock_bh(&mvmsta->lock);
1064 txq_id = tid_data->txq_id;
1066 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
1067 mvmsta->sta_id, tid, txq_id, tid_data->state);
1069 mvmsta->agg_tids &= ~BIT(tid);
1071 /* No need to mark as reserved anymore */
1072 spin_lock_bh(&mvm->queue_info_lock);
1073 mvm->queue_info[txq_id].setup_reserved = false;
1074 spin_unlock_bh(&mvm->queue_info_lock);
1076 switch (tid_data->state) {
1078 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1080 IWL_DEBUG_TX_QUEUES(mvm,
1081 "ssn = %d, next_recl = %d\n",
1082 tid_data->ssn, tid_data->next_reclaimed);
1084 /* There are still packets for this RA / TID in the HW */
1085 if (tid_data->ssn != tid_data->next_reclaimed) {
1086 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
1091 tid_data->ssn = 0xffff;
1092 tid_data->state = IWL_AGG_OFF;
1093 spin_unlock_bh(&mvmsta->lock);
1095 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1097 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1099 iwl_mvm_disable_txq(mvm, txq_id,
1100 vif->hw_queue[tid_to_mac80211_ac[tid]], tid,
1103 case IWL_AGG_STARTING:
1104 case IWL_EMPTYING_HW_QUEUE_ADDBA:
1106 * The agg session has been stopped before it was set up. This
1107 * can happen when the AddBA timer times out for example.
1110 /* No barriers since we are under mutex */
1111 lockdep_assert_held(&mvm->mutex);
1113 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1114 tid_data->state = IWL_AGG_OFF;
1119 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
1120 mvmsta->sta_id, tid, tid_data->state);
1122 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
1126 spin_unlock_bh(&mvmsta->lock);
1131 int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1132 struct ieee80211_sta *sta, u16 tid)
1134 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1135 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1137 enum iwl_mvm_agg_state old_state;
1140 * First set the agg state to OFF to avoid calling
1141 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
1143 spin_lock_bh(&mvmsta->lock);
1144 txq_id = tid_data->txq_id;
1145 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
1146 mvmsta->sta_id, tid, txq_id, tid_data->state);
1147 old_state = tid_data->state;
1148 tid_data->state = IWL_AGG_OFF;
1149 mvmsta->agg_tids &= ~BIT(tid);
1150 spin_unlock_bh(&mvmsta->lock);
1152 /* No need to mark as reserved */
1153 spin_lock_bh(&mvm->queue_info_lock);
1154 mvm->queue_info[txq_id].setup_reserved = false;
1155 spin_unlock_bh(&mvm->queue_info_lock);
1157 if (old_state >= IWL_AGG_ON) {
1158 iwl_mvm_drain_sta(mvm, mvmsta, true);
1159 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
1160 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
1161 iwl_trans_wait_tx_queue_empty(mvm->trans,
1162 mvmsta->tfd_queue_msk);
1163 iwl_mvm_drain_sta(mvm, mvmsta, false);
1165 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1167 iwl_mvm_disable_txq(mvm, tid_data->txq_id,
1168 vif->hw_queue[tid_to_mac80211_ac[tid]], tid,
1175 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
1177 int i, max = -1, max_offs = -1;
1179 lockdep_assert_held(&mvm->mutex);
1181 /* Pick the unused key offset with the highest 'deleted'
1182 * counter. Every time a key is deleted, all the counters
1183 * are incremented and the one that was just deleted is
1184 * reset to zero. Thus, the highest counter is the one
1185 * that was deleted longest ago. Pick that one.
1187 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
1188 if (test_bit(i, mvm->fw_key_table))
1190 if (mvm->fw_key_deleted[i] > max) {
1191 max = mvm->fw_key_deleted[i];
1197 return STA_KEY_IDX_INVALID;
1199 __set_bit(max_offs, mvm->fw_key_table);
1204 static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
1205 struct ieee80211_sta *sta)
1207 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1210 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1212 return mvm_sta->sta_id;
1216 * The device expects GTKs for station interfaces to be
1217 * installed as GTKs for the AP station. If we have no
1218 * station ID, then use AP's station ID.
1220 if (vif->type == NL80211_IFTYPE_STATION &&
1221 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
1222 return mvmvif->ap_sta_id;
1224 return IWL_MVM_STATION_COUNT;
1227 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
1228 struct iwl_mvm_sta *mvm_sta,
1229 struct ieee80211_key_conf *keyconf, bool mcast,
1230 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags)
1232 struct iwl_mvm_add_sta_key_cmd cmd = {};
1238 u8 sta_id = mvm_sta->sta_id;
1240 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1241 STA_KEY_FLG_KEYID_MSK;
1242 key_flags = cpu_to_le16(keyidx);
1243 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
1245 switch (keyconf->cipher) {
1246 case WLAN_CIPHER_SUITE_TKIP:
1247 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
1248 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
1249 for (i = 0; i < 5; i++)
1250 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1251 memcpy(cmd.key, keyconf->key, keyconf->keylen);
1253 case WLAN_CIPHER_SUITE_CCMP:
1254 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
1255 memcpy(cmd.key, keyconf->key, keyconf->keylen);
1257 case WLAN_CIPHER_SUITE_WEP104:
1258 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
1260 case WLAN_CIPHER_SUITE_WEP40:
1261 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
1262 memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
1265 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
1266 memcpy(cmd.key, keyconf->key, keyconf->keylen);
1270 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1272 cmd.key_offset = keyconf->hw_key_idx;
1273 cmd.key_flags = key_flags;
1274 cmd.sta_id = sta_id;
1276 status = ADD_STA_SUCCESS;
1277 if (cmd_flags & CMD_ASYNC)
1278 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
1281 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1285 case ADD_STA_SUCCESS:
1286 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1290 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1297 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1298 struct ieee80211_key_conf *keyconf,
1299 u8 sta_id, bool remove_key)
1301 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1303 /* verify the key details match the required command's expectations */
1304 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1305 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1306 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1309 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1310 igtk_cmd.sta_id = cpu_to_le32(sta_id);
1313 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1315 struct ieee80211_key_seq seq;
1318 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
1319 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1320 pn = seq.aes_cmac.pn;
1321 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1322 ((u64) pn[4] << 8) |
1323 ((u64) pn[3] << 16) |
1324 ((u64) pn[2] << 24) |
1325 ((u64) pn[1] << 32) |
1326 ((u64) pn[0] << 40));
1329 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1330 remove_key ? "removing" : "installing",
1333 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
1334 sizeof(igtk_cmd), &igtk_cmd);
1338 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1339 struct ieee80211_vif *vif,
1340 struct ieee80211_sta *sta)
1342 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1347 if (vif->type == NL80211_IFTYPE_STATION &&
1348 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1349 u8 sta_id = mvmvif->ap_sta_id;
1350 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1351 lockdep_is_held(&mvm->mutex));
1359 static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1360 struct ieee80211_vif *vif,
1361 struct ieee80211_sta *sta,
1362 struct ieee80211_key_conf *keyconf,
1365 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1368 struct ieee80211_key_seq seq;
1371 switch (keyconf->cipher) {
1372 case WLAN_CIPHER_SUITE_TKIP:
1373 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1374 /* get phase 1 key from mac80211 */
1375 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1376 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1377 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
1378 seq.tkip.iv32, p1k, 0);
1380 case WLAN_CIPHER_SUITE_CCMP:
1381 case WLAN_CIPHER_SUITE_WEP40:
1382 case WLAN_CIPHER_SUITE_WEP104:
1383 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
1387 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
1394 static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
1395 struct ieee80211_key_conf *keyconf,
1398 struct iwl_mvm_add_sta_key_cmd cmd = {};
1403 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1404 STA_KEY_FLG_KEYID_MSK);
1405 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1406 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1409 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1411 cmd.key_flags = key_flags;
1412 cmd.key_offset = keyconf->hw_key_idx;
1413 cmd.sta_id = sta_id;
1415 status = ADD_STA_SUCCESS;
1416 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1420 case ADD_STA_SUCCESS:
1421 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1425 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1432 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1433 struct ieee80211_vif *vif,
1434 struct ieee80211_sta *sta,
1435 struct ieee80211_key_conf *keyconf,
1436 bool have_key_offset)
1438 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
1441 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
1443 lockdep_assert_held(&mvm->mutex);
1445 /* Get the station id from the mvm local station table */
1446 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1447 if (sta_id == IWL_MVM_STATION_COUNT) {
1448 IWL_ERR(mvm, "Failed to find station id\n");
1452 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1453 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1458 * It is possible that the 'sta' parameter is NULL, and thus
1459 * there is a need to retrieve the sta from the local station table.
1462 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1463 lockdep_is_held(&mvm->mutex));
1464 if (IS_ERR_OR_NULL(sta)) {
1465 IWL_ERR(mvm, "Invalid station id\n");
1470 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
1473 if (!have_key_offset) {
1475 * The D3 firmware hardcodes the PTK offset to 0, so we have to
1476 * configure it there. As a result, this workaround exists to
1477 * let the caller set the key offset (hw_key_idx), see d3.c.
1479 keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
1480 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1484 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, mcast);
1486 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1491 * For WEP, the same key is used for multicast and unicast. Upload it
1492 * again, using the same key offset, and now pointing the other one
1493 * to the same key slot (offset).
1494 * If this fails, remove the original as well.
1496 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
1497 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
1498 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, !mcast);
1500 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1501 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
1506 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1507 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1508 sta ? sta->addr : zero_addr, ret);
1512 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1513 struct ieee80211_vif *vif,
1514 struct ieee80211_sta *sta,
1515 struct ieee80211_key_conf *keyconf)
1517 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
1521 lockdep_assert_held(&mvm->mutex);
1523 /* Get the station id from the mvm local station table */
1524 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1526 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1527 keyconf->keyidx, sta_id);
1529 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1530 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1532 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
1533 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1534 keyconf->hw_key_idx);
1538 /* track which key was deleted last */
1539 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
1540 if (mvm->fw_key_deleted[i] < U8_MAX)
1541 mvm->fw_key_deleted[i]++;
1543 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
1545 if (sta_id == IWL_MVM_STATION_COUNT) {
1546 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1551 * It is possible that the 'sta' parameter is NULL, and thus
1552 * there is a need to retrieve the sta from the local station table,
1553 * for example when a GTK is removed (where the sta_id will then be
1554 * the AP ID, and no station was passed by mac80211.)
1557 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1558 lockdep_is_held(&mvm->mutex));
1560 IWL_ERR(mvm, "Invalid station id\n");
1565 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
1568 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
1572 /* delete WEP key twice to get rid of (now useless) offset */
1573 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
1574 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
1575 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
1580 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1581 struct ieee80211_vif *vif,
1582 struct ieee80211_key_conf *keyconf,
1583 struct ieee80211_sta *sta, u32 iv32,
1586 struct iwl_mvm_sta *mvm_sta;
1587 u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1588 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
1590 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
1596 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1597 if (WARN_ON(IS_ERR_OR_NULL(sta))) {
1603 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1604 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
1605 iv32, phase1key, CMD_ASYNC);
1609 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
1610 struct ieee80211_sta *sta)
1612 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1613 struct iwl_mvm_add_sta_cmd cmd = {
1614 .add_modify = STA_MODE_MODIFY,
1615 .sta_id = mvmsta->sta_id,
1616 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
1617 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1621 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1623 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1626 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
1627 struct ieee80211_sta *sta,
1628 enum ieee80211_frame_release_type reason,
1629 u16 cnt, u16 tids, bool more_data,
1632 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1633 struct iwl_mvm_add_sta_cmd cmd = {
1634 .add_modify = STA_MODE_MODIFY,
1635 .sta_id = mvmsta->sta_id,
1636 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1637 .sleep_tx_count = cpu_to_le16(cnt),
1638 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1641 unsigned long _tids = tids;
1643 /* convert TIDs to ACs - we don't support TSPEC so that's OK
1644 * Note that this field is reserved and unused by firmware not
1645 * supporting GO uAPSD, so it's safe to always do this.
1647 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
1648 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
1650 /* If we're releasing frames from aggregation queues then check if the
1651 * all queues combined that we're releasing frames from have
1652 * - more frames than the service period, in which case more_data
1654 * - fewer than 'cnt' frames, in which case we need to adjust the
1655 * firmware command (but do that unconditionally)
1658 int remaining = cnt;
1660 spin_lock_bh(&mvmsta->lock);
1661 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
1662 struct iwl_mvm_tid_data *tid_data;
1665 tid_data = &mvmsta->tid_data[tid];
1666 if (WARN(tid_data->state != IWL_AGG_ON &&
1667 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
1668 "TID %d state is %d\n",
1669 tid, tid_data->state)) {
1670 spin_unlock_bh(&mvmsta->lock);
1671 ieee80211_sta_eosp(sta);
1675 n_queued = iwl_mvm_tid_queued(tid_data);
1676 if (n_queued > remaining) {
1681 remaining -= n_queued;
1683 spin_unlock_bh(&mvmsta->lock);
1685 cmd.sleep_tx_count = cpu_to_le16(cnt - remaining);
1686 if (WARN_ON(cnt - remaining == 0)) {
1687 ieee80211_sta_eosp(sta);
1692 /* Note: this is ignored by firmware not supporting GO uAPSD */
1694 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
1696 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
1697 mvmsta->next_status_eosp = true;
1698 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
1700 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
1703 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1705 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1708 void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
1709 struct iwl_rx_cmd_buffer *rxb)
1711 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1712 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
1713 struct ieee80211_sta *sta;
1714 u32 sta_id = le32_to_cpu(notif->sta_id);
1716 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
1720 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1721 if (!IS_ERR_OR_NULL(sta))
1722 ieee80211_sta_eosp(sta);
1726 void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
1727 struct iwl_mvm_sta *mvmsta, bool disable)
1729 struct iwl_mvm_add_sta_cmd cmd = {
1730 .add_modify = STA_MODE_MODIFY,
1731 .sta_id = mvmsta->sta_id,
1732 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
1733 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
1734 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1738 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1740 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1743 void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
1744 struct ieee80211_sta *sta,
1747 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1749 spin_lock_bh(&mvm_sta->lock);
1751 if (mvm_sta->disable_tx == disable) {
1752 spin_unlock_bh(&mvm_sta->lock);
1756 mvm_sta->disable_tx = disable;
1759 * Tell mac80211 to start/stop queuing tx for this station,
1760 * but don't stop queuing if there are still pending frames
1763 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
1764 ieee80211_sta_block_awake(mvm->hw, sta, disable);
1766 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
1768 spin_unlock_bh(&mvm_sta->lock);
1771 void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
1772 struct iwl_mvm_vif *mvmvif,
1775 struct ieee80211_sta *sta;
1776 struct iwl_mvm_sta *mvm_sta;
1779 lockdep_assert_held(&mvm->mutex);
1781 /* Block/unblock all the stations of the given mvmvif */
1782 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
1783 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
1784 lockdep_is_held(&mvm->mutex));
1785 if (IS_ERR_OR_NULL(sta))
1788 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1789 if (mvm_sta->mac_id_n_color !=
1790 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
1793 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
1797 void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1799 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1800 struct iwl_mvm_sta *mvmsta;
1804 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
1806 if (!WARN_ON(!mvmsta))
1807 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);