1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2002-2005, Instant802 Networks, Inc.
5 * Copyright 2013-2014 Intel Mobile Communications GmbH
6 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
7 * Copyright (C) 2018-2023 Intel Corporation
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netdevice.h>
14 #include <linux/types.h>
15 #include <linux/slab.h>
16 #include <linux/skbuff.h>
17 #include <linux/if_arp.h>
18 #include <linux/timer.h>
19 #include <linux/rtnetlink.h>
21 #include <net/codel.h>
22 #include <net/mac80211.h>
23 #include "ieee80211_i.h"
24 #include "driver-ops.h"
27 #include "debugfs_sta.h"
32 * DOC: STA information lifetime rules
34 * STA info structures (&struct sta_info) are managed in a hash table
35 * for faster lookup and a list for iteration. They are managed using
36 * RCU, i.e. access to the list and hash table is protected by RCU.
38 * Upon allocating a STA info structure with sta_info_alloc(), the caller
39 * owns that structure. It must then insert it into the hash table using
40 * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
41 * case (which acquires an rcu read section but must not be called from
42 * within one) will the pointer still be valid after the call. Note that
43 * the caller may not do much with the STA info before inserting it; in
44 * particular, it may not start any mesh peer link management or add
47 * When the insertion fails (sta_info_insert()) returns non-zero), the
48 * structure will have been freed by sta_info_insert()!
50 * Station entries are added by mac80211 when you establish a link with a
51 * peer. This means different things for the different type of interfaces
52 * we support. For a regular station this mean we add the AP sta when we
53 * receive an association response from the AP. For IBSS this occurs when
54 * get to know about a peer on the same IBSS. For WDS we add the sta for
55 * the peer immediately upon device open. When using AP mode we add stations
56 * for each respective station upon request from userspace through nl80211.
58 * In order to remove a STA info structure, various sta_info_destroy_*()
59 * calls are available.
61 * There is no concept of ownership on a STA entry; each structure is
62 * owned by the global hash table/list until it is removed. All users of
63 * the structure need to be RCU protected so that the structure won't be
64 * freed before they are done using it.
67 struct sta_link_alloc {
68 struct link_sta_info info;
69 struct ieee80211_link_sta sta;
70 struct rcu_head rcu_head;
73 static const struct rhashtable_params sta_rht_params = {
74 .nelem_hint = 3, /* start small */
75 .automatic_shrinking = true,
76 .head_offset = offsetof(struct sta_info, hash_node),
77 .key_offset = offsetof(struct sta_info, addr),
79 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
82 static const struct rhashtable_params link_sta_rht_params = {
83 .nelem_hint = 3, /* start small */
84 .automatic_shrinking = true,
85 .head_offset = offsetof(struct link_sta_info, link_hash_node),
86 .key_offset = offsetof(struct link_sta_info, addr),
88 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
91 static int sta_info_hash_del(struct ieee80211_local *local,
94 return rhltable_remove(&local->sta_hash, &sta->hash_node,
98 static int link_sta_info_hash_add(struct ieee80211_local *local,
99 struct link_sta_info *link_sta)
101 lockdep_assert_wiphy(local->hw.wiphy);
103 return rhltable_insert(&local->link_sta_hash,
104 &link_sta->link_hash_node, link_sta_rht_params);
107 static int link_sta_info_hash_del(struct ieee80211_local *local,
108 struct link_sta_info *link_sta)
110 lockdep_assert_wiphy(local->hw.wiphy);
112 return rhltable_remove(&local->link_sta_hash,
113 &link_sta->link_hash_node, link_sta_rht_params);
116 void ieee80211_purge_sta_txqs(struct sta_info *sta)
118 struct ieee80211_local *local = sta->sdata->local;
121 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
122 struct txq_info *txqi;
124 if (!sta->sta.txq[i])
127 txqi = to_txq_info(sta->sta.txq[i]);
129 ieee80211_txq_purge(local, txqi);
133 static void __cleanup_single_sta(struct sta_info *sta)
136 struct tid_ampdu_tx *tid_tx;
137 struct ieee80211_sub_if_data *sdata = sta->sdata;
138 struct ieee80211_local *local = sdata->local;
141 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
142 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
143 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
144 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
145 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
146 ps = &sdata->bss->ps;
147 else if (ieee80211_vif_is_mesh(&sdata->vif))
148 ps = &sdata->u.mesh.ps;
152 clear_sta_flag(sta, WLAN_STA_PS_STA);
153 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
154 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
156 atomic_dec(&ps->num_sta_ps);
159 ieee80211_purge_sta_txqs(sta);
161 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
162 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
163 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
164 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
167 if (ieee80211_vif_is_mesh(&sdata->vif))
168 mesh_sta_cleanup(sta);
170 cancel_work_sync(&sta->drv_deliver_wk);
173 * Destroy aggregation state here. It would be nice to wait for the
174 * driver to finish aggregation stop and then clean up, but for now
175 * drivers have to handle aggregation stop being requested, followed
176 * directly by station destruction.
178 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
179 kfree(sta->ampdu_mlme.tid_start_tx[i]);
180 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
183 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
188 static void cleanup_single_sta(struct sta_info *sta)
190 struct ieee80211_sub_if_data *sdata = sta->sdata;
191 struct ieee80211_local *local = sdata->local;
193 __cleanup_single_sta(sta);
194 sta_info_free(local, sta);
197 struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
200 return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
203 /* protected by RCU */
204 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
207 struct ieee80211_local *local = sdata->local;
208 struct rhlist_head *tmp;
209 struct sta_info *sta;
212 for_each_sta_info(local, addr, sta, tmp) {
213 if (sta->sdata == sdata) {
215 /* this is safe as the caller must already hold
216 * another rcu read section or the mutex
226 * Get sta info either from the specified interface
227 * or from one of its vlans
229 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
232 struct ieee80211_local *local = sdata->local;
233 struct rhlist_head *tmp;
234 struct sta_info *sta;
237 for_each_sta_info(local, addr, sta, tmp) {
238 if (sta->sdata == sdata ||
239 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
241 /* this is safe as the caller must already hold
242 * another rcu read section or the mutex
251 struct rhlist_head *link_sta_info_hash_lookup(struct ieee80211_local *local,
254 return rhltable_lookup(&local->link_sta_hash, addr,
255 link_sta_rht_params);
258 struct link_sta_info *
259 link_sta_info_get_bss(struct ieee80211_sub_if_data *sdata, const u8 *addr)
261 struct ieee80211_local *local = sdata->local;
262 struct rhlist_head *tmp;
263 struct link_sta_info *link_sta;
266 for_each_link_sta_info(local, addr, link_sta, tmp) {
267 struct sta_info *sta = link_sta->sta;
269 if (sta->sdata == sdata ||
270 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
272 /* this is safe as the caller must already hold
273 * another rcu read section or the mutex
282 struct ieee80211_sta *
283 ieee80211_find_sta_by_link_addrs(struct ieee80211_hw *hw,
286 unsigned int *link_id)
288 struct ieee80211_local *local = hw_to_local(hw);
289 struct link_sta_info *link_sta;
290 struct rhlist_head *tmp;
292 for_each_link_sta_info(local, addr, link_sta, tmp) {
293 struct sta_info *sta = link_sta->sta;
294 struct ieee80211_link_data *link;
295 u8 _link_id = link_sta->link_id;
303 link = rcu_dereference(sta->sdata->link[_link_id]);
307 if (memcmp(link->conf->addr, localaddr, ETH_ALEN))
317 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_link_addrs);
319 struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
320 const u8 *sta_addr, const u8 *vif_addr)
322 struct rhlist_head *tmp;
323 struct sta_info *sta;
325 for_each_sta_info(local, sta_addr, sta, tmp) {
326 if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
333 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
336 struct ieee80211_local *local = sdata->local;
337 struct sta_info *sta;
340 list_for_each_entry_rcu(sta, &local->sta_list, list,
341 lockdep_is_held(&local->hw.wiphy->mtx)) {
342 if (sdata != sta->sdata)
354 static void sta_info_free_link(struct link_sta_info *link_sta)
356 free_percpu(link_sta->pcpu_rx_stats);
359 static void sta_remove_link(struct sta_info *sta, unsigned int link_id,
362 struct sta_link_alloc *alloc = NULL;
363 struct link_sta_info *link_sta;
365 lockdep_assert_wiphy(sta->local->hw.wiphy);
367 link_sta = rcu_access_pointer(sta->link[link_id]);
368 if (WARN_ON(!link_sta))
372 link_sta_info_hash_del(sta->local, link_sta);
374 if (test_sta_flag(sta, WLAN_STA_INSERTED))
375 ieee80211_link_sta_debugfs_remove(link_sta);
377 if (link_sta != &sta->deflink)
378 alloc = container_of(link_sta, typeof(*alloc), info);
380 sta->sta.valid_links &= ~BIT(link_id);
381 RCU_INIT_POINTER(sta->link[link_id], NULL);
382 RCU_INIT_POINTER(sta->sta.link[link_id], NULL);
384 sta_info_free_link(&alloc->info);
385 kfree_rcu(alloc, rcu_head);
388 ieee80211_sta_recalc_aggregates(&sta->sta);
392 * sta_info_free - free STA
394 * @local: pointer to the global information
395 * @sta: STA info to free
397 * This function must undo everything done by sta_info_alloc()
398 * that may happen before sta_info_insert(). It may only be
399 * called when sta_info_insert() has not been attempted (and
400 * if that fails, the station is freed anyway.)
402 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
406 for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
407 struct link_sta_info *link_sta;
409 link_sta = rcu_access_pointer(sta->link[i]);
413 sta_remove_link(sta, i, false);
417 * If we had used sta_info_pre_move_state() then we might not
418 * have gone through the state transitions down again, so do
419 * it here now (and warn if it's inserted).
421 * This will clear state such as fast TX/RX that may have been
422 * allocated during state transitions.
424 while (sta->sta_state > IEEE80211_STA_NONE) {
427 WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
429 ret = sta_info_move_state(sta, sta->sta_state - 1);
430 if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
435 rate_control_free_sta(sta);
437 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
439 kfree(to_txq_info(sta->sta.txq[0]));
440 kfree(rcu_dereference_raw(sta->sta.rates));
441 #ifdef CONFIG_MAC80211_MESH
445 sta_info_free_link(&sta->deflink);
449 static int sta_info_hash_add(struct ieee80211_local *local,
450 struct sta_info *sta)
452 return rhltable_insert(&local->sta_hash, &sta->hash_node,
456 static void sta_deliver_ps_frames(struct work_struct *wk)
458 struct sta_info *sta;
460 sta = container_of(wk, struct sta_info, drv_deliver_wk);
466 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
467 ieee80211_sta_ps_deliver_wakeup(sta);
468 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
469 ieee80211_sta_ps_deliver_poll_response(sta);
470 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
471 ieee80211_sta_ps_deliver_uapsd(sta);
475 static int sta_prepare_rate_control(struct ieee80211_local *local,
476 struct sta_info *sta, gfp_t gfp)
478 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
481 sta->rate_ctrl = local->rate_ctrl;
482 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
484 if (!sta->rate_ctrl_priv)
490 static int sta_info_alloc_link(struct ieee80211_local *local,
491 struct link_sta_info *link_info,
494 struct ieee80211_hw *hw = &local->hw;
497 if (ieee80211_hw_check(hw, USES_RSS)) {
498 link_info->pcpu_rx_stats =
499 alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
500 if (!link_info->pcpu_rx_stats)
504 link_info->rx_stats.last_rx = jiffies;
505 u64_stats_init(&link_info->rx_stats.syncp);
507 ewma_signal_init(&link_info->rx_stats_avg.signal);
508 ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
509 for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
510 ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
515 static void sta_info_add_link(struct sta_info *sta,
516 unsigned int link_id,
517 struct link_sta_info *link_info,
518 struct ieee80211_link_sta *link_sta)
520 link_info->sta = sta;
521 link_info->link_id = link_id;
522 link_info->pub = link_sta;
523 link_info->pub->sta = &sta->sta;
524 link_sta->link_id = link_id;
525 rcu_assign_pointer(sta->link[link_id], link_info);
526 rcu_assign_pointer(sta->sta.link[link_id], link_sta);
528 link_sta->smps_mode = IEEE80211_SMPS_OFF;
529 link_sta->agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
532 static struct sta_info *
533 __sta_info_alloc(struct ieee80211_sub_if_data *sdata,
534 const u8 *addr, int link_id, const u8 *link_addr,
537 struct ieee80211_local *local = sdata->local;
538 struct ieee80211_hw *hw = &local->hw;
539 struct sta_info *sta;
544 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
551 if (sta_info_alloc_link(local, &sta->deflink, gfp))
555 sta_info_add_link(sta, link_id, &sta->deflink,
557 sta->sta.valid_links = BIT(link_id);
559 sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
562 sta->sta.cur = &sta->sta.deflink.agg;
564 spin_lock_init(&sta->lock);
565 spin_lock_init(&sta->ps_lock);
566 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
567 wiphy_work_init(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
568 #ifdef CONFIG_MAC80211_MESH
569 if (ieee80211_vif_is_mesh(&sdata->vif)) {
570 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
573 sta->mesh->plink_sta = sta;
574 spin_lock_init(&sta->mesh->plink_lock);
575 if (!sdata->u.mesh.user_mpm)
576 timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
578 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
582 memcpy(sta->addr, addr, ETH_ALEN);
583 memcpy(sta->sta.addr, addr, ETH_ALEN);
584 memcpy(sta->deflink.addr, link_addr, ETH_ALEN);
585 memcpy(sta->sta.deflink.addr, link_addr, ETH_ALEN);
586 sta->sta.max_rx_aggregation_subframes =
587 local->hw.max_rx_aggregation_subframes;
589 /* TODO link specific alloc and assignments for MLO Link STA */
591 /* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
592 * The Tx path starts to use a key as soon as the key slot ptk_idx
593 * references to is not NULL. To not use the initial Rx-only key
594 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
595 * which always will refer to a NULL key.
597 BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
598 sta->ptk_idx = INVALID_PTK_KEYIDX;
601 ieee80211_init_frag_cache(&sta->frags);
603 sta->sta_state = IEEE80211_STA_NONE;
605 if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
606 sta->amsdu_mesh_control = -1;
608 /* Mark TID as unreserved */
609 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
611 sta->last_connected = ktime_get_seconds();
613 size = sizeof(struct txq_info) +
614 ALIGN(hw->txq_data_size, sizeof(void *));
616 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
620 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
621 struct txq_info *txq = txq_data + i * size;
623 /* might not do anything for the (bufferable) MMPDU TXQ */
624 ieee80211_txq_init(sdata, sta, txq, i);
627 if (sta_prepare_rate_control(local, sta, gfp))
630 sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
632 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
633 skb_queue_head_init(&sta->ps_tx_buf[i]);
634 skb_queue_head_init(&sta->tx_filtered[i]);
635 sta->airtime[i].deficit = sta->airtime_weight;
636 atomic_set(&sta->airtime[i].aql_tx_pending, 0);
637 sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
638 sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
641 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
642 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
644 for (i = 0; i < NUM_NL80211_BANDS; i++) {
648 if (!hw->wiphy->bands[i])
652 case NL80211_BAND_2GHZ:
653 case NL80211_BAND_LC:
655 * We use both here, even if we cannot really know for
656 * sure the station will support both, but the only use
657 * for this is when we don't know anything yet and send
658 * management frames, and then we'll pick the lowest
659 * possible rate anyway.
660 * If we don't include _G here, we cannot find a rate
661 * in P2P, and thus trigger the WARN_ONCE() in rate.c
663 mandatory = IEEE80211_RATE_MANDATORY_B |
664 IEEE80211_RATE_MANDATORY_G;
666 case NL80211_BAND_5GHZ:
667 mandatory = IEEE80211_RATE_MANDATORY_A;
669 case NL80211_BAND_60GHZ:
675 for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
676 struct ieee80211_rate *rate;
678 rate = &hw->wiphy->bands[i]->bitrates[r];
680 if (!(rate->flags & mandatory))
682 sta->sta.deflink.supp_rates[i] |= BIT(r);
686 sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
687 sta->cparams.target = MS2TIME(20);
688 sta->cparams.interval = MS2TIME(100);
689 sta->cparams.ecn = true;
690 sta->cparams.ce_threshold_selector = 0;
691 sta->cparams.ce_threshold_mask = 0;
693 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
698 kfree(to_txq_info(sta->sta.txq[0]));
700 sta_info_free_link(&sta->deflink);
701 #ifdef CONFIG_MAC80211_MESH
708 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
709 const u8 *addr, gfp_t gfp)
711 return __sta_info_alloc(sdata, addr, -1, addr, gfp);
714 struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata,
716 unsigned int link_id,
720 return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp);
723 static int sta_info_insert_check(struct sta_info *sta)
725 struct ieee80211_sub_if_data *sdata = sta->sdata;
727 lockdep_assert_wiphy(sdata->local->hw.wiphy);
730 * Can't be a WARN_ON because it can be triggered through a race:
731 * something inserts a STA (on one CPU) without holding the RTNL
732 * and another CPU turns off the net device.
734 if (unlikely(!ieee80211_sdata_running(sdata)))
737 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
738 !is_valid_ether_addr(sta->sta.addr)))
741 /* The RCU read lock is required by rhashtable due to
742 * asynchronous resize/rehash. We also require the mutex
746 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
747 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
756 static int sta_info_insert_drv_state(struct ieee80211_local *local,
757 struct ieee80211_sub_if_data *sdata,
758 struct sta_info *sta)
760 enum ieee80211_sta_state state;
763 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
764 err = drv_sta_state(local, sdata, sta, state, state + 1);
771 * Drivers using legacy sta_add/sta_remove callbacks only
772 * get uploaded set to true after sta_add is called.
774 if (!local->ops->sta_add)
775 sta->uploaded = true;
779 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
781 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
782 sta->sta.addr, state + 1, err);
786 /* unwind on error */
787 for (; state > IEEE80211_STA_NOTEXIST; state--)
788 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
794 ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
796 struct ieee80211_local *local = sdata->local;
797 bool allow_p2p_go_ps = sdata->vif.p2p;
798 struct sta_info *sta;
801 list_for_each_entry_rcu(sta, &local->sta_list, list) {
802 if (sdata != sta->sdata ||
803 !test_sta_flag(sta, WLAN_STA_ASSOC))
805 if (!sta->sta.support_p2p_ps) {
806 allow_p2p_go_ps = false;
812 if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
813 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
814 ieee80211_link_info_change_notify(sdata, &sdata->deflink,
819 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
821 struct ieee80211_local *local = sta->local;
822 struct ieee80211_sub_if_data *sdata = sta->sdata;
823 struct station_info *sinfo = NULL;
826 lockdep_assert_wiphy(local->hw.wiphy);
828 /* check if STA exists already */
829 if (sta_info_get_bss(sdata, sta->sta.addr)) {
834 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
841 local->sta_generation++;
844 /* simplify things and don't accept BA sessions yet */
845 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
847 /* make the station visible */
848 err = sta_info_hash_add(local, sta);
852 if (sta->sta.valid_links) {
853 err = link_sta_info_hash_add(local, &sta->deflink);
855 sta_info_hash_del(local, sta);
860 list_add_tail_rcu(&sta->list, &local->sta_list);
862 /* update channel context before notifying the driver about state
863 * change, this enables driver using the updated channel context right away.
865 if (sta->sta_state >= IEEE80211_STA_ASSOC) {
866 ieee80211_recalc_min_chandef(sta->sdata, -1);
867 if (!sta->sta.support_p2p_ps)
868 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
872 err = sta_info_insert_drv_state(local, sdata, sta);
876 set_sta_flag(sta, WLAN_STA_INSERTED);
878 /* accept BA sessions now */
879 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
881 ieee80211_sta_debugfs_add(sta);
882 rate_control_add_sta_debugfs(sta);
883 if (sta->sta.valid_links) {
886 for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
887 struct link_sta_info *link_sta;
889 link_sta = rcu_dereference_protected(sta->link[i],
890 lockdep_is_held(&local->hw.wiphy->mtx));
895 ieee80211_link_sta_debugfs_add(link_sta);
896 if (sdata->vif.active_links & BIT(i))
897 ieee80211_link_sta_debugfs_drv_add(link_sta);
900 ieee80211_link_sta_debugfs_add(&sta->deflink);
901 ieee80211_link_sta_debugfs_drv_add(&sta->deflink);
904 sinfo->generation = local->sta_generation;
905 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
908 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
910 /* move reference to rcu-protected */
913 if (ieee80211_vif_is_mesh(&sdata->vif))
914 mesh_accept_plinks_update(sdata);
916 ieee80211_check_fast_xmit(sta);
920 if (sta->sta.valid_links)
921 link_sta_info_hash_del(local, &sta->deflink);
922 sta_info_hash_del(local, sta);
923 list_del_rcu(&sta->list);
928 cleanup_single_sta(sta);
934 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
936 struct ieee80211_local *local = sta->local;
940 lockdep_assert_wiphy(local->hw.wiphy);
942 err = sta_info_insert_check(sta);
944 sta_info_free(local, sta);
949 return sta_info_insert_finish(sta);
952 int sta_info_insert(struct sta_info *sta)
954 int err = sta_info_insert_rcu(sta);
961 static inline void __bss_tim_set(u8 *tim, u16 id)
964 * This format has been mandated by the IEEE specifications,
965 * so this line may not be changed to use the __set_bit() format.
967 tim[id / 8] |= (1 << (id % 8));
970 static inline void __bss_tim_clear(u8 *tim, u16 id)
973 * This format has been mandated by the IEEE specifications,
974 * so this line may not be changed to use the __clear_bit() format.
976 tim[id / 8] &= ~(1 << (id % 8));
979 static inline bool __bss_tim_get(u8 *tim, u16 id)
982 * This format has been mandated by the IEEE specifications,
983 * so this line may not be changed to use the test_bit() format.
985 return tim[id / 8] & (1 << (id % 8));
988 static unsigned long ieee80211_tids_for_ac(int ac)
990 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
992 case IEEE80211_AC_VO:
993 return BIT(6) | BIT(7);
994 case IEEE80211_AC_VI:
995 return BIT(4) | BIT(5);
996 case IEEE80211_AC_BE:
997 return BIT(0) | BIT(3);
998 case IEEE80211_AC_BK:
999 return BIT(1) | BIT(2);
1006 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
1008 struct ieee80211_local *local = sta->local;
1010 bool indicate_tim = false;
1011 u8 ignore_for_tim = sta->sta.uapsd_queues;
1013 u16 id = sta->sta.aid;
1015 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1016 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1017 if (WARN_ON_ONCE(!sta->sdata->bss))
1020 ps = &sta->sdata->bss->ps;
1021 #ifdef CONFIG_MAC80211_MESH
1022 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
1023 ps = &sta->sdata->u.mesh.ps;
1029 /* No need to do anything if the driver does all */
1030 if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
1037 * If all ACs are delivery-enabled then we should build
1038 * the TIM bit for all ACs anyway; if only some are then
1039 * we ignore those and build the TIM bit using only the
1042 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
1046 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
1048 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1051 if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
1054 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
1055 !skb_queue_empty(&sta->ps_tx_buf[ac]);
1059 tids = ieee80211_tids_for_ac(ac);
1062 sta->driver_buffered_tids & tids;
1064 sta->txq_buffered_tids & tids;
1068 spin_lock_bh(&local->tim_lock);
1070 if (indicate_tim == __bss_tim_get(ps->tim, id))
1074 __bss_tim_set(ps->tim, id);
1076 __bss_tim_clear(ps->tim, id);
1078 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
1079 local->tim_in_locked_section = true;
1080 drv_set_tim(local, &sta->sta, indicate_tim);
1081 local->tim_in_locked_section = false;
1085 spin_unlock_bh(&local->tim_lock);
1088 void sta_info_recalc_tim(struct sta_info *sta)
1090 __sta_info_recalc_tim(sta, false);
1093 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1095 struct ieee80211_tx_info *info;
1101 info = IEEE80211_SKB_CB(skb);
1103 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
1104 timeout = (sta->listen_interval *
1105 sta->sdata->vif.bss_conf.beacon_int *
1107 if (timeout < STA_TX_BUFFER_EXPIRE)
1108 timeout = STA_TX_BUFFER_EXPIRE;
1109 return time_after(jiffies, info->control.jiffies + timeout);
1113 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1114 struct sta_info *sta, int ac)
1116 unsigned long flags;
1117 struct sk_buff *skb;
1120 * First check for frames that should expire on the filtered
1121 * queue. Frames here were rejected by the driver and are on
1122 * a separate queue to avoid reordering with normal PS-buffered
1123 * frames. They also aren't accounted for right now in the
1124 * total_ps_buffered counter.
1127 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1128 skb = skb_peek(&sta->tx_filtered[ac]);
1129 if (sta_info_buffer_expired(sta, skb))
1130 skb = __skb_dequeue(&sta->tx_filtered[ac]);
1133 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1136 * Frames are queued in order, so if this one
1137 * hasn't expired yet we can stop testing. If
1138 * we actually reached the end of the queue we
1139 * also need to stop, of course.
1143 ieee80211_free_txskb(&local->hw, skb);
1147 * Now also check the normal PS-buffered queue, this will
1148 * only find something if the filtered queue was emptied
1149 * since the filtered frames are all before the normal PS
1153 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1154 skb = skb_peek(&sta->ps_tx_buf[ac]);
1155 if (sta_info_buffer_expired(sta, skb))
1156 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1159 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1162 * frames are queued in order, so if this one
1163 * hasn't expired yet (or we reached the end of
1164 * the queue) we can stop testing
1169 local->total_ps_buffered--;
1170 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1172 ieee80211_free_txskb(&local->hw, skb);
1176 * Finally, recalculate the TIM bit for this station -- it might
1177 * now be clear because the station was too slow to retrieve its
1180 sta_info_recalc_tim(sta);
1183 * Return whether there are any frames still buffered, this is
1184 * used to check whether the cleanup timer still needs to run,
1185 * if there are no frames we don't need to rearm the timer.
1187 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1188 skb_queue_empty(&sta->tx_filtered[ac]));
1191 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1192 struct sta_info *sta)
1194 bool have_buffered = false;
1197 /* This is only necessary for stations on BSS/MBSS interfaces */
1198 if (!sta->sdata->bss &&
1199 !ieee80211_vif_is_mesh(&sta->sdata->vif))
1202 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1204 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1206 return have_buffered;
1209 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
1211 struct ieee80211_local *local;
1212 struct ieee80211_sub_if_data *sdata;
1223 lockdep_assert_wiphy(local->hw.wiphy);
1226 * Before removing the station from the driver and
1227 * rate control, it might still start new aggregation
1228 * sessions -- block that to make sure the tear-down
1229 * will be sufficient.
1231 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1232 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1235 * Before removing the station from the driver there might be pending
1236 * rx frames on RSS queues sent prior to the disassociation - wait for
1237 * all such frames to be processed.
1239 drv_sync_rx_queues(local, sta);
1241 for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
1242 struct link_sta_info *link_sta;
1244 if (!(sta->sta.valid_links & BIT(i)))
1247 link_sta = rcu_dereference_protected(sta->link[i],
1248 lockdep_is_held(&local->hw.wiphy->mtx));
1250 link_sta_info_hash_del(local, link_sta);
1253 ret = sta_info_hash_del(local, sta);
1258 * for TDLS peers, make sure to return to the base channel before
1261 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1262 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1263 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1266 list_del_rcu(&sta->list);
1267 sta->removed = true;
1270 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
1272 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1273 rcu_access_pointer(sdata->u.vlan.sta) == sta)
1274 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1279 static int _sta_info_move_state(struct sta_info *sta,
1280 enum ieee80211_sta_state new_state,
1283 struct ieee80211_local *local = sta->local;
1287 if (sta->sta_state == new_state)
1290 /* check allowed transitions first */
1292 switch (new_state) {
1293 case IEEE80211_STA_NONE:
1294 if (sta->sta_state != IEEE80211_STA_AUTH)
1297 case IEEE80211_STA_AUTH:
1298 if (sta->sta_state != IEEE80211_STA_NONE &&
1299 sta->sta_state != IEEE80211_STA_ASSOC)
1302 case IEEE80211_STA_ASSOC:
1303 if (sta->sta_state != IEEE80211_STA_AUTH &&
1304 sta->sta_state != IEEE80211_STA_AUTHORIZED)
1307 case IEEE80211_STA_AUTHORIZED:
1308 if (sta->sta_state != IEEE80211_STA_ASSOC)
1312 WARN(1, "invalid state %d", new_state);
1316 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1317 sta->sta.addr, new_state);
1319 /* notify the driver before the actual changes so it can
1320 * fail the transition
1322 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1323 int err = drv_sta_state(sta->local, sta->sdata, sta,
1324 sta->sta_state, new_state);
1329 /* reflect the change in all state variables */
1331 switch (new_state) {
1332 case IEEE80211_STA_NONE:
1333 if (sta->sta_state == IEEE80211_STA_AUTH)
1334 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1336 case IEEE80211_STA_AUTH:
1337 if (sta->sta_state == IEEE80211_STA_NONE) {
1338 set_bit(WLAN_STA_AUTH, &sta->_flags);
1339 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1340 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1342 ieee80211_recalc_min_chandef(sta->sdata, -1);
1343 if (!sta->sta.support_p2p_ps)
1344 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1348 case IEEE80211_STA_ASSOC:
1349 if (sta->sta_state == IEEE80211_STA_AUTH) {
1350 set_bit(WLAN_STA_ASSOC, &sta->_flags);
1351 sta->assoc_at = ktime_get_boottime_ns();
1353 ieee80211_recalc_min_chandef(sta->sdata, -1);
1354 if (!sta->sta.support_p2p_ps)
1355 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1357 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1358 ieee80211_vif_dec_num_mcast(sta->sdata);
1359 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1362 * If we have encryption offload, flush (station) queues
1363 * (after ensuring concurrent TX completed) so we won't
1364 * transmit anything later unencrypted if/when keys are
1365 * also removed, which might otherwise happen depending
1366 * on how the hardware offload works.
1368 if (local->ops->set_key) {
1370 if (local->ops->flush_sta)
1371 drv_flush_sta(local, sta->sdata, sta);
1373 ieee80211_flush_queues(local,
1378 ieee80211_clear_fast_xmit(sta);
1379 ieee80211_clear_fast_rx(sta);
1382 case IEEE80211_STA_AUTHORIZED:
1383 if (sta->sta_state == IEEE80211_STA_ASSOC) {
1384 ieee80211_vif_inc_num_mcast(sta->sdata);
1385 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1386 ieee80211_check_fast_xmit(sta);
1387 ieee80211_check_fast_rx(sta);
1389 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1390 sta->sdata->vif.type == NL80211_IFTYPE_AP)
1391 cfg80211_send_layer2_update(sta->sdata->dev,
1398 sta->sta_state = new_state;
1403 int sta_info_move_state(struct sta_info *sta,
1404 enum ieee80211_sta_state new_state)
1406 return _sta_info_move_state(sta, new_state, true);
1409 static void __sta_info_destroy_part2(struct sta_info *sta, bool recalc)
1411 struct ieee80211_local *local = sta->local;
1412 struct ieee80211_sub_if_data *sdata = sta->sdata;
1413 struct station_info *sinfo;
1417 * NOTE: This assumes at least synchronize_net() was done
1418 * after _part1 and before _part2!
1422 * There's a potential race in _part1 where we set WLAN_STA_BLOCK_BA
1423 * but someone might have just gotten past a check, and not yet into
1424 * queuing the work/creating the data/etc.
1426 * Do another round of destruction so that the worker is certainly
1427 * canceled before we later free the station.
1429 * Since this is after synchronize_rcu()/synchronize_net() we're now
1430 * certain that nobody can actually hold a reference to the STA and
1431 * be calling e.g. ieee80211_start_tx_ba_session().
1433 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1436 lockdep_assert_wiphy(local->hw.wiphy);
1438 if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1439 ret = _sta_info_move_state(sta, IEEE80211_STA_ASSOC, recalc);
1443 /* now keys can no longer be reached */
1444 ieee80211_free_sta_keys(local, sta);
1446 /* disable TIM bit - last chance to tell driver */
1447 __sta_info_recalc_tim(sta, true);
1452 local->sta_generation++;
1454 while (sta->sta_state > IEEE80211_STA_NONE) {
1455 ret = _sta_info_move_state(sta, sta->sta_state - 1, recalc);
1462 if (sta->uploaded) {
1463 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1464 IEEE80211_STA_NOTEXIST);
1465 WARN_ON_ONCE(ret != 0);
1468 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1470 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1472 sta_set_sinfo(sta, sinfo, true);
1473 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
1476 ieee80211_sta_debugfs_remove(sta);
1478 ieee80211_destroy_frag_cache(&sta->frags);
1480 cleanup_single_sta(sta);
1483 int __must_check __sta_info_destroy(struct sta_info *sta)
1485 int err = __sta_info_destroy_part1(sta);
1492 __sta_info_destroy_part2(sta, true);
1497 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1499 struct sta_info *sta;
1501 lockdep_assert_wiphy(sdata->local->hw.wiphy);
1503 sta = sta_info_get(sdata, addr);
1504 return __sta_info_destroy(sta);
1507 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1510 struct sta_info *sta;
1512 lockdep_assert_wiphy(sdata->local->hw.wiphy);
1514 sta = sta_info_get_bss(sdata, addr);
1515 return __sta_info_destroy(sta);
1518 static void sta_info_cleanup(struct timer_list *t)
1520 struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1521 struct sta_info *sta;
1522 bool timer_needed = false;
1525 list_for_each_entry_rcu(sta, &local->sta_list, list)
1526 if (sta_info_cleanup_expire_buffered(local, sta))
1527 timer_needed = true;
1530 if (local->quiescing)
1536 mod_timer(&local->sta_cleanup,
1537 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1540 int sta_info_init(struct ieee80211_local *local)
1544 err = rhltable_init(&local->sta_hash, &sta_rht_params);
1548 err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1550 rhltable_destroy(&local->sta_hash);
1554 spin_lock_init(&local->tim_lock);
1555 INIT_LIST_HEAD(&local->sta_list);
1557 timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
1561 void sta_info_stop(struct ieee80211_local *local)
1563 del_timer_sync(&local->sta_cleanup);
1564 rhltable_destroy(&local->sta_hash);
1565 rhltable_destroy(&local->link_sta_hash);
1569 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1571 struct ieee80211_local *local = sdata->local;
1572 struct sta_info *sta, *tmp;
1573 LIST_HEAD(free_list);
1577 lockdep_assert_wiphy(local->hw.wiphy);
1579 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1580 WARN_ON(vlans && !sdata->bss);
1582 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1583 if (sdata == sta->sdata ||
1584 (vlans && sdata->bss == sta->sdata->bss)) {
1585 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1586 list_add(&sta->free_list, &free_list);
1591 if (!list_empty(&free_list)) {
1592 bool support_p2p_ps = true;
1595 list_for_each_entry_safe(sta, tmp, &free_list, free_list) {
1596 if (!sta->sta.support_p2p_ps)
1597 support_p2p_ps = false;
1598 __sta_info_destroy_part2(sta, false);
1601 ieee80211_recalc_min_chandef(sdata, -1);
1602 if (!support_p2p_ps)
1603 ieee80211_recalc_p2p_go_ps_allowed(sdata);
1609 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1610 unsigned long exp_time)
1612 struct ieee80211_local *local = sdata->local;
1613 struct sta_info *sta, *tmp;
1615 lockdep_assert_wiphy(local->hw.wiphy);
1617 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1618 unsigned long last_active = ieee80211_sta_last_active(sta);
1620 if (sdata != sta->sdata)
1623 if (time_is_before_jiffies(last_active + exp_time)) {
1624 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1627 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1628 test_sta_flag(sta, WLAN_STA_PS_STA))
1629 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1631 WARN_ON(__sta_info_destroy(sta));
1636 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1638 const u8 *localaddr)
1640 struct ieee80211_local *local = hw_to_local(hw);
1641 struct rhlist_head *tmp;
1642 struct sta_info *sta;
1645 * Just return a random station if localaddr is NULL
1646 * ... first in list.
1648 for_each_sta_info(local, addr, sta, tmp) {
1650 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1659 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1661 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1664 struct sta_info *sta;
1669 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1678 EXPORT_SYMBOL(ieee80211_find_sta);
1680 /* powersave support code */
1681 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1683 struct ieee80211_sub_if_data *sdata = sta->sdata;
1684 struct ieee80211_local *local = sdata->local;
1685 struct sk_buff_head pending;
1686 int filtered = 0, buffered = 0, ac, i;
1687 unsigned long flags;
1690 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1691 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1694 if (sdata->vif.type == NL80211_IFTYPE_AP)
1695 ps = &sdata->bss->ps;
1696 else if (ieee80211_vif_is_mesh(&sdata->vif))
1697 ps = &sdata->u.mesh.ps;
1701 clear_sta_flag(sta, WLAN_STA_SP);
1703 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1704 sta->driver_buffered_tids = 0;
1705 sta->txq_buffered_tids = 0;
1707 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1708 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1710 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1711 if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1714 schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1717 skb_queue_head_init(&pending);
1719 /* sync with ieee80211_tx_h_unicast_ps_buf */
1720 spin_lock(&sta->ps_lock);
1721 /* Send all buffered frames to the station */
1722 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1723 int count = skb_queue_len(&pending), tmp;
1725 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1726 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1727 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1728 tmp = skb_queue_len(&pending);
1729 filtered += tmp - count;
1732 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1733 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1734 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1735 tmp = skb_queue_len(&pending);
1736 buffered += tmp - count;
1739 ieee80211_add_pending_skbs(local, &pending);
1741 /* now we're no longer in the deliver code */
1742 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1744 /* The station might have polled and then woken up before we responded,
1745 * so clear these flags now to avoid them sticking around.
1747 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1748 clear_sta_flag(sta, WLAN_STA_UAPSD);
1749 spin_unlock(&sta->ps_lock);
1751 atomic_dec(&ps->num_sta_ps);
1753 local->total_ps_buffered -= buffered;
1755 sta_info_recalc_tim(sta);
1758 "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1759 sta->sta.addr, sta->sta.aid, filtered, buffered);
1761 ieee80211_check_fast_xmit(sta);
1764 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1765 enum ieee80211_frame_release_type reason,
1766 bool call_driver, bool more_data)
1768 struct ieee80211_sub_if_data *sdata = sta->sdata;
1769 struct ieee80211_local *local = sdata->local;
1770 struct ieee80211_qos_hdr *nullfunc;
1771 struct sk_buff *skb;
1772 int size = sizeof(*nullfunc);
1774 bool qos = sta->sta.wme;
1775 struct ieee80211_tx_info *info;
1776 struct ieee80211_chanctx_conf *chanctx_conf;
1779 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1780 IEEE80211_STYPE_QOS_NULLFUNC |
1781 IEEE80211_FCTL_FROMDS);
1784 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1785 IEEE80211_STYPE_NULLFUNC |
1786 IEEE80211_FCTL_FROMDS);
1789 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1793 skb_reserve(skb, local->hw.extra_tx_headroom);
1795 nullfunc = skb_put(skb, size);
1796 nullfunc->frame_control = fc;
1797 nullfunc->duration_id = 0;
1798 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1799 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1800 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1801 nullfunc->seq_ctrl = 0;
1803 skb->priority = tid;
1804 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1806 nullfunc->qos_ctrl = cpu_to_le16(tid);
1808 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1809 nullfunc->qos_ctrl |=
1810 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1812 nullfunc->frame_control |=
1813 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1817 info = IEEE80211_SKB_CB(skb);
1820 * Tell TX path to send this frame even though the
1821 * STA may still remain is PS mode after this frame
1822 * exchange. Also set EOSP to indicate this packet
1823 * ends the poll/service period.
1825 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1826 IEEE80211_TX_STATUS_EOSP |
1827 IEEE80211_TX_CTL_REQ_TX_STATUS;
1829 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1832 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1835 skb->dev = sdata->dev;
1838 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
1839 if (WARN_ON(!chanctx_conf)) {
1845 info->band = chanctx_conf->def.chan->band;
1846 ieee80211_xmit(sdata, sta, skb);
1850 static int find_highest_prio_tid(unsigned long tids)
1852 /* lower 3 TIDs aren't ordered perfectly */
1854 return fls(tids) - 1;
1855 /* TID 0 is BE just like TID 3 */
1858 return fls(tids) - 1;
1861 /* Indicates if the MORE_DATA bit should be set in the last
1862 * frame obtained by ieee80211_sta_ps_get_frames.
1863 * Note that driver_release_tids is relevant only if
1864 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1867 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1868 enum ieee80211_frame_release_type reason,
1869 unsigned long driver_release_tids)
1873 /* If the driver has data on more than one TID then
1874 * certainly there's more data if we release just a
1875 * single frame now (from a single TID). This will
1876 * only happen for PS-Poll.
1878 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1879 hweight16(driver_release_tids) > 1)
1882 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1883 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1886 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1887 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1895 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1896 enum ieee80211_frame_release_type reason,
1897 struct sk_buff_head *frames,
1898 unsigned long *driver_release_tids)
1900 struct ieee80211_sub_if_data *sdata = sta->sdata;
1901 struct ieee80211_local *local = sdata->local;
1904 /* Get response frame(s) and more data bit for the last one. */
1905 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1908 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1911 tids = ieee80211_tids_for_ac(ac);
1913 /* if we already have frames from software, then we can't also
1914 * release from hardware queues
1916 if (skb_queue_empty(frames)) {
1917 *driver_release_tids |=
1918 sta->driver_buffered_tids & tids;
1919 *driver_release_tids |= sta->txq_buffered_tids & tids;
1922 if (!*driver_release_tids) {
1923 struct sk_buff *skb;
1925 while (n_frames > 0) {
1926 skb = skb_dequeue(&sta->tx_filtered[ac]);
1929 &sta->ps_tx_buf[ac]);
1931 local->total_ps_buffered--;
1936 __skb_queue_tail(frames, skb);
1940 /* If we have more frames buffered on this AC, then abort the
1941 * loop since we can't send more data from other ACs before
1942 * the buffered frames from this.
1944 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1945 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1951 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1952 int n_frames, u8 ignored_acs,
1953 enum ieee80211_frame_release_type reason)
1955 struct ieee80211_sub_if_data *sdata = sta->sdata;
1956 struct ieee80211_local *local = sdata->local;
1957 unsigned long driver_release_tids = 0;
1958 struct sk_buff_head frames;
1961 /* Service or PS-Poll period starts */
1962 set_sta_flag(sta, WLAN_STA_SP);
1964 __skb_queue_head_init(&frames);
1966 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1967 &frames, &driver_release_tids);
1969 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1971 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1972 driver_release_tids =
1973 BIT(find_highest_prio_tid(driver_release_tids));
1975 if (skb_queue_empty(&frames) && !driver_release_tids) {
1979 * For PS-Poll, this can only happen due to a race condition
1980 * when we set the TIM bit and the station notices it, but
1981 * before it can poll for the frame we expire it.
1983 * For uAPSD, this is said in the standard (11.2.1.5 h):
1984 * At each unscheduled SP for a non-AP STA, the AP shall
1985 * attempt to transmit at least one MSDU or MMPDU, but no
1986 * more than the value specified in the Max SP Length field
1987 * in the QoS Capability element from delivery-enabled ACs,
1988 * that are destined for the non-AP STA.
1990 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1993 /* This will evaluate to 1, 3, 5 or 7. */
1994 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1995 if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1999 ieee80211_send_null_response(sta, tid, reason, true, false);
2000 } else if (!driver_release_tids) {
2001 struct sk_buff_head pending;
2002 struct sk_buff *skb;
2005 bool need_null = false;
2007 skb_queue_head_init(&pending);
2009 while ((skb = __skb_dequeue(&frames))) {
2010 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2011 struct ieee80211_hdr *hdr = (void *) skb->data;
2017 * Tell TX path to send this frame even though the
2018 * STA may still remain is PS mode after this frame
2021 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
2022 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
2025 * Use MoreData flag to indicate whether there are
2026 * more buffered frames for this STA
2028 if (more_data || !skb_queue_empty(&frames))
2029 hdr->frame_control |=
2030 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2032 hdr->frame_control &=
2033 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
2035 if (ieee80211_is_data_qos(hdr->frame_control) ||
2036 ieee80211_is_qos_nullfunc(hdr->frame_control))
2037 qoshdr = ieee80211_get_qos_ctl(hdr);
2039 tids |= BIT(skb->priority);
2041 __skb_queue_tail(&pending, skb);
2043 /* end service period after last frame or add one */
2044 if (!skb_queue_empty(&frames))
2047 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
2048 /* for PS-Poll, there's only one frame */
2049 info->flags |= IEEE80211_TX_STATUS_EOSP |
2050 IEEE80211_TX_CTL_REQ_TX_STATUS;
2054 /* For uAPSD, things are a bit more complicated. If the
2055 * last frame has a QoS header (i.e. is a QoS-data or
2056 * QoS-nulldata frame) then just set the EOSP bit there
2058 * If the frame doesn't have a QoS header (which means
2059 * it should be a bufferable MMPDU) then we can't set
2060 * the EOSP bit in the QoS header; add a QoS-nulldata
2061 * frame to the list to send it after the MMPDU.
2063 * Note that this code is only in the mac80211-release
2064 * code path, we assume that the driver will not buffer
2065 * anything but QoS-data frames, or if it does, will
2066 * create the QoS-nulldata frame by itself if needed.
2068 * Cf. 802.11-2012 10.2.1.10 (c).
2071 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
2073 info->flags |= IEEE80211_TX_STATUS_EOSP |
2074 IEEE80211_TX_CTL_REQ_TX_STATUS;
2076 /* The standard isn't completely clear on this
2077 * as it says the more-data bit should be set
2078 * if there are more BUs. The QoS-Null frame
2079 * we're about to send isn't buffered yet, we
2080 * only create it below, but let's pretend it
2081 * was buffered just in case some clients only
2082 * expect more-data=0 when eosp=1.
2084 hdr->frame_control |=
2085 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2092 drv_allow_buffered_frames(local, sta, tids, num,
2095 ieee80211_add_pending_skbs(local, &pending);
2098 ieee80211_send_null_response(
2099 sta, find_highest_prio_tid(tids),
2100 reason, false, false);
2102 sta_info_recalc_tim(sta);
2107 * We need to release a frame that is buffered somewhere in the
2108 * driver ... it'll have to handle that.
2109 * Note that the driver also has to check the number of frames
2110 * on the TIDs we're releasing from - if there are more than
2111 * n_frames it has to set the more-data bit (if we didn't ask
2112 * it to set it anyway due to other buffered frames); if there
2113 * are fewer than n_frames it has to make sure to adjust that
2114 * to allow the service period to end properly.
2116 drv_release_buffered_frames(local, sta, driver_release_tids,
2117 n_frames, reason, more_data);
2120 * Note that we don't recalculate the TIM bit here as it would
2121 * most likely have no effect at all unless the driver told us
2122 * that the TID(s) became empty before returning here from the
2124 * Either way, however, when the driver tells us that the TID(s)
2125 * became empty or we find that a txq became empty, we'll do the
2126 * TIM recalculation.
2129 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
2130 if (!sta->sta.txq[tid] ||
2131 !(driver_release_tids & BIT(tid)) ||
2132 txq_has_queue(sta->sta.txq[tid]))
2135 sta_info_recalc_tim(sta);
2141 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
2143 u8 ignore_for_response = sta->sta.uapsd_queues;
2146 * If all ACs are delivery-enabled then we should reply
2147 * from any of them, if only some are enabled we reply
2148 * only from the non-enabled ones.
2150 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
2151 ignore_for_response = 0;
2153 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
2154 IEEE80211_FRAME_RELEASE_PSPOLL);
2157 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
2159 int n_frames = sta->sta.max_sp;
2160 u8 delivery_enabled = sta->sta.uapsd_queues;
2163 * If we ever grow support for TSPEC this might happen if
2164 * the TSPEC update from hostapd comes in between a trigger
2165 * frame setting WLAN_STA_UAPSD in the RX path and this
2166 * actually getting called.
2168 if (!delivery_enabled)
2171 switch (sta->sta.max_sp) {
2182 /* XXX: what is a good value? */
2187 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
2188 IEEE80211_FRAME_RELEASE_UAPSD);
2191 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
2192 struct ieee80211_sta *pubsta, bool block)
2194 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2196 trace_api_sta_block_awake(sta->local, pubsta, block);
2199 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
2200 ieee80211_clear_fast_xmit(sta);
2204 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
2207 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
2208 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
2209 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2210 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2211 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
2212 test_sta_flag(sta, WLAN_STA_UAPSD)) {
2213 /* must be asleep in this case */
2214 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2215 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2217 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2218 ieee80211_check_fast_xmit(sta);
2221 EXPORT_SYMBOL(ieee80211_sta_block_awake);
2223 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
2225 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2226 struct ieee80211_local *local = sta->local;
2228 trace_api_eosp(local, pubsta);
2230 clear_sta_flag(sta, WLAN_STA_SP);
2232 EXPORT_SYMBOL(ieee80211_sta_eosp);
2234 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
2236 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2237 enum ieee80211_frame_release_type reason;
2240 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
2242 reason = IEEE80211_FRAME_RELEASE_UAPSD;
2243 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
2246 ieee80211_send_null_response(sta, tid, reason, false, more_data);
2248 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
2250 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2251 u8 tid, bool buffered)
2253 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2255 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2258 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
2261 set_bit(tid, &sta->driver_buffered_tids);
2263 clear_bit(tid, &sta->driver_buffered_tids);
2265 sta_info_recalc_tim(sta);
2267 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2269 void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2270 u32 tx_airtime, u32 rx_airtime)
2272 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2273 struct ieee80211_local *local = sta->sdata->local;
2274 u8 ac = ieee80211_ac_from_tid(tid);
2277 if (sta->local->airtime_flags & AIRTIME_USE_TX)
2278 airtime += tx_airtime;
2279 if (sta->local->airtime_flags & AIRTIME_USE_RX)
2280 airtime += rx_airtime;
2282 spin_lock_bh(&local->active_txq_lock[ac]);
2283 sta->airtime[ac].tx_airtime += tx_airtime;
2284 sta->airtime[ac].rx_airtime += rx_airtime;
2286 if (ieee80211_sta_keep_active(sta, ac))
2287 sta->airtime[ac].deficit -= airtime;
2289 spin_unlock_bh(&local->active_txq_lock[ac]);
2291 EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2293 void __ieee80211_sta_recalc_aggregates(struct sta_info *sta, u16 active_links)
2298 if (!sta->sta.valid_links || !sta->sta.mlo) {
2299 sta->sta.cur = &sta->sta.deflink.agg;
2304 for (link_id = 0; link_id < ARRAY_SIZE((sta)->link); link_id++) {
2305 struct ieee80211_link_sta *link_sta;
2308 if (!(active_links & BIT(link_id)))
2311 link_sta = rcu_dereference(sta->sta.link[link_id]);
2316 sta->cur = sta->sta.deflink.agg;
2321 sta->cur.max_amsdu_len =
2322 min(sta->cur.max_amsdu_len,
2323 link_sta->agg.max_amsdu_len);
2324 sta->cur.max_rc_amsdu_len =
2325 min(sta->cur.max_rc_amsdu_len,
2326 link_sta->agg.max_rc_amsdu_len);
2328 for (i = 0; i < ARRAY_SIZE(sta->cur.max_tid_amsdu_len); i++)
2329 sta->cur.max_tid_amsdu_len[i] =
2330 min(sta->cur.max_tid_amsdu_len[i],
2331 link_sta->agg.max_tid_amsdu_len[i]);
2335 sta->sta.cur = &sta->cur;
2338 void ieee80211_sta_recalc_aggregates(struct ieee80211_sta *pubsta)
2340 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2342 __ieee80211_sta_recalc_aggregates(sta, sta->sdata->vif.active_links);
2344 EXPORT_SYMBOL(ieee80211_sta_recalc_aggregates);
2346 void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
2347 struct sta_info *sta, u8 ac,
2348 u16 tx_airtime, bool tx_completed)
2352 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2355 if (!tx_completed) {
2357 atomic_add(tx_airtime,
2358 &sta->airtime[ac].aql_tx_pending);
2360 atomic_add(tx_airtime, &local->aql_total_pending_airtime);
2361 atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
2366 tx_pending = atomic_sub_return(tx_airtime,
2367 &sta->airtime[ac].aql_tx_pending);
2369 atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
2373 atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
2374 tx_pending = atomic_sub_return(tx_airtime,
2375 &local->aql_ac_pending_airtime[ac]);
2376 if (WARN_ONCE(tx_pending < 0,
2377 "Device %s AC %d pending airtime underflow: %u, %u",
2378 wiphy_name(local->hw.wiphy), ac, tx_pending,
2380 atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
2382 atomic_sub(tx_pending, &local->aql_total_pending_airtime);
2386 static struct ieee80211_sta_rx_stats *
2387 sta_get_last_rx_stats(struct sta_info *sta)
2389 struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2392 if (!sta->deflink.pcpu_rx_stats)
2395 for_each_possible_cpu(cpu) {
2396 struct ieee80211_sta_rx_stats *cpustats;
2398 cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2400 if (time_after(cpustats->last_rx, stats->last_rx))
2407 static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
2408 struct rate_info *rinfo)
2410 rinfo->bw = STA_STATS_GET(BW, rate);
2412 switch (STA_STATS_GET(TYPE, rate)) {
2413 case STA_STATS_RATE_TYPE_VHT:
2414 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2415 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2416 rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2417 if (STA_STATS_GET(SGI, rate))
2418 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2420 case STA_STATS_RATE_TYPE_HT:
2421 rinfo->flags = RATE_INFO_FLAGS_MCS;
2422 rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2423 if (STA_STATS_GET(SGI, rate))
2424 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2426 case STA_STATS_RATE_TYPE_LEGACY: {
2427 struct ieee80211_supported_band *sband;
2430 int band = STA_STATS_GET(LEGACY_BAND, rate);
2431 int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2433 sband = local->hw.wiphy->bands[band];
2435 if (WARN_ON_ONCE(!sband->bitrates))
2438 brate = sband->bitrates[rate_idx].bitrate;
2439 if (rinfo->bw == RATE_INFO_BW_5)
2441 else if (rinfo->bw == RATE_INFO_BW_10)
2445 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
2448 case STA_STATS_RATE_TYPE_HE:
2449 rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
2450 rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
2451 rinfo->nss = STA_STATS_GET(HE_NSS, rate);
2452 rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
2453 rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
2454 rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
2456 case STA_STATS_RATE_TYPE_EHT:
2457 rinfo->flags = RATE_INFO_FLAGS_EHT_MCS;
2458 rinfo->mcs = STA_STATS_GET(EHT_MCS, rate);
2459 rinfo->nss = STA_STATS_GET(EHT_NSS, rate);
2460 rinfo->eht_gi = STA_STATS_GET(EHT_GI, rate);
2461 rinfo->eht_ru_alloc = STA_STATS_GET(EHT_RU, rate);
2466 static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
2468 u32 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
2470 if (rate == STA_STATS_RATE_INVALID)
2473 sta_stats_decode_rate(sta->local, rate, rinfo);
2477 static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2484 start = u64_stats_fetch_begin(&rxstats->syncp);
2485 value = rxstats->msdu[tid];
2486 } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2491 static void sta_set_tidstats(struct sta_info *sta,
2492 struct cfg80211_tid_stats *tidstats,
2495 struct ieee80211_local *local = sta->local;
2498 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2499 tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2502 if (sta->deflink.pcpu_rx_stats) {
2503 for_each_possible_cpu(cpu) {
2504 struct ieee80211_sta_rx_stats *cpurxs;
2506 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2508 tidstats->rx_msdu +=
2509 sta_get_tidstats_msdu(cpurxs, tid);
2513 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2516 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2517 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2518 tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
2521 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2522 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2523 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2524 tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
2527 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2528 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2529 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2530 tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
2533 if (tid < IEEE80211_NUM_TIDS) {
2534 spin_lock_bh(&local->fq.lock);
2537 tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
2538 ieee80211_fill_txq_stats(&tidstats->txq_stats,
2539 to_txq_info(sta->sta.txq[tid]));
2542 spin_unlock_bh(&local->fq.lock);
2546 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2552 start = u64_stats_fetch_begin(&rxstats->syncp);
2553 value = rxstats->bytes;
2554 } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2559 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
2562 struct ieee80211_sub_if_data *sdata = sta->sdata;
2563 struct ieee80211_local *local = sdata->local;
2566 struct ieee80211_sta_rx_stats *last_rxstats;
2568 last_rxstats = sta_get_last_rx_stats(sta);
2570 sinfo->generation = sdata->local->sta_generation;
2572 /* do before driver, so beacon filtering drivers have a
2573 * chance to e.g. just add the number of filtered beacons
2574 * (or just modify the value entirely, of course)
2576 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2577 sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2579 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2580 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2581 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2582 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2583 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
2584 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2585 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2587 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2588 sinfo->beacon_loss_count =
2589 sdata->deflink.u.mgd.beacon_loss_count;
2590 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2593 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2594 sinfo->assoc_at = sta->assoc_at;
2595 sinfo->inactive_time =
2596 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2598 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2599 BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2600 sinfo->tx_bytes = 0;
2601 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2602 sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2603 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2606 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
2607 sinfo->tx_packets = 0;
2608 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2609 sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2610 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
2613 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2614 BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2615 sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
2617 if (sta->deflink.pcpu_rx_stats) {
2618 for_each_possible_cpu(cpu) {
2619 struct ieee80211_sta_rx_stats *cpurxs;
2621 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2623 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2627 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
2630 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2631 sinfo->rx_packets = sta->deflink.rx_stats.packets;
2632 if (sta->deflink.pcpu_rx_stats) {
2633 for_each_possible_cpu(cpu) {
2634 struct ieee80211_sta_rx_stats *cpurxs;
2636 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2638 sinfo->rx_packets += cpurxs->packets;
2641 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
2644 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2645 sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2646 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
2649 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2650 sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2651 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
2654 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2655 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2656 sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2657 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2660 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2661 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2662 sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2663 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2666 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2667 sinfo->airtime_weight = sta->airtime_weight;
2668 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2671 sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2672 if (sta->deflink.pcpu_rx_stats) {
2673 for_each_possible_cpu(cpu) {
2674 struct ieee80211_sta_rx_stats *cpurxs;
2676 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2677 sinfo->rx_dropped_misc += cpurxs->dropped;
2681 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2682 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2683 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2684 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2685 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2688 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2689 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2690 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2691 sinfo->signal = (s8)last_rxstats->last_signal;
2692 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2695 if (!sta->deflink.pcpu_rx_stats &&
2696 !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
2698 -ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2699 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
2703 /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2704 * the sta->rx_stats struct, so the check here is fine with and without
2707 if (last_rxstats->chains &&
2708 !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2709 BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2710 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2711 if (!sta->deflink.pcpu_rx_stats)
2712 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2714 sinfo->chains = last_rxstats->chains;
2716 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2717 sinfo->chain_signal[i] =
2718 last_rxstats->chain_signal_last[i];
2719 sinfo->chain_signal_avg[i] =
2720 -ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2724 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
2725 !sta->sta.valid_links &&
2726 ieee80211_rate_valid(&sta->deflink.tx_stats.last_rate)) {
2727 sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2729 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2732 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
2733 !sta->sta.valid_links) {
2734 if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2735 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
2738 if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
2739 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
2740 sta_set_tidstats(sta, &sinfo->pertid[i], i);
2743 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2744 #ifdef CONFIG_MAC80211_MESH
2745 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2746 BIT_ULL(NL80211_STA_INFO_PLID) |
2747 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2748 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2749 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2750 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
2751 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
2752 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2754 sinfo->llid = sta->mesh->llid;
2755 sinfo->plid = sta->mesh->plid;
2756 sinfo->plink_state = sta->mesh->plink_state;
2757 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2758 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2759 sinfo->t_offset = sta->mesh->t_offset;
2761 sinfo->local_pm = sta->mesh->local_pm;
2762 sinfo->peer_pm = sta->mesh->peer_pm;
2763 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2764 sinfo->connected_to_gate = sta->mesh->connected_to_gate;
2765 sinfo->connected_to_as = sta->mesh->connected_to_as;
2769 sinfo->bss_param.flags = 0;
2770 if (sdata->vif.bss_conf.use_cts_prot)
2771 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2772 if (sdata->vif.bss_conf.use_short_preamble)
2773 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2774 if (sdata->vif.bss_conf.use_short_slot)
2775 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2776 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2777 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2779 sinfo->sta_flags.set = 0;
2780 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2781 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2782 BIT(NL80211_STA_FLAG_WME) |
2783 BIT(NL80211_STA_FLAG_MFP) |
2784 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2785 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2786 BIT(NL80211_STA_FLAG_TDLS_PEER);
2787 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2788 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2789 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2790 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2792 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2793 if (test_sta_flag(sta, WLAN_STA_MFP))
2794 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2795 if (test_sta_flag(sta, WLAN_STA_AUTH))
2796 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2797 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2798 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2799 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2800 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2802 thr = sta_get_expected_throughput(sta);
2805 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2806 sinfo->expected_throughput = thr;
2809 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2810 sta->deflink.status_stats.ack_signal_filled) {
2811 sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2812 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2815 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2816 sta->deflink.status_stats.ack_signal_filled) {
2817 sinfo->avg_ack_signal =
2818 -(s8)ewma_avg_signal_read(
2819 &sta->deflink.status_stats.avg_ack_signal);
2821 BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2824 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2825 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2826 sinfo->airtime_link_metric =
2827 airtime_link_metric_get(local, sta);
2831 u32 sta_get_expected_throughput(struct sta_info *sta)
2833 struct ieee80211_sub_if_data *sdata = sta->sdata;
2834 struct ieee80211_local *local = sdata->local;
2835 struct rate_control_ref *ref = NULL;
2838 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2839 ref = local->rate_ctrl;
2841 /* check if the driver has a SW RC implementation */
2842 if (ref && ref->ops->get_expected_throughput)
2843 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2845 thr = drv_get_expected_throughput(local, sta);
2850 unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2852 struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2854 if (!sta->deflink.status_stats.last_ack ||
2855 time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2856 return stats->last_rx;
2857 return sta->deflink.status_stats.last_ack;
2860 static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2862 if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2863 sta->cparams.target = MS2TIME(50);
2864 sta->cparams.interval = MS2TIME(300);
2865 sta->cparams.ecn = false;
2867 sta->cparams.target = MS2TIME(20);
2868 sta->cparams.interval = MS2TIME(100);
2869 sta->cparams.ecn = true;
2873 void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2876 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2878 sta_update_codel_params(sta, thr);
2881 int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2883 struct ieee80211_sub_if_data *sdata = sta->sdata;
2884 struct sta_link_alloc *alloc;
2887 lockdep_assert_wiphy(sdata->local->hw.wiphy);
2889 WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED));
2891 /* must represent an MLD from the start */
2892 if (WARN_ON(!sta->sta.valid_links))
2895 if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2896 sta->link[link_id]))
2899 alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2903 ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2909 sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2911 ieee80211_link_sta_debugfs_add(&alloc->info);
2916 void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
2918 lockdep_assert_wiphy(sta->sdata->local->hw.wiphy);
2920 WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED));
2922 sta_remove_link(sta, link_id, false);
2925 int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2927 struct ieee80211_sub_if_data *sdata = sta->sdata;
2928 struct link_sta_info *link_sta;
2929 u16 old_links = sta->sta.valid_links;
2930 u16 new_links = old_links | BIT(link_id);
2933 link_sta = rcu_dereference_protected(sta->link[link_id],
2934 lockdep_is_held(&sdata->local->hw.wiphy->mtx));
2936 if (WARN_ON(old_links == new_links || !link_sta))
2940 if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) {
2944 /* we only modify under the mutex so this is fine */
2947 sta->sta.valid_links = new_links;
2949 if (WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED)))
2952 ieee80211_recalc_min_chandef(sdata, link_id);
2954 /* Ensure the values are updated for the driver,
2955 * redone by sta_remove_link on failure.
2957 ieee80211_sta_recalc_aggregates(&sta->sta);
2959 ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2960 old_links, new_links);
2962 sta->sta.valid_links = old_links;
2963 sta_remove_link(sta, link_id, false);
2968 ret = link_sta_info_hash_add(sdata->local, link_sta);
2973 void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2975 struct ieee80211_sub_if_data *sdata = sta->sdata;
2976 u16 old_links = sta->sta.valid_links;
2978 lockdep_assert_wiphy(sdata->local->hw.wiphy);
2980 sta->sta.valid_links &= ~BIT(link_id);
2982 if (!WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED)))
2983 drv_change_sta_links(sdata->local, sdata, &sta->sta,
2984 old_links, sta->sta.valid_links);
2986 sta_remove_link(sta, link_id, true);
2989 void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
2990 const u8 *ext_capab,
2991 unsigned int ext_capab_len)
2995 sta->sta.max_amsdu_subframes = 0;
2997 if (ext_capab_len < 8)
3000 /* The sender might not have sent the last bit, consider it to be 0 */
3001 val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
3003 /* we did get all the bits, take the MSB as well */
3004 if (ext_capab_len >= 9)
3005 val |= u8_get_bits(ext_capab[8],
3006 WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
3009 sta->sta.max_amsdu_subframes = 4 << (4 - val);
3012 #ifdef CONFIG_LOCKDEP
3013 bool lockdep_sta_mutex_held(struct ieee80211_sta *pubsta)
3015 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
3017 return lockdep_is_held(&sta->local->hw.wiphy->mtx);
3019 EXPORT_SYMBOL(lockdep_sta_mutex_held);