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.
9 * Copyright(c) 2013 - 2014 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 - 2014 Intel Corporation. All rights reserved.
35 * Copyright(c) 2013 - 2014 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 = (void *)sta->drv_priv;
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 int iwl_mvm_add_sta(struct iwl_mvm *mvm,
208 struct ieee80211_vif *vif,
209 struct ieee80211_sta *sta)
211 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
212 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
215 lockdep_assert_held(&mvm->mutex);
217 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
218 sta_id = iwl_mvm_find_free_sta_id(mvm,
219 ieee80211_vif_type_p2p(vif));
221 sta_id = mvm_sta->sta_id;
223 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
226 spin_lock_init(&mvm_sta->lock);
228 mvm_sta->sta_id = sta_id;
229 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
232 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
233 mvm_sta->tx_protection = 0;
234 mvm_sta->tt_tx_protection = false;
236 /* HW restart, don't assume the memory has been zeroed */
237 atomic_set(&mvm->pending_frames[sta_id], 0);
238 mvm_sta->tid_disable_agg = 0;
239 mvm_sta->tfd_queue_msk = 0;
240 for (i = 0; i < IEEE80211_NUM_ACS; i++)
241 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
242 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
244 /* for HW restart - reset everything but the sequence number */
245 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
246 u16 seq = mvm_sta->tid_data[i].seq_number;
247 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
248 mvm_sta->tid_data[i].seq_number = seq;
251 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
255 if (vif->type == NL80211_IFTYPE_STATION) {
257 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
258 mvmvif->ap_sta_id = sta_id;
260 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
264 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
269 int iwl_mvm_update_sta(struct iwl_mvm *mvm,
270 struct ieee80211_vif *vif,
271 struct ieee80211_sta *sta)
273 return iwl_mvm_sta_send_to_fw(mvm, sta, true);
276 int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
279 struct iwl_mvm_add_sta_cmd cmd = {};
283 lockdep_assert_held(&mvm->mutex);
285 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
286 cmd.sta_id = mvmsta->sta_id;
287 cmd.add_modify = STA_MODE_MODIFY;
288 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
289 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
291 status = ADD_STA_SUCCESS;
292 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
298 case ADD_STA_SUCCESS:
299 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
304 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
313 * Remove a station from the FW table. Before sending the command to remove
314 * the station validate that the station is indeed known to the driver (sanity
317 static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
319 struct ieee80211_sta *sta;
320 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
325 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
326 lockdep_is_held(&mvm->mutex));
328 /* Note: internal stations are marked as error values */
330 IWL_ERR(mvm, "Invalid station id\n");
334 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
335 sizeof(rm_sta_cmd), &rm_sta_cmd);
337 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
344 void iwl_mvm_sta_drained_wk(struct work_struct *wk)
346 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
350 * The mutex is needed because of the SYNC cmd, but not only: if the
351 * work would run concurrently with iwl_mvm_rm_sta, it would run before
352 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
353 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
356 mutex_lock(&mvm->mutex);
358 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
360 struct ieee80211_sta *sta =
361 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
362 lockdep_is_held(&mvm->mutex));
365 * This station is in use or RCU-removed; the latter happens in
366 * managed mode, where mac80211 removes the station before we
367 * can remove it from firmware (we can only do that after the
368 * MAC is marked unassociated), and possibly while the deauth
369 * frame to disconnect from the AP is still queued. Then, the
370 * station pointer is -ENOENT when the last skb is reclaimed.
372 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
375 if (PTR_ERR(sta) == -EINVAL) {
376 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
382 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
387 WARN_ON(PTR_ERR(sta) != -EBUSY);
388 /* This station was removed and we waited until it got drained,
389 * we can now proceed and remove it.
391 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
394 "Couldn't remove sta %d after it was drained\n",
398 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
399 clear_bit(sta_id, mvm->sta_drained);
402 mutex_unlock(&mvm->mutex);
405 int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
406 struct ieee80211_vif *vif,
407 struct ieee80211_sta *sta)
409 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
410 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
413 lockdep_assert_held(&mvm->mutex);
415 if (vif->type == NL80211_IFTYPE_STATION &&
416 mvmvif->ap_sta_id == mvm_sta->sta_id) {
417 /* flush its queues here since we are freeing mvm_sta */
418 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true);
420 /* if we are associated - we can't remove the AP STA now */
421 if (vif->bss_conf.assoc)
424 /* unassoc - go ahead - remove the AP STA now */
425 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
427 /* clear d0i3_ap_sta_id if no longer relevant */
428 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
429 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
433 * Make sure that the tx response code sees the station as -EBUSY and
434 * calls the drain worker.
436 spin_lock_bh(&mvm_sta->lock);
438 * There are frames pending on the AC queues for this station.
439 * We need to wait until all the frames are drained...
441 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
442 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
444 spin_unlock_bh(&mvm_sta->lock);
445 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
447 spin_unlock_bh(&mvm_sta->lock);
448 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
449 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
455 int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
456 struct ieee80211_vif *vif,
459 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
461 lockdep_assert_held(&mvm->mutex);
463 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
467 static int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
468 struct iwl_mvm_int_sta *sta,
469 u32 qmask, enum nl80211_iftype iftype)
471 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
472 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
473 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
477 sta->tfd_queue_msk = qmask;
479 /* put a non-NULL value so iterating over the stations won't stop */
480 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
484 static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
485 struct iwl_mvm_int_sta *sta)
487 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
488 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
489 sta->sta_id = IWL_MVM_STATION_COUNT;
492 static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
493 struct iwl_mvm_int_sta *sta,
495 u16 mac_id, u16 color)
497 struct iwl_mvm_add_sta_cmd cmd;
501 lockdep_assert_held(&mvm->mutex);
503 memset(&cmd, 0, sizeof(cmd));
504 cmd.sta_id = sta->sta_id;
505 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
508 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
511 memcpy(cmd.addr, addr, ETH_ALEN);
513 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
519 case ADD_STA_SUCCESS:
520 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
524 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
531 int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
535 lockdep_assert_held(&mvm->mutex);
537 /* Map Aux queue to fifo - needs to happen before adding Aux station */
538 iwl_trans_ac_txq_enable(mvm->trans, mvm->aux_queue,
539 IWL_MVM_TX_FIFO_MCAST);
541 /* Allocate aux station and assign to it the aux queue */
542 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
543 NL80211_IFTYPE_UNSPECIFIED);
547 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
551 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
555 void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
557 lockdep_assert_held(&mvm->mutex);
559 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
563 * Send the add station command for the vif's broadcast station.
564 * Assumes that the station was already allocated.
566 * @mvm: the mvm component
567 * @vif: the interface to which the broadcast station is added
568 * @bsta: the broadcast station to add.
570 int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
572 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
573 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
574 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
575 const u8 *baddr = _baddr;
577 lockdep_assert_held(&mvm->mutex);
579 if (vif->type == NL80211_IFTYPE_ADHOC)
580 baddr = vif->bss_conf.bssid;
582 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
585 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
586 mvmvif->id, mvmvif->color);
589 /* Send the FW a request to remove the station from it's internal data
590 * structures, but DO NOT remove the entry from the local data structures. */
591 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
593 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
596 lockdep_assert_held(&mvm->mutex);
598 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
600 IWL_WARN(mvm, "Failed sending remove station\n");
604 int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
606 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
609 lockdep_assert_held(&mvm->mutex);
611 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
614 * The firmware defines the TFD queue mask to only be relevant
615 * for *unicast* queues, so the multicast (CAB) queue shouldn't
618 if (vif->type == NL80211_IFTYPE_AP)
619 qmask &= ~BIT(vif->cab_queue);
621 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
622 ieee80211_vif_type_p2p(vif));
625 /* Allocate a new station entry for the broadcast station to the given vif,
626 * and send it to the FW.
627 * Note that each P2P mac should have its own broadcast station.
629 * @mvm: the mvm component
630 * @vif: the interface to which the broadcast station is added
631 * @bsta: the broadcast station to add. */
632 int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
634 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
635 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
638 lockdep_assert_held(&mvm->mutex);
640 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
644 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
647 iwl_mvm_dealloc_int_sta(mvm, bsta);
652 void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
654 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
656 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
660 * Send the FW a request to remove the station from it's internal data
661 * structures, and in addition remove it from the local data structure.
663 int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
667 lockdep_assert_held(&mvm->mutex);
669 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
671 iwl_mvm_dealloc_bcast_sta(mvm, vif);
676 #define IWL_MAX_RX_BA_SESSIONS 16
678 int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
679 int tid, u16 ssn, bool start)
681 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
682 struct iwl_mvm_add_sta_cmd cmd = {};
686 lockdep_assert_held(&mvm->mutex);
688 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
689 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
693 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
694 cmd.sta_id = mvm_sta->sta_id;
695 cmd.add_modify = STA_MODE_MODIFY;
697 cmd.add_immediate_ba_tid = (u8) tid;
698 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
700 cmd.remove_immediate_ba_tid = (u8) tid;
702 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
703 STA_MODIFY_REMOVE_BA_TID;
705 status = ADD_STA_SUCCESS;
706 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
712 case ADD_STA_SUCCESS:
713 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
714 start ? "start" : "stopp");
716 case ADD_STA_IMMEDIATE_BA_FAILURE:
717 IWL_WARN(mvm, "RX BA Session refused by fw\n");
722 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
723 start ? "start" : "stopp", status);
729 mvm->rx_ba_sessions++;
730 else if (mvm->rx_ba_sessions > 0)
731 /* check that restart flow didn't zero the counter */
732 mvm->rx_ba_sessions--;
738 static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
739 int tid, u8 queue, bool start)
741 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
742 struct iwl_mvm_add_sta_cmd cmd = {};
746 lockdep_assert_held(&mvm->mutex);
749 mvm_sta->tfd_queue_msk |= BIT(queue);
750 mvm_sta->tid_disable_agg &= ~BIT(tid);
752 mvm_sta->tfd_queue_msk &= ~BIT(queue);
753 mvm_sta->tid_disable_agg |= BIT(tid);
756 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
757 cmd.sta_id = mvm_sta->sta_id;
758 cmd.add_modify = STA_MODE_MODIFY;
759 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
760 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
761 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
763 status = ADD_STA_SUCCESS;
764 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
770 case ADD_STA_SUCCESS:
774 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
775 start ? "start" : "stopp", status);
782 const u8 tid_to_mac80211_ac[] = {
793 static const u8 tid_to_ucode_ac[] = {
804 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
805 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
807 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
808 struct iwl_mvm_tid_data *tid_data;
811 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
814 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
815 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
816 mvmsta->tid_data[tid].state);
820 lockdep_assert_held(&mvm->mutex);
822 for (txq_id = mvm->first_agg_queue;
823 txq_id <= mvm->last_agg_queue; txq_id++)
824 if (mvm->queue_to_mac80211[txq_id] ==
825 IWL_INVALID_MAC80211_QUEUE)
828 if (txq_id > mvm->last_agg_queue) {
829 IWL_ERR(mvm, "Failed to allocate agg queue\n");
833 spin_lock_bh(&mvmsta->lock);
835 /* possible race condition - we entered D0i3 while starting agg */
836 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
837 spin_unlock_bh(&mvmsta->lock);
838 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
842 /* the new tx queue is still connected to the same mac80211 queue */
843 mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_mac80211_ac[tid]];
845 tid_data = &mvmsta->tid_data[tid];
846 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
847 tid_data->txq_id = txq_id;
848 *ssn = tid_data->ssn;
850 IWL_DEBUG_TX_QUEUES(mvm,
851 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
852 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
853 tid_data->next_reclaimed);
855 if (tid_data->ssn == tid_data->next_reclaimed) {
856 tid_data->state = IWL_AGG_STARTING;
857 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
859 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
862 spin_unlock_bh(&mvmsta->lock);
867 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
868 struct ieee80211_sta *sta, u16 tid, u8 buf_size)
870 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
871 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
872 int queue, fifo, ret;
875 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
877 spin_lock_bh(&mvmsta->lock);
879 queue = tid_data->txq_id;
880 tid_data->state = IWL_AGG_ON;
881 tid_data->ssn = 0xffff;
882 spin_unlock_bh(&mvmsta->lock);
884 fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
886 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
890 iwl_trans_txq_enable(mvm->trans, queue, fifo, mvmsta->sta_id, tid,
894 * Even though in theory the peer could have different
895 * aggregation reorder buffer sizes for different sessions,
896 * our ucode doesn't allow for that and has a global limit
897 * for each station. Therefore, use the minimum of all the
898 * aggregation sessions and our default value.
900 mvmsta->max_agg_bufsize =
901 min(mvmsta->max_agg_bufsize, buf_size);
902 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
904 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
907 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
910 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
911 struct ieee80211_sta *sta, u16 tid)
913 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
914 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
920 * If mac80211 is cleaning its state, then say that we finished since
921 * our state has been cleared anyway.
923 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
924 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
928 spin_lock_bh(&mvmsta->lock);
930 txq_id = tid_data->txq_id;
932 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
933 mvmsta->sta_id, tid, txq_id, tid_data->state);
935 switch (tid_data->state) {
937 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
939 IWL_DEBUG_TX_QUEUES(mvm,
940 "ssn = %d, next_recl = %d\n",
941 tid_data->ssn, tid_data->next_reclaimed);
943 /* There are still packets for this RA / TID in the HW */
944 if (tid_data->ssn != tid_data->next_reclaimed) {
945 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
950 tid_data->ssn = 0xffff;
951 iwl_trans_txq_disable(mvm->trans, txq_id, true);
953 case IWL_AGG_STARTING:
954 case IWL_EMPTYING_HW_QUEUE_ADDBA:
956 * The agg session has been stopped before it was set up. This
957 * can happen when the AddBA timer times out for example.
960 /* No barriers since we are under mutex */
961 lockdep_assert_held(&mvm->mutex);
962 mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
964 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
965 tid_data->state = IWL_AGG_OFF;
970 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
971 mvmsta->sta_id, tid, tid_data->state);
973 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
977 spin_unlock_bh(&mvmsta->lock);
982 int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
983 struct ieee80211_sta *sta, u16 tid)
985 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
986 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
988 enum iwl_mvm_agg_state old_state;
991 * First set the agg state to OFF to avoid calling
992 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
994 spin_lock_bh(&mvmsta->lock);
995 txq_id = tid_data->txq_id;
996 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
997 mvmsta->sta_id, tid, txq_id, tid_data->state);
998 old_state = tid_data->state;
999 tid_data->state = IWL_AGG_OFF;
1000 spin_unlock_bh(&mvmsta->lock);
1002 if (old_state >= IWL_AGG_ON) {
1003 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true))
1004 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
1006 iwl_trans_txq_disable(mvm->trans, tid_data->txq_id, true);
1009 mvm->queue_to_mac80211[tid_data->txq_id] =
1010 IWL_INVALID_MAC80211_QUEUE;
1015 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
1019 lockdep_assert_held(&mvm->mutex);
1021 i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM);
1023 if (i == STA_KEY_MAX_NUM)
1024 return STA_KEY_IDX_INVALID;
1026 __set_bit(i, mvm->fw_key_table);
1031 static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
1032 struct ieee80211_sta *sta)
1034 struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
1037 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1039 return mvm_sta->sta_id;
1043 * The device expects GTKs for station interfaces to be
1044 * installed as GTKs for the AP station. If we have no
1045 * station ID, then use AP's station ID.
1047 if (vif->type == NL80211_IFTYPE_STATION &&
1048 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
1049 return mvmvif->ap_sta_id;
1051 return IWL_MVM_STATION_COUNT;
1054 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
1055 struct iwl_mvm_sta *mvm_sta,
1056 struct ieee80211_key_conf *keyconf,
1057 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1060 struct iwl_mvm_add_sta_key_cmd cmd = {};
1066 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1067 STA_KEY_FLG_KEYID_MSK;
1068 key_flags = cpu_to_le16(keyidx);
1069 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
1071 switch (keyconf->cipher) {
1072 case WLAN_CIPHER_SUITE_TKIP:
1073 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
1074 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
1075 for (i = 0; i < 5; i++)
1076 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1077 memcpy(cmd.key, keyconf->key, keyconf->keylen);
1079 case WLAN_CIPHER_SUITE_CCMP:
1080 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
1081 memcpy(cmd.key, keyconf->key, keyconf->keylen);
1084 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
1085 memcpy(cmd.key, keyconf->key, keyconf->keylen);
1088 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1089 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1091 cmd.key_offset = keyconf->hw_key_idx;
1092 cmd.key_flags = key_flags;
1093 cmd.sta_id = sta_id;
1095 status = ADD_STA_SUCCESS;
1096 if (cmd_flags & CMD_ASYNC)
1097 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
1100 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1104 case ADD_STA_SUCCESS:
1105 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1109 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1116 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1117 struct ieee80211_key_conf *keyconf,
1118 u8 sta_id, bool remove_key)
1120 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1122 /* verify the key details match the required command's expectations */
1123 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1124 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1125 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1128 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1129 igtk_cmd.sta_id = cpu_to_le32(sta_id);
1132 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1134 struct ieee80211_key_seq seq;
1137 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
1138 ieee80211_aes_cmac_calculate_k1_k2(keyconf,
1139 igtk_cmd.K1, igtk_cmd.K2);
1140 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1141 pn = seq.aes_cmac.pn;
1142 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1143 ((u64) pn[4] << 8) |
1144 ((u64) pn[3] << 16) |
1145 ((u64) pn[2] << 24) |
1146 ((u64) pn[1] << 32) |
1147 ((u64) pn[0] << 40));
1150 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1151 remove_key ? "removing" : "installing",
1154 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
1155 sizeof(igtk_cmd), &igtk_cmd);
1159 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1160 struct ieee80211_vif *vif,
1161 struct ieee80211_sta *sta)
1163 struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
1168 if (vif->type == NL80211_IFTYPE_STATION &&
1169 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1170 u8 sta_id = mvmvif->ap_sta_id;
1171 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1172 lockdep_is_held(&mvm->mutex));
1180 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1181 struct ieee80211_vif *vif,
1182 struct ieee80211_sta *sta,
1183 struct ieee80211_key_conf *keyconf,
1184 bool have_key_offset)
1186 struct iwl_mvm_sta *mvm_sta;
1189 struct ieee80211_key_seq seq;
1192 lockdep_assert_held(&mvm->mutex);
1194 /* Get the station id from the mvm local station table */
1195 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1196 if (sta_id == IWL_MVM_STATION_COUNT) {
1197 IWL_ERR(mvm, "Failed to find station id\n");
1201 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1202 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1207 * It is possible that the 'sta' parameter is NULL, and thus
1208 * there is a need to retrieve the sta from the local station table.
1211 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1212 lockdep_is_held(&mvm->mutex));
1213 if (IS_ERR_OR_NULL(sta)) {
1214 IWL_ERR(mvm, "Invalid station id\n");
1219 mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1220 if (WARN_ON_ONCE(mvm_sta->vif != vif))
1223 if (!have_key_offset) {
1225 * The D3 firmware hardcodes the PTK offset to 0, so we have to
1226 * configure it there. As a result, this workaround exists to
1227 * let the caller set the key offset (hw_key_idx), see d3.c.
1229 keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
1230 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1234 switch (keyconf->cipher) {
1235 case WLAN_CIPHER_SUITE_TKIP:
1236 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1237 /* get phase 1 key from mac80211 */
1238 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1239 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1240 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1241 seq.tkip.iv32, p1k, 0);
1243 case WLAN_CIPHER_SUITE_CCMP:
1244 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1248 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf,
1249 sta_id, 0, NULL, 0);
1253 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1256 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1257 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1262 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1263 struct ieee80211_vif *vif,
1264 struct ieee80211_sta *sta,
1265 struct ieee80211_key_conf *keyconf)
1267 struct iwl_mvm_sta *mvm_sta;
1268 struct iwl_mvm_add_sta_key_cmd cmd = {};
1273 lockdep_assert_held(&mvm->mutex);
1275 /* Get the station id from the mvm local station table */
1276 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1278 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1279 keyconf->keyidx, sta_id);
1281 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1282 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1284 ret = __test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1286 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1287 keyconf->hw_key_idx);
1291 if (sta_id == IWL_MVM_STATION_COUNT) {
1292 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1297 * It is possible that the 'sta' parameter is NULL, and thus
1298 * there is a need to retrieve the sta from the local station table,
1299 * for example when a GTK is removed (where the sta_id will then be
1300 * the AP ID, and no station was passed by mac80211.)
1303 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1304 lockdep_is_held(&mvm->mutex));
1306 IWL_ERR(mvm, "Invalid station id\n");
1311 mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1312 if (WARN_ON_ONCE(mvm_sta->vif != vif))
1315 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1316 STA_KEY_FLG_KEYID_MSK);
1317 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1318 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1320 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1321 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1323 cmd.key_flags = key_flags;
1324 cmd.key_offset = keyconf->hw_key_idx;
1325 cmd.sta_id = sta_id;
1327 status = ADD_STA_SUCCESS;
1328 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1332 case ADD_STA_SUCCESS:
1333 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1337 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1344 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1345 struct ieee80211_vif *vif,
1346 struct ieee80211_key_conf *keyconf,
1347 struct ieee80211_sta *sta, u32 iv32,
1350 struct iwl_mvm_sta *mvm_sta;
1351 u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1353 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
1359 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1360 if (WARN_ON(IS_ERR_OR_NULL(sta))) {
1366 mvm_sta = (void *)sta->drv_priv;
1367 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1368 iv32, phase1key, CMD_ASYNC);
1372 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
1373 struct ieee80211_sta *sta)
1375 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1376 struct iwl_mvm_add_sta_cmd cmd = {
1377 .add_modify = STA_MODE_MODIFY,
1378 .sta_id = mvmsta->sta_id,
1379 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
1380 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1384 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1386 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1389 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
1390 struct ieee80211_sta *sta,
1391 enum ieee80211_frame_release_type reason,
1392 u16 cnt, u16 tids, bool more_data,
1395 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1396 struct iwl_mvm_add_sta_cmd cmd = {
1397 .add_modify = STA_MODE_MODIFY,
1398 .sta_id = mvmsta->sta_id,
1399 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1400 .sleep_tx_count = cpu_to_le16(cnt),
1401 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1404 unsigned long _tids = tids;
1406 /* convert TIDs to ACs - we don't support TSPEC so that's OK
1407 * Note that this field is reserved and unused by firmware not
1408 * supporting GO uAPSD, so it's safe to always do this.
1410 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
1411 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
1413 /* If we're releasing frames from aggregation queues then check if the
1414 * all queues combined that we're releasing frames from have
1415 * - more frames than the service period, in which case more_data
1417 * - fewer than 'cnt' frames, in which case we need to adjust the
1418 * firmware command (but do that unconditionally)
1421 int remaining = cnt;
1423 spin_lock_bh(&mvmsta->lock);
1424 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
1425 struct iwl_mvm_tid_data *tid_data;
1428 tid_data = &mvmsta->tid_data[tid];
1429 if (WARN(tid_data->state != IWL_AGG_ON &&
1430 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
1431 "TID %d state is %d\n",
1432 tid, tid_data->state)) {
1433 spin_unlock_bh(&mvmsta->lock);
1434 ieee80211_sta_eosp(sta);
1438 n_queued = iwl_mvm_tid_queued(tid_data);
1439 if (n_queued > remaining) {
1444 remaining -= n_queued;
1446 spin_unlock_bh(&mvmsta->lock);
1448 cmd.sleep_tx_count = cpu_to_le16(cnt - remaining);
1449 if (WARN_ON(cnt - remaining == 0)) {
1450 ieee80211_sta_eosp(sta);
1455 /* Note: this is ignored by firmware not supporting GO uAPSD */
1457 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
1459 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
1460 mvmsta->next_status_eosp = true;
1461 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
1463 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
1466 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1468 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1471 int iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
1472 struct iwl_rx_cmd_buffer *rxb,
1473 struct iwl_device_cmd *cmd)
1475 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1476 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
1477 struct ieee80211_sta *sta;
1478 u32 sta_id = le32_to_cpu(notif->sta_id);
1480 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
1484 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1485 if (!IS_ERR_OR_NULL(sta))
1486 ieee80211_sta_eosp(sta);
1492 void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
1493 struct iwl_mvm_sta *mvmsta, bool disable)
1495 struct iwl_mvm_add_sta_cmd cmd = {
1496 .add_modify = STA_MODE_MODIFY,
1497 .sta_id = mvmsta->sta_id,
1498 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
1499 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
1500 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1504 if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_DISABLE_STA_TX))
1507 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1509 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1512 void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
1513 struct ieee80211_sta *sta,
1516 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1518 spin_lock_bh(&mvm_sta->lock);
1520 if (mvm_sta->disable_tx == disable) {
1521 spin_unlock_bh(&mvm_sta->lock);
1525 mvm_sta->disable_tx = disable;
1528 * Tell mac80211 to start/stop queueing tx for this station,
1529 * but don't stop queueing if there are still pending frames
1532 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
1533 ieee80211_sta_block_awake(mvm->hw, sta, disable);
1535 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
1537 spin_unlock_bh(&mvm_sta->lock);
1540 void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
1541 struct iwl_mvm_vif *mvmvif,
1544 struct ieee80211_sta *sta;
1545 struct iwl_mvm_sta *mvm_sta;
1548 lockdep_assert_held(&mvm->mutex);
1550 /* Block/unblock all the stations of the given mvmvif */
1551 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
1552 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
1553 lockdep_is_held(&mvm->mutex));
1554 if (IS_ERR_OR_NULL(sta))
1557 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1558 if (mvm_sta->mac_id_n_color !=
1559 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
1562 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);