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
10 * Copyright(c) 2016 Intel Deutschland GmbH
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
26 * The full GNU General Public License is included in this distribution
27 * in the file called COPYING.
29 * Contact Information:
30 * Intel Linux Wireless <linuxwifi@intel.com>
31 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
35 * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
36 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
37 * Copyright(c) 2016 Intel Deutschland GmbH
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
44 * * Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * * Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in
48 * the documentation and/or other materials provided with the
50 * * Neither the name Intel Corporation nor the names of its
51 * contributors may be used to endorse or promote products derived
52 * from this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 *****************************************************************************/
67 #include <net/mac80211.h>
74 * New version of ADD_STA_sta command added new fields at the end of the
75 * structure, so sending the size of the relevant API's structure is enough to
76 * support both API versions.
78 static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
80 return iwl_mvm_has_new_rx_api(mvm) ?
81 sizeof(struct iwl_mvm_add_sta_cmd) :
82 sizeof(struct iwl_mvm_add_sta_cmd_v7);
85 static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
86 enum nl80211_iftype iftype)
91 BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
92 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
94 lockdep_assert_held(&mvm->mutex);
96 /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
97 if (iftype != NL80211_IFTYPE_STATION)
98 reserved_ids = BIT(0);
100 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
101 for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++) {
102 if (BIT(sta_id) & reserved_ids)
105 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
106 lockdep_is_held(&mvm->mutex)))
109 return IWL_MVM_STATION_COUNT;
112 /* send station add/update command to firmware */
113 int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
116 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
117 struct iwl_mvm_add_sta_cmd add_sta_cmd = {
118 .sta_id = mvm_sta->sta_id,
119 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
120 .add_modify = update ? 1 : 0,
121 .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
122 STA_FLG_MIMO_EN_MSK),
123 .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
127 u32 agg_size = 0, mpdu_dens = 0;
130 add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
131 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
134 switch (sta->bandwidth) {
135 case IEEE80211_STA_RX_BW_160:
136 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
138 case IEEE80211_STA_RX_BW_80:
139 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
141 case IEEE80211_STA_RX_BW_40:
142 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
144 case IEEE80211_STA_RX_BW_20:
145 if (sta->ht_cap.ht_supported)
146 add_sta_cmd.station_flags |=
147 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
151 switch (sta->rx_nss) {
153 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
156 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
159 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
163 switch (sta->smps_mode) {
164 case IEEE80211_SMPS_AUTOMATIC:
165 case IEEE80211_SMPS_NUM_MODES:
168 case IEEE80211_SMPS_STATIC:
170 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
171 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
173 case IEEE80211_SMPS_DYNAMIC:
174 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
176 case IEEE80211_SMPS_OFF:
181 if (sta->ht_cap.ht_supported) {
182 add_sta_cmd.station_flags_msk |=
183 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
184 STA_FLG_AGG_MPDU_DENS_MSK);
186 mpdu_dens = sta->ht_cap.ampdu_density;
189 if (sta->vht_cap.vht_supported) {
190 agg_size = sta->vht_cap.cap &
191 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
193 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
194 } else if (sta->ht_cap.ht_supported) {
195 agg_size = sta->ht_cap.ampdu_factor;
198 add_sta_cmd.station_flags |=
199 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
200 add_sta_cmd.station_flags |=
201 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
203 status = ADD_STA_SUCCESS;
204 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
205 iwl_mvm_add_sta_cmd_size(mvm),
206 &add_sta_cmd, &status);
210 switch (status & IWL_ADD_STA_STATUS_MASK) {
211 case ADD_STA_SUCCESS:
212 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
216 IWL_ERR(mvm, "ADD_STA failed\n");
223 static int iwl_mvm_tdls_sta_init(struct iwl_mvm *mvm,
224 struct ieee80211_sta *sta)
226 unsigned long used_hw_queues;
227 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
228 unsigned int wdg_timeout =
229 iwl_mvm_get_wd_timeout(mvm, NULL, true, false);
232 lockdep_assert_held(&mvm->mutex);
234 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, NULL);
236 /* Find available queues, and allocate them to the ACs */
237 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
238 u8 queue = find_first_zero_bit(&used_hw_queues,
239 mvm->first_agg_queue);
241 if (queue >= mvm->first_agg_queue) {
242 IWL_ERR(mvm, "Failed to allocate STA queue\n");
246 __set_bit(queue, &used_hw_queues);
247 mvmsta->hw_queue[ac] = queue;
250 /* Found a place for all queues - enable them */
251 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
252 iwl_mvm_enable_ac_txq(mvm, mvmsta->hw_queue[ac],
253 mvmsta->hw_queue[ac],
254 iwl_mvm_ac_to_tx_fifo[ac], 0,
256 mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
262 static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
263 struct ieee80211_sta *sta)
265 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
266 unsigned long sta_msk;
269 lockdep_assert_held(&mvm->mutex);
271 /* disable the TDLS STA-specific queues */
272 sta_msk = mvmsta->tfd_queue_msk;
273 for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
274 iwl_mvm_disable_txq(mvm, i, i, IWL_MAX_TID_COUNT, 0);
277 int iwl_mvm_add_sta(struct iwl_mvm *mvm,
278 struct ieee80211_vif *vif,
279 struct ieee80211_sta *sta)
281 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
282 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
285 lockdep_assert_held(&mvm->mutex);
287 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
288 sta_id = iwl_mvm_find_free_sta_id(mvm,
289 ieee80211_vif_type_p2p(vif));
291 sta_id = mvm_sta->sta_id;
293 if (sta_id == IWL_MVM_STATION_COUNT)
296 spin_lock_init(&mvm_sta->lock);
298 mvm_sta->sta_id = sta_id;
299 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
302 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
303 mvm_sta->tx_protection = 0;
304 mvm_sta->tt_tx_protection = false;
306 /* HW restart, don't assume the memory has been zeroed */
307 atomic_set(&mvm->pending_frames[sta_id], 0);
308 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
309 mvm_sta->tfd_queue_msk = 0;
311 /* allocate new queues for a TDLS station */
313 ret = iwl_mvm_tdls_sta_init(mvm, sta);
317 for (i = 0; i < IEEE80211_NUM_ACS; i++)
318 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
319 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
322 /* for HW restart - reset everything but the sequence number */
323 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
324 u16 seq = mvm_sta->tid_data[i].seq_number;
325 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
326 mvm_sta->tid_data[i].seq_number = seq;
328 mvm_sta->agg_tids = 0;
330 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
334 if (vif->type == NL80211_IFTYPE_STATION) {
336 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
337 mvmvif->ap_sta_id = sta_id;
339 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
343 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
348 iwl_mvm_tdls_sta_deinit(mvm, sta);
352 int iwl_mvm_update_sta(struct iwl_mvm *mvm,
353 struct ieee80211_vif *vif,
354 struct ieee80211_sta *sta)
356 return iwl_mvm_sta_send_to_fw(mvm, sta, true);
359 int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
362 struct iwl_mvm_add_sta_cmd cmd = {};
366 lockdep_assert_held(&mvm->mutex);
368 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
369 cmd.sta_id = mvmsta->sta_id;
370 cmd.add_modify = STA_MODE_MODIFY;
371 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
372 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
374 status = ADD_STA_SUCCESS;
375 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
376 iwl_mvm_add_sta_cmd_size(mvm),
381 switch (status & IWL_ADD_STA_STATUS_MASK) {
382 case ADD_STA_SUCCESS:
383 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
388 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
397 * Remove a station from the FW table. Before sending the command to remove
398 * the station validate that the station is indeed known to the driver (sanity
401 static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
403 struct ieee80211_sta *sta;
404 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
409 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
410 lockdep_is_held(&mvm->mutex));
412 /* Note: internal stations are marked as error values */
414 IWL_ERR(mvm, "Invalid station id\n");
418 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
419 sizeof(rm_sta_cmd), &rm_sta_cmd);
421 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
428 void iwl_mvm_sta_drained_wk(struct work_struct *wk)
430 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
434 * The mutex is needed because of the SYNC cmd, but not only: if the
435 * work would run concurrently with iwl_mvm_rm_sta, it would run before
436 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
437 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
440 mutex_lock(&mvm->mutex);
442 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
444 struct ieee80211_sta *sta =
445 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
446 lockdep_is_held(&mvm->mutex));
449 * This station is in use or RCU-removed; the latter happens in
450 * managed mode, where mac80211 removes the station before we
451 * can remove it from firmware (we can only do that after the
452 * MAC is marked unassociated), and possibly while the deauth
453 * frame to disconnect from the AP is still queued. Then, the
454 * station pointer is -ENOENT when the last skb is reclaimed.
456 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
459 if (PTR_ERR(sta) == -EINVAL) {
460 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
466 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
471 WARN_ON(PTR_ERR(sta) != -EBUSY);
472 /* This station was removed and we waited until it got drained,
473 * we can now proceed and remove it.
475 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
478 "Couldn't remove sta %d after it was drained\n",
482 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
483 clear_bit(sta_id, mvm->sta_drained);
485 if (mvm->tfd_drained[sta_id]) {
486 unsigned long i, msk = mvm->tfd_drained[sta_id];
488 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
489 iwl_mvm_disable_txq(mvm, i, i,
490 IWL_MAX_TID_COUNT, 0);
492 mvm->tfd_drained[sta_id] = 0;
493 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
498 mutex_unlock(&mvm->mutex);
501 int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
502 struct ieee80211_vif *vif,
503 struct ieee80211_sta *sta)
505 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
506 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
509 lockdep_assert_held(&mvm->mutex);
511 if (vif->type == NL80211_IFTYPE_STATION &&
512 mvmvif->ap_sta_id == mvm_sta->sta_id) {
513 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
516 /* flush its queues here since we are freeing mvm_sta */
517 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
520 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
521 mvm_sta->tfd_queue_msk);
524 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
526 /* if we are associated - we can't remove the AP STA now */
527 if (vif->bss_conf.assoc)
530 /* unassoc - go ahead - remove the AP STA now */
531 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
533 /* clear d0i3_ap_sta_id if no longer relevant */
534 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
535 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
539 * This shouldn't happen - the TDLS channel switch should be canceled
540 * before the STA is removed.
542 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
543 mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
544 cancel_delayed_work(&mvm->tdls_cs.dwork);
548 * Make sure that the tx response code sees the station as -EBUSY and
549 * calls the drain worker.
551 spin_lock_bh(&mvm_sta->lock);
553 * There are frames pending on the AC queues for this station.
554 * We need to wait until all the frames are drained...
556 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
557 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
559 spin_unlock_bh(&mvm_sta->lock);
561 /* disable TDLS sta queues on drain complete */
563 mvm->tfd_drained[mvm_sta->sta_id] =
564 mvm_sta->tfd_queue_msk;
565 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
569 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
571 spin_unlock_bh(&mvm_sta->lock);
574 iwl_mvm_tdls_sta_deinit(mvm, sta);
576 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
577 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
583 int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
584 struct ieee80211_vif *vif,
587 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
589 lockdep_assert_held(&mvm->mutex);
591 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
595 int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
596 struct iwl_mvm_int_sta *sta,
597 u32 qmask, enum nl80211_iftype iftype)
599 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
600 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
601 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
605 sta->tfd_queue_msk = qmask;
607 /* put a non-NULL value so iterating over the stations won't stop */
608 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
612 static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
613 struct iwl_mvm_int_sta *sta)
615 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
616 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
617 sta->sta_id = IWL_MVM_STATION_COUNT;
620 static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
621 struct iwl_mvm_int_sta *sta,
623 u16 mac_id, u16 color)
625 struct iwl_mvm_add_sta_cmd cmd;
629 lockdep_assert_held(&mvm->mutex);
631 memset(&cmd, 0, sizeof(cmd));
632 cmd.sta_id = sta->sta_id;
633 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
636 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
637 cmd.tid_disable_tx = cpu_to_le16(0xffff);
640 memcpy(cmd.addr, addr, ETH_ALEN);
642 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
643 iwl_mvm_add_sta_cmd_size(mvm),
648 switch (status & IWL_ADD_STA_STATUS_MASK) {
649 case ADD_STA_SUCCESS:
650 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
654 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
661 int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
663 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
664 mvm->cfg->base_params->wd_timeout :
665 IWL_WATCHDOG_DISABLED;
668 lockdep_assert_held(&mvm->mutex);
670 /* Map Aux queue to fifo - needs to happen before adding Aux station */
671 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
672 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
674 /* Allocate aux station and assign to it the aux queue */
675 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
676 NL80211_IFTYPE_UNSPECIFIED);
680 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
684 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
688 int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
690 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
692 lockdep_assert_held(&mvm->mutex);
693 return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
697 int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
701 lockdep_assert_held(&mvm->mutex);
703 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
705 IWL_WARN(mvm, "Failed sending remove station\n");
710 void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
712 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
715 void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
717 lockdep_assert_held(&mvm->mutex);
719 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
723 * Send the add station command for the vif's broadcast station.
724 * Assumes that the station was already allocated.
726 * @mvm: the mvm component
727 * @vif: the interface to which the broadcast station is added
728 * @bsta: the broadcast station to add.
730 int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
732 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
733 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
734 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
735 const u8 *baddr = _baddr;
737 lockdep_assert_held(&mvm->mutex);
739 if (vif->type == NL80211_IFTYPE_ADHOC)
740 baddr = vif->bss_conf.bssid;
742 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
745 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
746 mvmvif->id, mvmvif->color);
749 /* Send the FW a request to remove the station from it's internal data
750 * structures, but DO NOT remove the entry from the local data structures. */
751 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
753 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
756 lockdep_assert_held(&mvm->mutex);
758 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
760 IWL_WARN(mvm, "Failed sending remove station\n");
764 int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
766 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
769 lockdep_assert_held(&mvm->mutex);
771 qmask = iwl_mvm_mac_get_queues_mask(vif);
774 * The firmware defines the TFD queue mask to only be relevant
775 * for *unicast* queues, so the multicast (CAB) queue shouldn't
778 if (vif->type == NL80211_IFTYPE_AP)
779 qmask &= ~BIT(vif->cab_queue);
781 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
782 ieee80211_vif_type_p2p(vif));
785 /* Allocate a new station entry for the broadcast station to the given vif,
786 * and send it to the FW.
787 * Note that each P2P mac should have its own broadcast station.
789 * @mvm: the mvm component
790 * @vif: the interface to which the broadcast station is added
791 * @bsta: the broadcast station to add. */
792 int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
794 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
795 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
798 lockdep_assert_held(&mvm->mutex);
800 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
804 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
807 iwl_mvm_dealloc_int_sta(mvm, bsta);
812 void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
814 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
816 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
820 * Send the FW a request to remove the station from it's internal data
821 * structures, and in addition remove it from the local data structure.
823 int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
827 lockdep_assert_held(&mvm->mutex);
829 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
831 iwl_mvm_dealloc_bcast_sta(mvm, vif);
836 #define IWL_MAX_RX_BA_SESSIONS 16
838 int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
839 int tid, u16 ssn, bool start, u8 buf_size)
841 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
842 struct iwl_mvm_add_sta_cmd cmd = {};
846 lockdep_assert_held(&mvm->mutex);
848 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
849 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
853 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
854 cmd.sta_id = mvm_sta->sta_id;
855 cmd.add_modify = STA_MODE_MODIFY;
857 cmd.add_immediate_ba_tid = (u8) tid;
858 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
859 cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
861 cmd.remove_immediate_ba_tid = (u8) tid;
863 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
864 STA_MODIFY_REMOVE_BA_TID;
866 status = ADD_STA_SUCCESS;
867 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
868 iwl_mvm_add_sta_cmd_size(mvm),
873 switch (status & IWL_ADD_STA_STATUS_MASK) {
874 case ADD_STA_SUCCESS:
875 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
876 start ? "start" : "stopp");
878 case ADD_STA_IMMEDIATE_BA_FAILURE:
879 IWL_WARN(mvm, "RX BA Session refused by fw\n");
884 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
885 start ? "start" : "stopp", status);
891 mvm->rx_ba_sessions++;
892 else if (mvm->rx_ba_sessions > 0)
893 /* check that restart flow didn't zero the counter */
894 mvm->rx_ba_sessions--;
900 static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
901 int tid, u8 queue, bool start)
903 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
904 struct iwl_mvm_add_sta_cmd cmd = {};
908 lockdep_assert_held(&mvm->mutex);
911 mvm_sta->tfd_queue_msk |= BIT(queue);
912 mvm_sta->tid_disable_agg &= ~BIT(tid);
914 mvm_sta->tfd_queue_msk &= ~BIT(queue);
915 mvm_sta->tid_disable_agg |= BIT(tid);
918 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
919 cmd.sta_id = mvm_sta->sta_id;
920 cmd.add_modify = STA_MODE_MODIFY;
921 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
922 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
923 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
925 status = ADD_STA_SUCCESS;
926 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
927 iwl_mvm_add_sta_cmd_size(mvm),
932 switch (status & IWL_ADD_STA_STATUS_MASK) {
933 case ADD_STA_SUCCESS:
937 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
938 start ? "start" : "stopp", status);
945 const u8 tid_to_mac80211_ac[] = {
956 static const u8 tid_to_ucode_ac[] = {
967 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
968 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
970 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
971 struct iwl_mvm_tid_data *tid_data;
975 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
978 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
979 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
980 mvmsta->tid_data[tid].state);
984 lockdep_assert_held(&mvm->mutex);
986 spin_lock_bh(&mvmsta->lock);
988 /* possible race condition - we entered D0i3 while starting agg */
989 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
990 spin_unlock_bh(&mvmsta->lock);
991 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
995 spin_lock_bh(&mvm->queue_info_lock);
997 txq_id = iwl_mvm_find_free_queue(mvm, mvm->first_agg_queue,
998 mvm->last_agg_queue);
1001 spin_unlock_bh(&mvm->queue_info_lock);
1002 IWL_ERR(mvm, "Failed to allocate agg queue\n");
1005 mvm->queue_info[txq_id].setup_reserved = true;
1006 spin_unlock_bh(&mvm->queue_info_lock);
1008 tid_data = &mvmsta->tid_data[tid];
1009 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1010 tid_data->txq_id = txq_id;
1011 *ssn = tid_data->ssn;
1013 IWL_DEBUG_TX_QUEUES(mvm,
1014 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
1015 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
1016 tid_data->next_reclaimed);
1018 if (tid_data->ssn == tid_data->next_reclaimed) {
1019 tid_data->state = IWL_AGG_STARTING;
1020 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1022 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
1028 spin_unlock_bh(&mvmsta->lock);
1033 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1034 struct ieee80211_sta *sta, u16 tid, u8 buf_size)
1036 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1037 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1038 unsigned int wdg_timeout =
1039 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
1040 int queue, fifo, ret;
1043 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
1044 != IWL_MAX_TID_COUNT);
1046 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
1048 spin_lock_bh(&mvmsta->lock);
1049 ssn = tid_data->ssn;
1050 queue = tid_data->txq_id;
1051 tid_data->state = IWL_AGG_ON;
1052 mvmsta->agg_tids |= BIT(tid);
1053 tid_data->ssn = 0xffff;
1054 spin_unlock_bh(&mvmsta->lock);
1056 fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
1058 iwl_mvm_enable_agg_txq(mvm, queue,
1059 vif->hw_queue[tid_to_mac80211_ac[tid]], fifo,
1060 mvmsta->sta_id, tid, buf_size, ssn, wdg_timeout);
1062 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1066 /* No need to mark as reserved */
1067 spin_lock_bh(&mvm->queue_info_lock);
1068 mvm->queue_info[queue].setup_reserved = false;
1069 spin_unlock_bh(&mvm->queue_info_lock);
1072 * Even though in theory the peer could have different
1073 * aggregation reorder buffer sizes for different sessions,
1074 * our ucode doesn't allow for that and has a global limit
1075 * for each station. Therefore, use the minimum of all the
1076 * aggregation sessions and our default value.
1078 mvmsta->max_agg_bufsize =
1079 min(mvmsta->max_agg_bufsize, buf_size);
1080 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
1082 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
1085 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
1088 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1089 struct ieee80211_sta *sta, u16 tid)
1091 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1092 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1098 * If mac80211 is cleaning its state, then say that we finished since
1099 * our state has been cleared anyway.
1101 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1102 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1106 spin_lock_bh(&mvmsta->lock);
1108 txq_id = tid_data->txq_id;
1110 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
1111 mvmsta->sta_id, tid, txq_id, tid_data->state);
1113 mvmsta->agg_tids &= ~BIT(tid);
1115 /* No need to mark as reserved anymore */
1116 spin_lock_bh(&mvm->queue_info_lock);
1117 mvm->queue_info[txq_id].setup_reserved = false;
1118 spin_unlock_bh(&mvm->queue_info_lock);
1120 switch (tid_data->state) {
1122 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1124 IWL_DEBUG_TX_QUEUES(mvm,
1125 "ssn = %d, next_recl = %d\n",
1126 tid_data->ssn, tid_data->next_reclaimed);
1128 /* There are still packets for this RA / TID in the HW */
1129 if (tid_data->ssn != tid_data->next_reclaimed) {
1130 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
1135 tid_data->ssn = 0xffff;
1136 tid_data->state = IWL_AGG_OFF;
1137 spin_unlock_bh(&mvmsta->lock);
1139 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1141 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1143 iwl_mvm_disable_txq(mvm, txq_id,
1144 vif->hw_queue[tid_to_mac80211_ac[tid]], tid,
1147 case IWL_AGG_STARTING:
1148 case IWL_EMPTYING_HW_QUEUE_ADDBA:
1150 * The agg session has been stopped before it was set up. This
1151 * can happen when the AddBA timer times out for example.
1154 /* No barriers since we are under mutex */
1155 lockdep_assert_held(&mvm->mutex);
1157 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1158 tid_data->state = IWL_AGG_OFF;
1163 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
1164 mvmsta->sta_id, tid, tid_data->state);
1166 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
1170 spin_unlock_bh(&mvmsta->lock);
1175 int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1176 struct ieee80211_sta *sta, u16 tid)
1178 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1179 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1181 enum iwl_mvm_agg_state old_state;
1184 * First set the agg state to OFF to avoid calling
1185 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
1187 spin_lock_bh(&mvmsta->lock);
1188 txq_id = tid_data->txq_id;
1189 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
1190 mvmsta->sta_id, tid, txq_id, tid_data->state);
1191 old_state = tid_data->state;
1192 tid_data->state = IWL_AGG_OFF;
1193 mvmsta->agg_tids &= ~BIT(tid);
1194 spin_unlock_bh(&mvmsta->lock);
1196 /* No need to mark as reserved */
1197 spin_lock_bh(&mvm->queue_info_lock);
1198 mvm->queue_info[txq_id].setup_reserved = false;
1199 spin_unlock_bh(&mvm->queue_info_lock);
1201 if (old_state >= IWL_AGG_ON) {
1202 iwl_mvm_drain_sta(mvm, mvmsta, true);
1203 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
1204 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
1205 iwl_trans_wait_tx_queue_empty(mvm->trans,
1206 mvmsta->tfd_queue_msk);
1207 iwl_mvm_drain_sta(mvm, mvmsta, false);
1209 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1211 iwl_mvm_disable_txq(mvm, tid_data->txq_id,
1212 vif->hw_queue[tid_to_mac80211_ac[tid]], tid,
1219 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
1221 int i, max = -1, max_offs = -1;
1223 lockdep_assert_held(&mvm->mutex);
1225 /* Pick the unused key offset with the highest 'deleted'
1226 * counter. Every time a key is deleted, all the counters
1227 * are incremented and the one that was just deleted is
1228 * reset to zero. Thus, the highest counter is the one
1229 * that was deleted longest ago. Pick that one.
1231 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
1232 if (test_bit(i, mvm->fw_key_table))
1234 if (mvm->fw_key_deleted[i] > max) {
1235 max = mvm->fw_key_deleted[i];
1241 return STA_KEY_IDX_INVALID;
1246 static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
1247 struct ieee80211_vif *vif,
1248 struct ieee80211_sta *sta)
1250 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1253 return iwl_mvm_sta_from_mac80211(sta);
1256 * The device expects GTKs for station interfaces to be
1257 * installed as GTKs for the AP station. If we have no
1258 * station ID, then use AP's station ID.
1260 if (vif->type == NL80211_IFTYPE_STATION &&
1261 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1262 u8 sta_id = mvmvif->ap_sta_id;
1264 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
1265 lockdep_is_held(&mvm->mutex));
1267 * It is possible that the 'sta' parameter is NULL,
1268 * for example when a GTK is removed - the sta_id will then
1269 * be the AP ID, and no station was passed by mac80211.
1271 if (IS_ERR_OR_NULL(sta))
1274 return iwl_mvm_sta_from_mac80211(sta);
1280 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
1281 struct iwl_mvm_sta *mvm_sta,
1282 struct ieee80211_key_conf *keyconf, bool mcast,
1283 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
1286 struct iwl_mvm_add_sta_key_cmd cmd = {};
1292 u8 sta_id = mvm_sta->sta_id;
1294 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1295 STA_KEY_FLG_KEYID_MSK;
1296 key_flags = cpu_to_le16(keyidx);
1297 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
1299 switch (keyconf->cipher) {
1300 case WLAN_CIPHER_SUITE_TKIP:
1301 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
1302 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
1303 for (i = 0; i < 5; i++)
1304 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1305 memcpy(cmd.key, keyconf->key, keyconf->keylen);
1307 case WLAN_CIPHER_SUITE_CCMP:
1308 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
1309 memcpy(cmd.key, keyconf->key, keyconf->keylen);
1311 case WLAN_CIPHER_SUITE_WEP104:
1312 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
1314 case WLAN_CIPHER_SUITE_WEP40:
1315 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
1316 memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
1319 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
1320 memcpy(cmd.key, keyconf->key, keyconf->keylen);
1324 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1326 cmd.key_offset = key_offset;
1327 cmd.key_flags = key_flags;
1328 cmd.sta_id = sta_id;
1330 status = ADD_STA_SUCCESS;
1331 if (cmd_flags & CMD_ASYNC)
1332 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
1335 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1339 case ADD_STA_SUCCESS:
1340 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1344 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1351 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1352 struct ieee80211_key_conf *keyconf,
1353 u8 sta_id, bool remove_key)
1355 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1357 /* verify the key details match the required command's expectations */
1358 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1359 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1360 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1363 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1364 igtk_cmd.sta_id = cpu_to_le32(sta_id);
1367 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1369 struct ieee80211_key_seq seq;
1372 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
1373 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1374 pn = seq.aes_cmac.pn;
1375 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1376 ((u64) pn[4] << 8) |
1377 ((u64) pn[3] << 16) |
1378 ((u64) pn[2] << 24) |
1379 ((u64) pn[1] << 32) |
1380 ((u64) pn[0] << 40));
1383 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1384 remove_key ? "removing" : "installing",
1387 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
1388 sizeof(igtk_cmd), &igtk_cmd);
1392 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1393 struct ieee80211_vif *vif,
1394 struct ieee80211_sta *sta)
1396 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1401 if (vif->type == NL80211_IFTYPE_STATION &&
1402 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1403 u8 sta_id = mvmvif->ap_sta_id;
1404 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1405 lockdep_is_held(&mvm->mutex));
1413 static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1414 struct ieee80211_vif *vif,
1415 struct ieee80211_sta *sta,
1416 struct ieee80211_key_conf *keyconf,
1420 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1423 struct ieee80211_key_seq seq;
1426 switch (keyconf->cipher) {
1427 case WLAN_CIPHER_SUITE_TKIP:
1428 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1429 /* get phase 1 key from mac80211 */
1430 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1431 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1432 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
1433 seq.tkip.iv32, p1k, 0, key_offset);
1435 case WLAN_CIPHER_SUITE_CCMP:
1436 case WLAN_CIPHER_SUITE_WEP40:
1437 case WLAN_CIPHER_SUITE_WEP104:
1438 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
1439 0, NULL, 0, key_offset);
1442 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
1443 0, NULL, 0, key_offset);
1449 static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
1450 struct ieee80211_key_conf *keyconf,
1453 struct iwl_mvm_add_sta_key_cmd cmd = {};
1458 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1459 STA_KEY_FLG_KEYID_MSK);
1460 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1461 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1464 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1466 cmd.key_flags = key_flags;
1467 cmd.key_offset = keyconf->hw_key_idx;
1468 cmd.sta_id = sta_id;
1470 status = ADD_STA_SUCCESS;
1471 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1475 case ADD_STA_SUCCESS:
1476 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1480 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1487 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1488 struct ieee80211_vif *vif,
1489 struct ieee80211_sta *sta,
1490 struct ieee80211_key_conf *keyconf,
1493 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
1494 struct iwl_mvm_sta *mvm_sta;
1497 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
1499 lockdep_assert_held(&mvm->mutex);
1501 /* Get the station id from the mvm local station table */
1502 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
1504 IWL_ERR(mvm, "Failed to find station\n");
1507 sta_id = mvm_sta->sta_id;
1509 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1510 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1515 * It is possible that the 'sta' parameter is NULL, and thus
1516 * there is a need to retrieve the sta from the local station table.
1519 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1520 lockdep_is_held(&mvm->mutex));
1521 if (IS_ERR_OR_NULL(sta)) {
1522 IWL_ERR(mvm, "Invalid station id\n");
1527 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
1530 /* If the key_offset is not pre-assigned, we need to find a
1531 * new offset to use. In normal cases, the offset is not
1532 * pre-assigned, but during HW_RESTART we want to reuse the
1533 * same indices, so we pass them when this function is called.
1535 * In D3 entry, we need to hardcoded the indices (because the
1536 * firmware hardcodes the PTK offset to 0). In this case, we
1537 * need to make sure we don't overwrite the hw_key_idx in the
1538 * keyconf structure, because otherwise we cannot configure
1539 * the original ones back when resuming.
1541 if (key_offset == STA_KEY_IDX_INVALID) {
1542 key_offset = iwl_mvm_set_fw_key_idx(mvm);
1543 if (key_offset == STA_KEY_IDX_INVALID)
1545 keyconf->hw_key_idx = key_offset;
1548 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
1553 * For WEP, the same key is used for multicast and unicast. Upload it
1554 * again, using the same key offset, and now pointing the other one
1555 * to the same key slot (offset).
1556 * If this fails, remove the original as well.
1558 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
1559 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
1560 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
1561 key_offset, !mcast);
1563 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
1568 __set_bit(key_offset, mvm->fw_key_table);
1571 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1572 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1573 sta ? sta->addr : zero_addr, ret);
1577 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1578 struct ieee80211_vif *vif,
1579 struct ieee80211_sta *sta,
1580 struct ieee80211_key_conf *keyconf)
1582 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
1583 struct iwl_mvm_sta *mvm_sta;
1584 u8 sta_id = IWL_MVM_STATION_COUNT;
1587 lockdep_assert_held(&mvm->mutex);
1589 /* Get the station from the mvm local station table */
1590 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
1592 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1593 keyconf->keyidx, sta_id);
1595 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1596 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1598 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
1599 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1600 keyconf->hw_key_idx);
1604 /* track which key was deleted last */
1605 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
1606 if (mvm->fw_key_deleted[i] < U8_MAX)
1607 mvm->fw_key_deleted[i]++;
1609 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
1612 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1616 sta_id = mvm_sta->sta_id;
1618 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
1622 /* delete WEP key twice to get rid of (now useless) offset */
1623 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
1624 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
1625 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
1630 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1631 struct ieee80211_vif *vif,
1632 struct ieee80211_key_conf *keyconf,
1633 struct ieee80211_sta *sta, u32 iv32,
1636 struct iwl_mvm_sta *mvm_sta;
1637 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
1641 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
1642 if (WARN_ON_ONCE(!mvm_sta))
1644 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
1645 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
1651 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
1652 struct ieee80211_sta *sta)
1654 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1655 struct iwl_mvm_add_sta_cmd cmd = {
1656 .add_modify = STA_MODE_MODIFY,
1657 .sta_id = mvmsta->sta_id,
1658 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
1659 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1663 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
1664 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
1666 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1669 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
1670 struct ieee80211_sta *sta,
1671 enum ieee80211_frame_release_type reason,
1672 u16 cnt, u16 tids, bool more_data,
1675 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1676 struct iwl_mvm_add_sta_cmd cmd = {
1677 .add_modify = STA_MODE_MODIFY,
1678 .sta_id = mvmsta->sta_id,
1679 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1680 .sleep_tx_count = cpu_to_le16(cnt),
1681 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1684 unsigned long _tids = tids;
1686 /* convert TIDs to ACs - we don't support TSPEC so that's OK
1687 * Note that this field is reserved and unused by firmware not
1688 * supporting GO uAPSD, so it's safe to always do this.
1690 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
1691 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
1693 /* If we're releasing frames from aggregation queues then check if the
1694 * all queues combined that we're releasing frames from have
1695 * - more frames than the service period, in which case more_data
1697 * - fewer than 'cnt' frames, in which case we need to adjust the
1698 * firmware command (but do that unconditionally)
1701 int remaining = cnt;
1704 spin_lock_bh(&mvmsta->lock);
1705 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
1706 struct iwl_mvm_tid_data *tid_data;
1709 tid_data = &mvmsta->tid_data[tid];
1710 if (WARN(tid_data->state != IWL_AGG_ON &&
1711 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
1712 "TID %d state is %d\n",
1713 tid, tid_data->state)) {
1714 spin_unlock_bh(&mvmsta->lock);
1715 ieee80211_sta_eosp(sta);
1719 n_queued = iwl_mvm_tid_queued(tid_data);
1720 if (n_queued > remaining) {
1725 remaining -= n_queued;
1727 sleep_tx_count = cnt - remaining;
1728 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1729 mvmsta->sleep_tx_count = sleep_tx_count;
1730 spin_unlock_bh(&mvmsta->lock);
1732 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
1733 if (WARN_ON(cnt - remaining == 0)) {
1734 ieee80211_sta_eosp(sta);
1739 /* Note: this is ignored by firmware not supporting GO uAPSD */
1741 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
1743 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
1744 mvmsta->next_status_eosp = true;
1745 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
1747 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
1750 /* block the Tx queues until the FW updated the sleep Tx count */
1751 iwl_trans_block_txq_ptrs(mvm->trans, true);
1753 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
1754 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
1755 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
1757 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1760 void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
1761 struct iwl_rx_cmd_buffer *rxb)
1763 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1764 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
1765 struct ieee80211_sta *sta;
1766 u32 sta_id = le32_to_cpu(notif->sta_id);
1768 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
1772 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1773 if (!IS_ERR_OR_NULL(sta))
1774 ieee80211_sta_eosp(sta);
1778 void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
1779 struct iwl_mvm_sta *mvmsta, bool disable)
1781 struct iwl_mvm_add_sta_cmd cmd = {
1782 .add_modify = STA_MODE_MODIFY,
1783 .sta_id = mvmsta->sta_id,
1784 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
1785 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
1786 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1790 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
1791 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
1793 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1796 void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
1797 struct ieee80211_sta *sta,
1800 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1802 spin_lock_bh(&mvm_sta->lock);
1804 if (mvm_sta->disable_tx == disable) {
1805 spin_unlock_bh(&mvm_sta->lock);
1809 mvm_sta->disable_tx = disable;
1812 * Tell mac80211 to start/stop queuing tx for this station,
1813 * but don't stop queuing if there are still pending frames
1816 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
1817 ieee80211_sta_block_awake(mvm->hw, sta, disable);
1819 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
1821 spin_unlock_bh(&mvm_sta->lock);
1824 void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
1825 struct iwl_mvm_vif *mvmvif,
1828 struct ieee80211_sta *sta;
1829 struct iwl_mvm_sta *mvm_sta;
1832 lockdep_assert_held(&mvm->mutex);
1834 /* Block/unblock all the stations of the given mvmvif */
1835 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
1836 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
1837 lockdep_is_held(&mvm->mutex));
1838 if (IS_ERR_OR_NULL(sta))
1841 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1842 if (mvm_sta->mac_id_n_color !=
1843 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
1846 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
1850 void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1852 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1853 struct iwl_mvm_sta *mvmsta;
1857 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
1859 if (!WARN_ON(!mvmsta))
1860 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);