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 - 2014 Intel Corporation. All rights reserved.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24 * The full GNU General Public License is included in this distribution
25 * in the file called COPYING.
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
33 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 *****************************************************************************/
63 #include <net/mac80211.h>
69 static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
70 enum nl80211_iftype iftype)
75 BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
76 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
78 lockdep_assert_held(&mvm->mutex);
80 /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
81 if (iftype != NL80211_IFTYPE_STATION)
82 reserved_ids = BIT(0);
84 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
85 for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++) {
86 if (BIT(sta_id) & reserved_ids)
89 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
90 lockdep_is_held(&mvm->mutex)))
93 return IWL_MVM_STATION_COUNT;
96 /* send station add/update command to firmware */
97 int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
100 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
101 struct iwl_mvm_add_sta_cmd add_sta_cmd = {
102 .sta_id = mvm_sta->sta_id,
103 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
104 .add_modify = update ? 1 : 0,
105 .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
106 STA_FLG_MIMO_EN_MSK),
110 u32 agg_size = 0, mpdu_dens = 0;
113 add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
114 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
117 switch (sta->bandwidth) {
118 case IEEE80211_STA_RX_BW_160:
119 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
121 case IEEE80211_STA_RX_BW_80:
122 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
124 case IEEE80211_STA_RX_BW_40:
125 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
127 case IEEE80211_STA_RX_BW_20:
128 if (sta->ht_cap.ht_supported)
129 add_sta_cmd.station_flags |=
130 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
134 switch (sta->rx_nss) {
136 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
139 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
142 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
146 switch (sta->smps_mode) {
147 case IEEE80211_SMPS_AUTOMATIC:
148 case IEEE80211_SMPS_NUM_MODES:
151 case IEEE80211_SMPS_STATIC:
153 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
154 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
156 case IEEE80211_SMPS_DYNAMIC:
157 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
159 case IEEE80211_SMPS_OFF:
164 if (sta->ht_cap.ht_supported) {
165 add_sta_cmd.station_flags_msk |=
166 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
167 STA_FLG_AGG_MPDU_DENS_MSK);
169 mpdu_dens = sta->ht_cap.ampdu_density;
172 if (sta->vht_cap.vht_supported) {
173 agg_size = sta->vht_cap.cap &
174 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
176 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
177 } else if (sta->ht_cap.ht_supported) {
178 agg_size = sta->ht_cap.ampdu_factor;
181 add_sta_cmd.station_flags |=
182 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
183 add_sta_cmd.station_flags |=
184 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
186 status = ADD_STA_SUCCESS;
187 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(add_sta_cmd),
188 &add_sta_cmd, &status);
193 case ADD_STA_SUCCESS:
194 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
198 IWL_ERR(mvm, "ADD_STA failed\n");
205 int iwl_mvm_add_sta(struct iwl_mvm *mvm,
206 struct ieee80211_vif *vif,
207 struct ieee80211_sta *sta)
209 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
210 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
213 lockdep_assert_held(&mvm->mutex);
215 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
216 sta_id = iwl_mvm_find_free_sta_id(mvm,
217 ieee80211_vif_type_p2p(vif));
219 sta_id = mvm_sta->sta_id;
221 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
224 spin_lock_init(&mvm_sta->lock);
226 mvm_sta->sta_id = sta_id;
227 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
230 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
231 mvm_sta->tx_protection = 0;
232 mvm_sta->tt_tx_protection = false;
234 /* HW restart, don't assume the memory has been zeroed */
235 atomic_set(&mvm->pending_frames[sta_id], 0);
236 mvm_sta->tid_disable_agg = 0;
237 mvm_sta->tfd_queue_msk = 0;
238 for (i = 0; i < IEEE80211_NUM_ACS; i++)
239 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
240 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
242 /* for HW restart - reset everything but the sequence number */
243 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
244 u16 seq = mvm_sta->tid_data[i].seq_number;
245 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
246 mvm_sta->tid_data[i].seq_number = seq;
249 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
253 /* The first station added is the AP, the others are TDLS STAs */
254 if (vif->type == NL80211_IFTYPE_STATION &&
255 mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
256 mvmvif->ap_sta_id = sta_id;
258 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
263 int iwl_mvm_update_sta(struct iwl_mvm *mvm,
264 struct ieee80211_vif *vif,
265 struct ieee80211_sta *sta)
267 return iwl_mvm_sta_send_to_fw(mvm, sta, true);
270 int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
273 struct iwl_mvm_add_sta_cmd cmd = {};
277 lockdep_assert_held(&mvm->mutex);
279 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
280 cmd.sta_id = mvmsta->sta_id;
281 cmd.add_modify = STA_MODE_MODIFY;
282 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
283 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
285 status = ADD_STA_SUCCESS;
286 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
292 case ADD_STA_SUCCESS:
293 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
298 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
307 * Remove a station from the FW table. Before sending the command to remove
308 * the station validate that the station is indeed known to the driver (sanity
311 static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
313 struct ieee80211_sta *sta;
314 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
319 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
320 lockdep_is_held(&mvm->mutex));
322 /* Note: internal stations are marked as error values */
324 IWL_ERR(mvm, "Invalid station id\n");
328 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
329 sizeof(rm_sta_cmd), &rm_sta_cmd);
331 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
338 void iwl_mvm_sta_drained_wk(struct work_struct *wk)
340 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
344 * The mutex is needed because of the SYNC cmd, but not only: if the
345 * work would run concurrently with iwl_mvm_rm_sta, it would run before
346 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
347 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
350 mutex_lock(&mvm->mutex);
352 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
354 struct ieee80211_sta *sta =
355 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
356 lockdep_is_held(&mvm->mutex));
359 * This station is in use or RCU-removed; the latter happens in
360 * managed mode, where mac80211 removes the station before we
361 * can remove it from firmware (we can only do that after the
362 * MAC is marked unassociated), and possibly while the deauth
363 * frame to disconnect from the AP is still queued. Then, the
364 * station pointer is -ENOENT when the last skb is reclaimed.
366 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
369 if (PTR_ERR(sta) == -EINVAL) {
370 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
376 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
381 WARN_ON(PTR_ERR(sta) != -EBUSY);
382 /* This station was removed and we waited until it got drained,
383 * we can now proceed and remove it.
385 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
388 "Couldn't remove sta %d after it was drained\n",
392 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
393 clear_bit(sta_id, mvm->sta_drained);
396 mutex_unlock(&mvm->mutex);
399 int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
400 struct ieee80211_vif *vif,
401 struct ieee80211_sta *sta)
403 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
404 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
407 lockdep_assert_held(&mvm->mutex);
409 if (vif->type == NL80211_IFTYPE_STATION &&
410 mvmvif->ap_sta_id == mvm_sta->sta_id) {
411 /* flush its queues here since we are freeing mvm_sta */
412 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true);
414 /* if we are associated - we can't remove the AP STA now */
415 if (vif->bss_conf.assoc)
418 /* unassoc - go ahead - remove the AP STA now */
419 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
421 /* clear d0i3_ap_sta_id if no longer relevant */
422 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
423 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
427 * Make sure that the tx response code sees the station as -EBUSY and
428 * calls the drain worker.
430 spin_lock_bh(&mvm_sta->lock);
432 * There are frames pending on the AC queues for this station.
433 * We need to wait until all the frames are drained...
435 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
436 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
438 spin_unlock_bh(&mvm_sta->lock);
439 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
441 spin_unlock_bh(&mvm_sta->lock);
442 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
443 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
449 int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
450 struct ieee80211_vif *vif,
453 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
455 lockdep_assert_held(&mvm->mutex);
457 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
461 int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta,
462 u32 qmask, enum nl80211_iftype iftype)
464 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
465 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
466 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
470 sta->tfd_queue_msk = qmask;
472 /* put a non-NULL value so iterating over the stations won't stop */
473 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
477 void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
479 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
480 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
481 sta->sta_id = IWL_MVM_STATION_COUNT;
484 static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
485 struct iwl_mvm_int_sta *sta,
487 u16 mac_id, u16 color)
489 struct iwl_mvm_add_sta_cmd cmd;
493 lockdep_assert_held(&mvm->mutex);
495 memset(&cmd, 0, sizeof(cmd));
496 cmd.sta_id = sta->sta_id;
497 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
500 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
503 memcpy(cmd.addr, addr, ETH_ALEN);
505 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
511 case ADD_STA_SUCCESS:
512 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
516 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
523 int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
527 lockdep_assert_held(&mvm->mutex);
529 /* Map Aux queue to fifo - needs to happen before adding Aux station */
530 iwl_trans_ac_txq_enable(mvm->trans, mvm->aux_queue,
531 IWL_MVM_TX_FIFO_MCAST);
533 /* Allocate aux station and assign to it the aux queue */
534 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
535 NL80211_IFTYPE_UNSPECIFIED);
539 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
543 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
548 * Send the add station command for the vif's broadcast station.
549 * Assumes that the station was already allocated.
551 * @mvm: the mvm component
552 * @vif: the interface to which the broadcast station is added
553 * @bsta: the broadcast station to add.
555 int iwl_mvm_send_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
556 struct iwl_mvm_int_sta *bsta)
558 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
559 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
560 const u8 *baddr = _baddr;
562 lockdep_assert_held(&mvm->mutex);
564 if (vif->type == NL80211_IFTYPE_ADHOC)
565 baddr = vif->bss_conf.bssid;
567 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
570 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
571 mvmvif->id, mvmvif->color);
574 /* Send the FW a request to remove the station from it's internal data
575 * structures, but DO NOT remove the entry from the local data structures. */
576 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm,
577 struct iwl_mvm_int_sta *bsta)
581 lockdep_assert_held(&mvm->mutex);
583 ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
585 IWL_WARN(mvm, "Failed sending remove station\n");
589 /* Allocate a new station entry for the broadcast station to the given vif,
590 * and send it to the FW.
591 * Note that each P2P mac should have its own broadcast station.
593 * @mvm: the mvm component
594 * @vif: the interface to which the broadcast station is added
595 * @bsta: the broadcast station to add. */
596 int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
597 struct iwl_mvm_int_sta *bsta)
599 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
600 static const u8 baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
604 lockdep_assert_held(&mvm->mutex);
606 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
607 ret = iwl_mvm_allocate_int_sta(mvm, bsta, qmask,
608 ieee80211_vif_type_p2p(vif));
612 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
613 mvmvif->id, mvmvif->color);
616 iwl_mvm_dealloc_int_sta(mvm, bsta);
621 * Send the FW a request to remove the station from it's internal data
622 * structures, and in addition remove it from the local data structure.
624 int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *bsta)
628 lockdep_assert_held(&mvm->mutex);
630 ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
634 iwl_mvm_dealloc_int_sta(mvm, bsta);
638 #define IWL_MAX_RX_BA_SESSIONS 16
640 int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
641 int tid, u16 ssn, bool start)
643 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
644 struct iwl_mvm_add_sta_cmd cmd = {};
648 lockdep_assert_held(&mvm->mutex);
650 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
651 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
655 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
656 cmd.sta_id = mvm_sta->sta_id;
657 cmd.add_modify = STA_MODE_MODIFY;
659 cmd.add_immediate_ba_tid = (u8) tid;
660 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
662 cmd.remove_immediate_ba_tid = (u8) tid;
664 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
665 STA_MODIFY_REMOVE_BA_TID;
667 status = ADD_STA_SUCCESS;
668 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
674 case ADD_STA_SUCCESS:
675 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
676 start ? "start" : "stopp");
678 case ADD_STA_IMMEDIATE_BA_FAILURE:
679 IWL_WARN(mvm, "RX BA Session refused by fw\n");
684 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
685 start ? "start" : "stopp", status);
691 mvm->rx_ba_sessions++;
692 else if (mvm->rx_ba_sessions > 0)
693 /* check that restart flow didn't zero the counter */
694 mvm->rx_ba_sessions--;
700 static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
701 int tid, u8 queue, bool start)
703 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
704 struct iwl_mvm_add_sta_cmd cmd = {};
708 lockdep_assert_held(&mvm->mutex);
711 mvm_sta->tfd_queue_msk |= BIT(queue);
712 mvm_sta->tid_disable_agg &= ~BIT(tid);
714 mvm_sta->tfd_queue_msk &= ~BIT(queue);
715 mvm_sta->tid_disable_agg |= BIT(tid);
718 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
719 cmd.sta_id = mvm_sta->sta_id;
720 cmd.add_modify = STA_MODE_MODIFY;
721 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
722 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
723 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
725 status = ADD_STA_SUCCESS;
726 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
732 case ADD_STA_SUCCESS:
736 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
737 start ? "start" : "stopp", status);
744 const u8 tid_to_mac80211_ac[] = {
755 static const u8 tid_to_ucode_ac[] = {
766 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
767 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
769 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
770 struct iwl_mvm_tid_data *tid_data;
773 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
776 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
777 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
778 mvmsta->tid_data[tid].state);
782 lockdep_assert_held(&mvm->mutex);
784 for (txq_id = mvm->first_agg_queue;
785 txq_id <= mvm->last_agg_queue; txq_id++)
786 if (mvm->queue_to_mac80211[txq_id] ==
787 IWL_INVALID_MAC80211_QUEUE)
790 if (txq_id > mvm->last_agg_queue) {
791 IWL_ERR(mvm, "Failed to allocate agg queue\n");
795 spin_lock_bh(&mvmsta->lock);
797 /* possible race condition - we entered D0i3 while starting agg */
798 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
799 spin_unlock_bh(&mvmsta->lock);
800 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
804 /* the new tx queue is still connected to the same mac80211 queue */
805 mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_mac80211_ac[tid]];
807 tid_data = &mvmsta->tid_data[tid];
808 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
809 tid_data->txq_id = txq_id;
810 *ssn = tid_data->ssn;
812 IWL_DEBUG_TX_QUEUES(mvm,
813 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
814 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
815 tid_data->next_reclaimed);
817 if (tid_data->ssn == tid_data->next_reclaimed) {
818 tid_data->state = IWL_AGG_STARTING;
819 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
821 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
824 spin_unlock_bh(&mvmsta->lock);
829 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
830 struct ieee80211_sta *sta, u16 tid, u8 buf_size)
832 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
833 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
834 int queue, fifo, ret;
837 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
839 spin_lock_bh(&mvmsta->lock);
841 queue = tid_data->txq_id;
842 tid_data->state = IWL_AGG_ON;
843 tid_data->ssn = 0xffff;
844 spin_unlock_bh(&mvmsta->lock);
846 fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
848 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
852 iwl_trans_txq_enable(mvm->trans, queue, fifo, mvmsta->sta_id, tid,
856 * Even though in theory the peer could have different
857 * aggregation reorder buffer sizes for different sessions,
858 * our ucode doesn't allow for that and has a global limit
859 * for each station. Therefore, use the minimum of all the
860 * aggregation sessions and our default value.
862 mvmsta->max_agg_bufsize =
863 min(mvmsta->max_agg_bufsize, buf_size);
864 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
866 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
869 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
872 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
873 struct ieee80211_sta *sta, u16 tid)
875 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
876 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
882 * If mac80211 is cleaning its state, then say that we finished since
883 * our state has been cleared anyway.
885 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
886 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
890 spin_lock_bh(&mvmsta->lock);
892 txq_id = tid_data->txq_id;
894 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
895 mvmsta->sta_id, tid, txq_id, tid_data->state);
897 switch (tid_data->state) {
899 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
901 IWL_DEBUG_TX_QUEUES(mvm,
902 "ssn = %d, next_recl = %d\n",
903 tid_data->ssn, tid_data->next_reclaimed);
905 /* There are still packets for this RA / TID in the HW */
906 if (tid_data->ssn != tid_data->next_reclaimed) {
907 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
912 tid_data->ssn = 0xffff;
913 iwl_trans_txq_disable(mvm->trans, txq_id);
915 case IWL_AGG_STARTING:
916 case IWL_EMPTYING_HW_QUEUE_ADDBA:
918 * The agg session has been stopped before it was set up. This
919 * can happen when the AddBA timer times out for example.
922 /* No barriers since we are under mutex */
923 lockdep_assert_held(&mvm->mutex);
924 mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
926 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
927 tid_data->state = IWL_AGG_OFF;
932 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
933 mvmsta->sta_id, tid, tid_data->state);
935 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
939 spin_unlock_bh(&mvmsta->lock);
944 int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
945 struct ieee80211_sta *sta, u16 tid)
947 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
948 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
950 enum iwl_mvm_agg_state old_state;
953 * First set the agg state to OFF to avoid calling
954 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
956 spin_lock_bh(&mvmsta->lock);
957 txq_id = tid_data->txq_id;
958 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
959 mvmsta->sta_id, tid, txq_id, tid_data->state);
960 old_state = tid_data->state;
961 tid_data->state = IWL_AGG_OFF;
962 spin_unlock_bh(&mvmsta->lock);
964 if (old_state >= IWL_AGG_ON) {
965 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true))
966 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
968 iwl_trans_txq_disable(mvm->trans, tid_data->txq_id);
971 mvm->queue_to_mac80211[tid_data->txq_id] =
972 IWL_INVALID_MAC80211_QUEUE;
977 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
981 lockdep_assert_held(&mvm->mutex);
983 i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM);
985 if (i == STA_KEY_MAX_NUM)
986 return STA_KEY_IDX_INVALID;
988 __set_bit(i, mvm->fw_key_table);
993 static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
994 struct ieee80211_sta *sta)
996 struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
999 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1001 return mvm_sta->sta_id;
1005 * The device expects GTKs for station interfaces to be
1006 * installed as GTKs for the AP station. If we have no
1007 * station ID, then use AP's station ID.
1009 if (vif->type == NL80211_IFTYPE_STATION &&
1010 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
1011 return mvmvif->ap_sta_id;
1013 return IWL_MVM_STATION_COUNT;
1016 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
1017 struct iwl_mvm_sta *mvm_sta,
1018 struct ieee80211_key_conf *keyconf,
1019 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1022 struct iwl_mvm_add_sta_key_cmd cmd = {};
1028 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1029 STA_KEY_FLG_KEYID_MSK;
1030 key_flags = cpu_to_le16(keyidx);
1031 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
1033 switch (keyconf->cipher) {
1034 case WLAN_CIPHER_SUITE_TKIP:
1035 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
1036 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
1037 for (i = 0; i < 5; i++)
1038 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1039 memcpy(cmd.key, keyconf->key, keyconf->keylen);
1041 case WLAN_CIPHER_SUITE_CCMP:
1042 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
1043 memcpy(cmd.key, keyconf->key, keyconf->keylen);
1046 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
1047 memcpy(cmd.key, keyconf->key, keyconf->keylen);
1050 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1051 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1053 cmd.key_offset = keyconf->hw_key_idx;
1054 cmd.key_flags = key_flags;
1055 cmd.sta_id = sta_id;
1057 status = ADD_STA_SUCCESS;
1058 if (cmd_flags & CMD_ASYNC)
1059 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
1062 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1066 case ADD_STA_SUCCESS:
1067 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1071 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1078 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1079 struct ieee80211_key_conf *keyconf,
1080 u8 sta_id, bool remove_key)
1082 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1084 /* verify the key details match the required command's expectations */
1085 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1086 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1087 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1090 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1091 igtk_cmd.sta_id = cpu_to_le32(sta_id);
1094 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1096 struct ieee80211_key_seq seq;
1099 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
1100 ieee80211_aes_cmac_calculate_k1_k2(keyconf,
1101 igtk_cmd.K1, igtk_cmd.K2);
1102 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1103 pn = seq.aes_cmac.pn;
1104 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1105 ((u64) pn[4] << 8) |
1106 ((u64) pn[3] << 16) |
1107 ((u64) pn[2] << 24) |
1108 ((u64) pn[1] << 32) |
1109 ((u64) pn[0] << 40));
1112 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1113 remove_key ? "removing" : "installing",
1116 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
1117 sizeof(igtk_cmd), &igtk_cmd);
1121 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1122 struct ieee80211_vif *vif,
1123 struct ieee80211_sta *sta)
1125 struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
1130 if (vif->type == NL80211_IFTYPE_STATION &&
1131 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1132 u8 sta_id = mvmvif->ap_sta_id;
1133 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1134 lockdep_is_held(&mvm->mutex));
1142 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1143 struct ieee80211_vif *vif,
1144 struct ieee80211_sta *sta,
1145 struct ieee80211_key_conf *keyconf,
1146 bool have_key_offset)
1148 struct iwl_mvm_sta *mvm_sta;
1151 struct ieee80211_key_seq seq;
1154 lockdep_assert_held(&mvm->mutex);
1156 /* Get the station id from the mvm local station table */
1157 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1158 if (sta_id == IWL_MVM_STATION_COUNT) {
1159 IWL_ERR(mvm, "Failed to find station id\n");
1163 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1164 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1169 * It is possible that the 'sta' parameter is NULL, and thus
1170 * there is a need to retrieve the sta from the local station table.
1173 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1174 lockdep_is_held(&mvm->mutex));
1175 if (IS_ERR_OR_NULL(sta)) {
1176 IWL_ERR(mvm, "Invalid station id\n");
1181 mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1182 if (WARN_ON_ONCE(mvm_sta->vif != vif))
1185 if (!have_key_offset) {
1187 * The D3 firmware hardcodes the PTK offset to 0, so we have to
1188 * configure it there. As a result, this workaround exists to
1189 * let the caller set the key offset (hw_key_idx), see d3.c.
1191 keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
1192 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1196 switch (keyconf->cipher) {
1197 case WLAN_CIPHER_SUITE_TKIP:
1198 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1199 /* get phase 1 key from mac80211 */
1200 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1201 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1202 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1203 seq.tkip.iv32, p1k, 0);
1205 case WLAN_CIPHER_SUITE_CCMP:
1206 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1210 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf,
1211 sta_id, 0, NULL, 0);
1215 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1218 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1219 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1224 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1225 struct ieee80211_vif *vif,
1226 struct ieee80211_sta *sta,
1227 struct ieee80211_key_conf *keyconf)
1229 struct iwl_mvm_sta *mvm_sta;
1230 struct iwl_mvm_add_sta_key_cmd cmd = {};
1235 lockdep_assert_held(&mvm->mutex);
1237 /* Get the station id from the mvm local station table */
1238 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1240 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1241 keyconf->keyidx, sta_id);
1243 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1244 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1246 ret = __test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1248 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1249 keyconf->hw_key_idx);
1253 if (sta_id == IWL_MVM_STATION_COUNT) {
1254 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1259 * It is possible that the 'sta' parameter is NULL, and thus
1260 * there is a need to retrieve the sta from the local station table,
1261 * for example when a GTK is removed (where the sta_id will then be
1262 * the AP ID, and no station was passed by mac80211.)
1265 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1266 lockdep_is_held(&mvm->mutex));
1268 IWL_ERR(mvm, "Invalid station id\n");
1273 mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1274 if (WARN_ON_ONCE(mvm_sta->vif != vif))
1277 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1278 STA_KEY_FLG_KEYID_MSK);
1279 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1280 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1282 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1283 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1285 cmd.key_flags = key_flags;
1286 cmd.key_offset = keyconf->hw_key_idx;
1287 cmd.sta_id = sta_id;
1289 status = ADD_STA_SUCCESS;
1290 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1294 case ADD_STA_SUCCESS:
1295 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1299 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1306 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1307 struct ieee80211_vif *vif,
1308 struct ieee80211_key_conf *keyconf,
1309 struct ieee80211_sta *sta, u32 iv32,
1312 struct iwl_mvm_sta *mvm_sta;
1313 u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1315 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
1321 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1322 if (WARN_ON(IS_ERR_OR_NULL(sta))) {
1328 mvm_sta = (void *)sta->drv_priv;
1329 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1330 iv32, phase1key, CMD_ASYNC);
1334 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
1335 struct ieee80211_sta *sta)
1337 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1338 struct iwl_mvm_add_sta_cmd cmd = {
1339 .add_modify = STA_MODE_MODIFY,
1340 .sta_id = mvmsta->sta_id,
1341 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
1342 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1346 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1348 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1351 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
1352 struct ieee80211_sta *sta,
1353 enum ieee80211_frame_release_type reason,
1354 u16 cnt, u16 tids, bool more_data,
1357 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1358 struct iwl_mvm_add_sta_cmd cmd = {
1359 .add_modify = STA_MODE_MODIFY,
1360 .sta_id = mvmsta->sta_id,
1361 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1362 .sleep_tx_count = cpu_to_le16(cnt),
1363 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1366 unsigned long _tids = tids;
1368 /* convert TIDs to ACs - we don't support TSPEC so that's OK
1369 * Note that this field is reserved and unused by firmware not
1370 * supporting GO uAPSD, so it's safe to always do this.
1372 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
1373 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
1375 /* If we're releasing frames from aggregation queues then check if the
1376 * all queues combined that we're releasing frames from have
1377 * - more frames than the service period, in which case more_data
1379 * - fewer than 'cnt' frames, in which case we need to adjust the
1380 * firmware command (but do that unconditionally)
1383 int remaining = cnt;
1385 spin_lock_bh(&mvmsta->lock);
1386 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
1387 struct iwl_mvm_tid_data *tid_data;
1390 tid_data = &mvmsta->tid_data[tid];
1391 if (WARN(tid_data->state != IWL_AGG_ON &&
1392 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
1393 "TID %d state is %d\n",
1394 tid, tid_data->state)) {
1395 spin_unlock_bh(&mvmsta->lock);
1396 ieee80211_sta_eosp(sta);
1400 n_queued = iwl_mvm_tid_queued(tid_data);
1401 if (n_queued > remaining) {
1406 remaining -= n_queued;
1408 spin_unlock_bh(&mvmsta->lock);
1410 cmd.sleep_tx_count = cpu_to_le16(cnt - remaining);
1411 if (WARN_ON(cnt - remaining == 0)) {
1412 ieee80211_sta_eosp(sta);
1417 /* Note: this is ignored by firmware not supporting GO uAPSD */
1419 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
1421 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
1422 mvmsta->next_status_eosp = true;
1423 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
1425 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
1428 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1430 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1433 int iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
1434 struct iwl_rx_cmd_buffer *rxb,
1435 struct iwl_device_cmd *cmd)
1437 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1438 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
1439 struct ieee80211_sta *sta;
1440 u32 sta_id = le32_to_cpu(notif->sta_id);
1442 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
1446 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1447 if (!IS_ERR_OR_NULL(sta))
1448 ieee80211_sta_eosp(sta);
1454 void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
1455 struct iwl_mvm_sta *mvmsta, bool disable)
1457 struct iwl_mvm_add_sta_cmd cmd = {
1458 .add_modify = STA_MODE_MODIFY,
1459 .sta_id = mvmsta->sta_id,
1460 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
1461 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
1462 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1466 if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_DISABLE_STA_TX))
1469 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1471 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1474 void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
1475 struct ieee80211_sta *sta,
1478 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1480 spin_lock_bh(&mvm_sta->lock);
1482 if (mvm_sta->disable_tx == disable) {
1483 spin_unlock_bh(&mvm_sta->lock);
1487 mvm_sta->disable_tx = disable;
1490 * Tell mac80211 to start/stop queueing tx for this station,
1491 * but don't stop queueing if there are still pending frames
1494 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
1495 ieee80211_sta_block_awake(mvm->hw, sta, disable);
1497 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
1499 spin_unlock_bh(&mvm_sta->lock);
1502 void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
1503 struct iwl_mvm_vif *mvmvif,
1506 struct ieee80211_sta *sta;
1507 struct iwl_mvm_sta *mvm_sta;
1510 lockdep_assert_held(&mvm->mutex);
1512 /* Block/unblock all the stations of the given mvmvif */
1513 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
1514 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
1515 lockdep_is_held(&mvm->mutex));
1516 if (IS_ERR_OR_NULL(sta))
1519 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1520 if (mvm_sta->mac_id_n_color !=
1521 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
1524 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);