1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2002-2005, Instant802 Networks, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
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 if (!(sta->sta.valid_links & BIT(i)))
410 sta_remove_link(sta, i, false);
414 * If we had used sta_info_pre_move_state() then we might not
415 * have gone through the state transitions down again, so do
416 * it here now (and warn if it's inserted).
418 * This will clear state such as fast TX/RX that may have been
419 * allocated during state transitions.
421 while (sta->sta_state > IEEE80211_STA_NONE) {
424 WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
426 ret = sta_info_move_state(sta, sta->sta_state - 1);
427 if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
432 rate_control_free_sta(sta);
434 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
436 kfree(to_txq_info(sta->sta.txq[0]));
437 kfree(rcu_dereference_raw(sta->sta.rates));
438 #ifdef CONFIG_MAC80211_MESH
442 sta_info_free_link(&sta->deflink);
446 static int sta_info_hash_add(struct ieee80211_local *local,
447 struct sta_info *sta)
449 return rhltable_insert(&local->sta_hash, &sta->hash_node,
453 static void sta_deliver_ps_frames(struct work_struct *wk)
455 struct sta_info *sta;
457 sta = container_of(wk, struct sta_info, drv_deliver_wk);
463 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
464 ieee80211_sta_ps_deliver_wakeup(sta);
465 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
466 ieee80211_sta_ps_deliver_poll_response(sta);
467 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
468 ieee80211_sta_ps_deliver_uapsd(sta);
472 static int sta_prepare_rate_control(struct ieee80211_local *local,
473 struct sta_info *sta, gfp_t gfp)
475 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
478 sta->rate_ctrl = local->rate_ctrl;
479 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
481 if (!sta->rate_ctrl_priv)
487 static int sta_info_alloc_link(struct ieee80211_local *local,
488 struct link_sta_info *link_info,
491 struct ieee80211_hw *hw = &local->hw;
494 if (ieee80211_hw_check(hw, USES_RSS)) {
495 link_info->pcpu_rx_stats =
496 alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
497 if (!link_info->pcpu_rx_stats)
501 link_info->rx_stats.last_rx = jiffies;
502 u64_stats_init(&link_info->rx_stats.syncp);
504 ewma_signal_init(&link_info->rx_stats_avg.signal);
505 ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
506 for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
507 ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
512 static void sta_info_add_link(struct sta_info *sta,
513 unsigned int link_id,
514 struct link_sta_info *link_info,
515 struct ieee80211_link_sta *link_sta)
517 link_info->sta = sta;
518 link_info->link_id = link_id;
519 link_info->pub = link_sta;
520 link_info->pub->sta = &sta->sta;
521 link_sta->link_id = link_id;
522 rcu_assign_pointer(sta->link[link_id], link_info);
523 rcu_assign_pointer(sta->sta.link[link_id], link_sta);
525 link_sta->smps_mode = IEEE80211_SMPS_OFF;
526 link_sta->agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
529 static struct sta_info *
530 __sta_info_alloc(struct ieee80211_sub_if_data *sdata,
531 const u8 *addr, int link_id, const u8 *link_addr,
534 struct ieee80211_local *local = sdata->local;
535 struct ieee80211_hw *hw = &local->hw;
536 struct sta_info *sta;
541 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
548 if (sta_info_alloc_link(local, &sta->deflink, gfp))
552 sta_info_add_link(sta, link_id, &sta->deflink,
554 sta->sta.valid_links = BIT(link_id);
556 sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
559 sta->sta.cur = &sta->sta.deflink.agg;
561 spin_lock_init(&sta->lock);
562 spin_lock_init(&sta->ps_lock);
563 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
564 wiphy_work_init(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
565 #ifdef CONFIG_MAC80211_MESH
566 if (ieee80211_vif_is_mesh(&sdata->vif)) {
567 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
570 sta->mesh->plink_sta = sta;
571 spin_lock_init(&sta->mesh->plink_lock);
572 if (!sdata->u.mesh.user_mpm)
573 timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
575 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
579 memcpy(sta->addr, addr, ETH_ALEN);
580 memcpy(sta->sta.addr, addr, ETH_ALEN);
581 memcpy(sta->deflink.addr, link_addr, ETH_ALEN);
582 memcpy(sta->sta.deflink.addr, link_addr, ETH_ALEN);
583 sta->sta.max_rx_aggregation_subframes =
584 local->hw.max_rx_aggregation_subframes;
586 /* TODO link specific alloc and assignments for MLO Link STA */
588 /* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
589 * The Tx path starts to use a key as soon as the key slot ptk_idx
590 * references to is not NULL. To not use the initial Rx-only key
591 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
592 * which always will refer to a NULL key.
594 BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
595 sta->ptk_idx = INVALID_PTK_KEYIDX;
598 ieee80211_init_frag_cache(&sta->frags);
600 sta->sta_state = IEEE80211_STA_NONE;
602 if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
603 sta->amsdu_mesh_control = -1;
605 /* Mark TID as unreserved */
606 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
608 sta->last_connected = ktime_get_seconds();
610 size = sizeof(struct txq_info) +
611 ALIGN(hw->txq_data_size, sizeof(void *));
613 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
617 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
618 struct txq_info *txq = txq_data + i * size;
620 /* might not do anything for the (bufferable) MMPDU TXQ */
621 ieee80211_txq_init(sdata, sta, txq, i);
624 if (sta_prepare_rate_control(local, sta, gfp))
627 sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
629 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
630 skb_queue_head_init(&sta->ps_tx_buf[i]);
631 skb_queue_head_init(&sta->tx_filtered[i]);
632 sta->airtime[i].deficit = sta->airtime_weight;
633 atomic_set(&sta->airtime[i].aql_tx_pending, 0);
634 sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
635 sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
638 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
639 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
641 for (i = 0; i < NUM_NL80211_BANDS; i++) {
645 if (!hw->wiphy->bands[i])
649 case NL80211_BAND_2GHZ:
650 case NL80211_BAND_LC:
652 * We use both here, even if we cannot really know for
653 * sure the station will support both, but the only use
654 * for this is when we don't know anything yet and send
655 * management frames, and then we'll pick the lowest
656 * possible rate anyway.
657 * If we don't include _G here, we cannot find a rate
658 * in P2P, and thus trigger the WARN_ONCE() in rate.c
660 mandatory = IEEE80211_RATE_MANDATORY_B |
661 IEEE80211_RATE_MANDATORY_G;
663 case NL80211_BAND_5GHZ:
664 mandatory = IEEE80211_RATE_MANDATORY_A;
666 case NL80211_BAND_60GHZ:
672 for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
673 struct ieee80211_rate *rate;
675 rate = &hw->wiphy->bands[i]->bitrates[r];
677 if (!(rate->flags & mandatory))
679 sta->sta.deflink.supp_rates[i] |= BIT(r);
683 sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
684 sta->cparams.target = MS2TIME(20);
685 sta->cparams.interval = MS2TIME(100);
686 sta->cparams.ecn = true;
687 sta->cparams.ce_threshold_selector = 0;
688 sta->cparams.ce_threshold_mask = 0;
690 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
695 kfree(to_txq_info(sta->sta.txq[0]));
697 sta_info_free_link(&sta->deflink);
698 #ifdef CONFIG_MAC80211_MESH
705 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
706 const u8 *addr, gfp_t gfp)
708 return __sta_info_alloc(sdata, addr, -1, addr, gfp);
711 struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata,
713 unsigned int link_id,
717 return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp);
720 static int sta_info_insert_check(struct sta_info *sta)
722 struct ieee80211_sub_if_data *sdata = sta->sdata;
724 lockdep_assert_wiphy(sdata->local->hw.wiphy);
727 * Can't be a WARN_ON because it can be triggered through a race:
728 * something inserts a STA (on one CPU) without holding the RTNL
729 * and another CPU turns off the net device.
731 if (unlikely(!ieee80211_sdata_running(sdata)))
734 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
735 !is_valid_ether_addr(sta->sta.addr)))
738 /* The RCU read lock is required by rhashtable due to
739 * asynchronous resize/rehash. We also require the mutex
743 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
744 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
753 static int sta_info_insert_drv_state(struct ieee80211_local *local,
754 struct ieee80211_sub_if_data *sdata,
755 struct sta_info *sta)
757 enum ieee80211_sta_state state;
760 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
761 err = drv_sta_state(local, sdata, sta, state, state + 1);
768 * Drivers using legacy sta_add/sta_remove callbacks only
769 * get uploaded set to true after sta_add is called.
771 if (!local->ops->sta_add)
772 sta->uploaded = true;
776 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
778 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
779 sta->sta.addr, state + 1, err);
783 /* unwind on error */
784 for (; state > IEEE80211_STA_NOTEXIST; state--)
785 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
791 ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
793 struct ieee80211_local *local = sdata->local;
794 bool allow_p2p_go_ps = sdata->vif.p2p;
795 struct sta_info *sta;
798 list_for_each_entry_rcu(sta, &local->sta_list, list) {
799 if (sdata != sta->sdata ||
800 !test_sta_flag(sta, WLAN_STA_ASSOC))
802 if (!sta->sta.support_p2p_ps) {
803 allow_p2p_go_ps = false;
809 if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
810 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
811 ieee80211_link_info_change_notify(sdata, &sdata->deflink,
816 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
818 struct ieee80211_local *local = sta->local;
819 struct ieee80211_sub_if_data *sdata = sta->sdata;
820 struct station_info *sinfo = NULL;
823 lockdep_assert_wiphy(local->hw.wiphy);
825 /* check if STA exists already */
826 if (sta_info_get_bss(sdata, sta->sta.addr)) {
831 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
838 local->sta_generation++;
841 /* simplify things and don't accept BA sessions yet */
842 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
844 /* make the station visible */
845 err = sta_info_hash_add(local, sta);
849 if (sta->sta.valid_links) {
850 err = link_sta_info_hash_add(local, &sta->deflink);
852 sta_info_hash_del(local, sta);
857 list_add_tail_rcu(&sta->list, &local->sta_list);
859 /* update channel context before notifying the driver about state
860 * change, this enables driver using the updated channel context right away.
862 if (sta->sta_state >= IEEE80211_STA_ASSOC) {
863 ieee80211_recalc_min_chandef(sta->sdata, -1);
864 if (!sta->sta.support_p2p_ps)
865 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
869 err = sta_info_insert_drv_state(local, sdata, sta);
873 set_sta_flag(sta, WLAN_STA_INSERTED);
875 /* accept BA sessions now */
876 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
878 ieee80211_sta_debugfs_add(sta);
879 rate_control_add_sta_debugfs(sta);
880 if (sta->sta.valid_links) {
883 for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
884 struct link_sta_info *link_sta;
886 link_sta = rcu_dereference_protected(sta->link[i],
887 lockdep_is_held(&local->hw.wiphy->mtx));
892 ieee80211_link_sta_debugfs_add(link_sta);
893 if (sdata->vif.active_links & BIT(i))
894 ieee80211_link_sta_debugfs_drv_add(link_sta);
897 ieee80211_link_sta_debugfs_add(&sta->deflink);
898 ieee80211_link_sta_debugfs_drv_add(&sta->deflink);
901 sinfo->generation = local->sta_generation;
902 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
905 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
907 /* move reference to rcu-protected */
910 if (ieee80211_vif_is_mesh(&sdata->vif))
911 mesh_accept_plinks_update(sdata);
915 if (sta->sta.valid_links)
916 link_sta_info_hash_del(local, &sta->deflink);
917 sta_info_hash_del(local, sta);
918 list_del_rcu(&sta->list);
923 cleanup_single_sta(sta);
929 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
931 struct ieee80211_local *local = sta->local;
935 lockdep_assert_wiphy(local->hw.wiphy);
937 err = sta_info_insert_check(sta);
939 sta_info_free(local, sta);
944 return sta_info_insert_finish(sta);
947 int sta_info_insert(struct sta_info *sta)
949 int err = sta_info_insert_rcu(sta);
956 static inline void __bss_tim_set(u8 *tim, u16 id)
959 * This format has been mandated by the IEEE specifications,
960 * so this line may not be changed to use the __set_bit() format.
962 tim[id / 8] |= (1 << (id % 8));
965 static inline void __bss_tim_clear(u8 *tim, u16 id)
968 * This format has been mandated by the IEEE specifications,
969 * so this line may not be changed to use the __clear_bit() format.
971 tim[id / 8] &= ~(1 << (id % 8));
974 static inline bool __bss_tim_get(u8 *tim, u16 id)
977 * This format has been mandated by the IEEE specifications,
978 * so this line may not be changed to use the test_bit() format.
980 return tim[id / 8] & (1 << (id % 8));
983 static unsigned long ieee80211_tids_for_ac(int ac)
985 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
987 case IEEE80211_AC_VO:
988 return BIT(6) | BIT(7);
989 case IEEE80211_AC_VI:
990 return BIT(4) | BIT(5);
991 case IEEE80211_AC_BE:
992 return BIT(0) | BIT(3);
993 case IEEE80211_AC_BK:
994 return BIT(1) | BIT(2);
1001 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
1003 struct ieee80211_local *local = sta->local;
1005 bool indicate_tim = false;
1006 u8 ignore_for_tim = sta->sta.uapsd_queues;
1008 u16 id = sta->sta.aid;
1010 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1011 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1012 if (WARN_ON_ONCE(!sta->sdata->bss))
1015 ps = &sta->sdata->bss->ps;
1016 #ifdef CONFIG_MAC80211_MESH
1017 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
1018 ps = &sta->sdata->u.mesh.ps;
1024 /* No need to do anything if the driver does all */
1025 if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
1032 * If all ACs are delivery-enabled then we should build
1033 * the TIM bit for all ACs anyway; if only some are then
1034 * we ignore those and build the TIM bit using only the
1037 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
1041 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
1043 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1046 if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
1049 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
1050 !skb_queue_empty(&sta->ps_tx_buf[ac]);
1054 tids = ieee80211_tids_for_ac(ac);
1057 sta->driver_buffered_tids & tids;
1059 sta->txq_buffered_tids & tids;
1063 spin_lock_bh(&local->tim_lock);
1065 if (indicate_tim == __bss_tim_get(ps->tim, id))
1069 __bss_tim_set(ps->tim, id);
1071 __bss_tim_clear(ps->tim, id);
1073 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
1074 local->tim_in_locked_section = true;
1075 drv_set_tim(local, &sta->sta, indicate_tim);
1076 local->tim_in_locked_section = false;
1080 spin_unlock_bh(&local->tim_lock);
1083 void sta_info_recalc_tim(struct sta_info *sta)
1085 __sta_info_recalc_tim(sta, false);
1088 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1090 struct ieee80211_tx_info *info;
1096 info = IEEE80211_SKB_CB(skb);
1098 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
1099 timeout = (sta->listen_interval *
1100 sta->sdata->vif.bss_conf.beacon_int *
1102 if (timeout < STA_TX_BUFFER_EXPIRE)
1103 timeout = STA_TX_BUFFER_EXPIRE;
1104 return time_after(jiffies, info->control.jiffies + timeout);
1108 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1109 struct sta_info *sta, int ac)
1111 unsigned long flags;
1112 struct sk_buff *skb;
1115 * First check for frames that should expire on the filtered
1116 * queue. Frames here were rejected by the driver and are on
1117 * a separate queue to avoid reordering with normal PS-buffered
1118 * frames. They also aren't accounted for right now in the
1119 * total_ps_buffered counter.
1122 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1123 skb = skb_peek(&sta->tx_filtered[ac]);
1124 if (sta_info_buffer_expired(sta, skb))
1125 skb = __skb_dequeue(&sta->tx_filtered[ac]);
1128 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1131 * Frames are queued in order, so if this one
1132 * hasn't expired yet we can stop testing. If
1133 * we actually reached the end of the queue we
1134 * also need to stop, of course.
1138 ieee80211_free_txskb(&local->hw, skb);
1142 * Now also check the normal PS-buffered queue, this will
1143 * only find something if the filtered queue was emptied
1144 * since the filtered frames are all before the normal PS
1148 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1149 skb = skb_peek(&sta->ps_tx_buf[ac]);
1150 if (sta_info_buffer_expired(sta, skb))
1151 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1154 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1157 * frames are queued in order, so if this one
1158 * hasn't expired yet (or we reached the end of
1159 * the queue) we can stop testing
1164 local->total_ps_buffered--;
1165 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1167 ieee80211_free_txskb(&local->hw, skb);
1171 * Finally, recalculate the TIM bit for this station -- it might
1172 * now be clear because the station was too slow to retrieve its
1175 sta_info_recalc_tim(sta);
1178 * Return whether there are any frames still buffered, this is
1179 * used to check whether the cleanup timer still needs to run,
1180 * if there are no frames we don't need to rearm the timer.
1182 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1183 skb_queue_empty(&sta->tx_filtered[ac]));
1186 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1187 struct sta_info *sta)
1189 bool have_buffered = false;
1192 /* This is only necessary for stations on BSS/MBSS interfaces */
1193 if (!sta->sdata->bss &&
1194 !ieee80211_vif_is_mesh(&sta->sdata->vif))
1197 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1199 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1201 return have_buffered;
1204 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
1206 struct ieee80211_local *local;
1207 struct ieee80211_sub_if_data *sdata;
1218 lockdep_assert_wiphy(local->hw.wiphy);
1221 * Before removing the station from the driver and
1222 * rate control, it might still start new aggregation
1223 * sessions -- block that to make sure the tear-down
1224 * will be sufficient.
1226 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1227 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1230 * Before removing the station from the driver there might be pending
1231 * rx frames on RSS queues sent prior to the disassociation - wait for
1232 * all such frames to be processed.
1234 drv_sync_rx_queues(local, sta);
1236 for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
1237 struct link_sta_info *link_sta;
1239 if (!(sta->sta.valid_links & BIT(i)))
1242 link_sta = rcu_dereference_protected(sta->link[i],
1243 lockdep_is_held(&local->hw.wiphy->mtx));
1245 link_sta_info_hash_del(local, link_sta);
1248 ret = sta_info_hash_del(local, sta);
1253 * for TDLS peers, make sure to return to the base channel before
1256 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1257 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1258 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1261 list_del_rcu(&sta->list);
1262 sta->removed = true;
1265 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
1267 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1268 rcu_access_pointer(sdata->u.vlan.sta) == sta)
1269 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1274 static int _sta_info_move_state(struct sta_info *sta,
1275 enum ieee80211_sta_state new_state,
1278 struct ieee80211_local *local = sta->local;
1282 if (sta->sta_state == new_state)
1285 /* check allowed transitions first */
1287 switch (new_state) {
1288 case IEEE80211_STA_NONE:
1289 if (sta->sta_state != IEEE80211_STA_AUTH)
1292 case IEEE80211_STA_AUTH:
1293 if (sta->sta_state != IEEE80211_STA_NONE &&
1294 sta->sta_state != IEEE80211_STA_ASSOC)
1297 case IEEE80211_STA_ASSOC:
1298 if (sta->sta_state != IEEE80211_STA_AUTH &&
1299 sta->sta_state != IEEE80211_STA_AUTHORIZED)
1302 case IEEE80211_STA_AUTHORIZED:
1303 if (sta->sta_state != IEEE80211_STA_ASSOC)
1307 WARN(1, "invalid state %d", new_state);
1311 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1312 sta->sta.addr, new_state);
1314 /* notify the driver before the actual changes so it can
1315 * fail the transition
1317 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1318 int err = drv_sta_state(sta->local, sta->sdata, sta,
1319 sta->sta_state, new_state);
1324 /* reflect the change in all state variables */
1326 switch (new_state) {
1327 case IEEE80211_STA_NONE:
1328 if (sta->sta_state == IEEE80211_STA_AUTH)
1329 clear_bit(WLAN_STA_AUTH, &sta->_flags);
1331 case IEEE80211_STA_AUTH:
1332 if (sta->sta_state == IEEE80211_STA_NONE) {
1333 set_bit(WLAN_STA_AUTH, &sta->_flags);
1334 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1335 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1337 ieee80211_recalc_min_chandef(sta->sdata, -1);
1338 if (!sta->sta.support_p2p_ps)
1339 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1343 case IEEE80211_STA_ASSOC:
1344 if (sta->sta_state == IEEE80211_STA_AUTH) {
1345 set_bit(WLAN_STA_ASSOC, &sta->_flags);
1346 sta->assoc_at = ktime_get_boottime_ns();
1348 ieee80211_recalc_min_chandef(sta->sdata, -1);
1349 if (!sta->sta.support_p2p_ps)
1350 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1352 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1353 ieee80211_vif_dec_num_mcast(sta->sdata);
1354 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1357 * If we have encryption offload, flush (station) queues
1358 * (after ensuring concurrent TX completed) so we won't
1359 * transmit anything later unencrypted if/when keys are
1360 * also removed, which might otherwise happen depending
1361 * on how the hardware offload works.
1363 if (local->ops->set_key) {
1365 if (local->ops->flush_sta)
1366 drv_flush_sta(local, sta->sdata, sta);
1368 ieee80211_flush_queues(local,
1373 ieee80211_clear_fast_xmit(sta);
1374 ieee80211_clear_fast_rx(sta);
1377 case IEEE80211_STA_AUTHORIZED:
1378 if (sta->sta_state == IEEE80211_STA_ASSOC) {
1379 ieee80211_vif_inc_num_mcast(sta->sdata);
1380 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1381 ieee80211_check_fast_xmit(sta);
1382 ieee80211_check_fast_rx(sta);
1384 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1385 sta->sdata->vif.type == NL80211_IFTYPE_AP)
1386 cfg80211_send_layer2_update(sta->sdata->dev,
1393 sta->sta_state = new_state;
1398 int sta_info_move_state(struct sta_info *sta,
1399 enum ieee80211_sta_state new_state)
1401 return _sta_info_move_state(sta, new_state, true);
1404 static void __sta_info_destroy_part2(struct sta_info *sta, bool recalc)
1406 struct ieee80211_local *local = sta->local;
1407 struct ieee80211_sub_if_data *sdata = sta->sdata;
1408 struct station_info *sinfo;
1412 * NOTE: This assumes at least synchronize_net() was done
1413 * after _part1 and before _part2!
1417 * There's a potential race in _part1 where we set WLAN_STA_BLOCK_BA
1418 * but someone might have just gotten past a check, and not yet into
1419 * queuing the work/creating the data/etc.
1421 * Do another round of destruction so that the worker is certainly
1422 * canceled before we later free the station.
1424 * Since this is after synchronize_rcu()/synchronize_net() we're now
1425 * certain that nobody can actually hold a reference to the STA and
1426 * be calling e.g. ieee80211_start_tx_ba_session().
1428 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1431 lockdep_assert_wiphy(local->hw.wiphy);
1433 if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1434 ret = _sta_info_move_state(sta, IEEE80211_STA_ASSOC, recalc);
1438 /* now keys can no longer be reached */
1439 ieee80211_free_sta_keys(local, sta);
1441 /* disable TIM bit - last chance to tell driver */
1442 __sta_info_recalc_tim(sta, true);
1447 local->sta_generation++;
1449 while (sta->sta_state > IEEE80211_STA_NONE) {
1450 ret = _sta_info_move_state(sta, sta->sta_state - 1, recalc);
1457 if (sta->uploaded) {
1458 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1459 IEEE80211_STA_NOTEXIST);
1460 WARN_ON_ONCE(ret != 0);
1463 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1465 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1467 sta_set_sinfo(sta, sinfo, true);
1468 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
1471 ieee80211_sta_debugfs_remove(sta);
1473 ieee80211_destroy_frag_cache(&sta->frags);
1475 cleanup_single_sta(sta);
1478 int __must_check __sta_info_destroy(struct sta_info *sta)
1480 int err = __sta_info_destroy_part1(sta);
1487 __sta_info_destroy_part2(sta, true);
1492 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1494 struct sta_info *sta;
1496 lockdep_assert_wiphy(sdata->local->hw.wiphy);
1498 sta = sta_info_get(sdata, addr);
1499 return __sta_info_destroy(sta);
1502 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1505 struct sta_info *sta;
1507 lockdep_assert_wiphy(sdata->local->hw.wiphy);
1509 sta = sta_info_get_bss(sdata, addr);
1510 return __sta_info_destroy(sta);
1513 static void sta_info_cleanup(struct timer_list *t)
1515 struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1516 struct sta_info *sta;
1517 bool timer_needed = false;
1520 list_for_each_entry_rcu(sta, &local->sta_list, list)
1521 if (sta_info_cleanup_expire_buffered(local, sta))
1522 timer_needed = true;
1525 if (local->quiescing)
1531 mod_timer(&local->sta_cleanup,
1532 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1535 int sta_info_init(struct ieee80211_local *local)
1539 err = rhltable_init(&local->sta_hash, &sta_rht_params);
1543 err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1545 rhltable_destroy(&local->sta_hash);
1549 spin_lock_init(&local->tim_lock);
1550 INIT_LIST_HEAD(&local->sta_list);
1552 timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
1556 void sta_info_stop(struct ieee80211_local *local)
1558 del_timer_sync(&local->sta_cleanup);
1559 rhltable_destroy(&local->sta_hash);
1560 rhltable_destroy(&local->link_sta_hash);
1564 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1566 struct ieee80211_local *local = sdata->local;
1567 struct sta_info *sta, *tmp;
1568 LIST_HEAD(free_list);
1572 lockdep_assert_wiphy(local->hw.wiphy);
1574 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1575 WARN_ON(vlans && !sdata->bss);
1577 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1578 if (sdata == sta->sdata ||
1579 (vlans && sdata->bss == sta->sdata->bss)) {
1580 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1581 list_add(&sta->free_list, &free_list);
1586 if (!list_empty(&free_list)) {
1587 bool support_p2p_ps = true;
1590 list_for_each_entry_safe(sta, tmp, &free_list, free_list) {
1591 if (!sta->sta.support_p2p_ps)
1592 support_p2p_ps = false;
1593 __sta_info_destroy_part2(sta, false);
1596 ieee80211_recalc_min_chandef(sdata, -1);
1597 if (!support_p2p_ps)
1598 ieee80211_recalc_p2p_go_ps_allowed(sdata);
1604 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1605 unsigned long exp_time)
1607 struct ieee80211_local *local = sdata->local;
1608 struct sta_info *sta, *tmp;
1610 lockdep_assert_wiphy(local->hw.wiphy);
1612 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1613 unsigned long last_active = ieee80211_sta_last_active(sta);
1615 if (sdata != sta->sdata)
1618 if (time_is_before_jiffies(last_active + exp_time)) {
1619 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1622 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1623 test_sta_flag(sta, WLAN_STA_PS_STA))
1624 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1626 WARN_ON(__sta_info_destroy(sta));
1631 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1633 const u8 *localaddr)
1635 struct ieee80211_local *local = hw_to_local(hw);
1636 struct rhlist_head *tmp;
1637 struct sta_info *sta;
1640 * Just return a random station if localaddr is NULL
1641 * ... first in list.
1643 for_each_sta_info(local, addr, sta, tmp) {
1645 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1654 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1656 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1659 struct sta_info *sta;
1664 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1673 EXPORT_SYMBOL(ieee80211_find_sta);
1675 /* powersave support code */
1676 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1678 struct ieee80211_sub_if_data *sdata = sta->sdata;
1679 struct ieee80211_local *local = sdata->local;
1680 struct sk_buff_head pending;
1681 int filtered = 0, buffered = 0, ac, i;
1682 unsigned long flags;
1685 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1686 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1689 if (sdata->vif.type == NL80211_IFTYPE_AP)
1690 ps = &sdata->bss->ps;
1691 else if (ieee80211_vif_is_mesh(&sdata->vif))
1692 ps = &sdata->u.mesh.ps;
1696 clear_sta_flag(sta, WLAN_STA_SP);
1698 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1699 sta->driver_buffered_tids = 0;
1700 sta->txq_buffered_tids = 0;
1702 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1703 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1705 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1706 if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1709 schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1712 skb_queue_head_init(&pending);
1714 /* sync with ieee80211_tx_h_unicast_ps_buf */
1715 spin_lock(&sta->ps_lock);
1716 /* Send all buffered frames to the station */
1717 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1718 int count = skb_queue_len(&pending), tmp;
1720 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1721 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1722 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1723 tmp = skb_queue_len(&pending);
1724 filtered += tmp - count;
1727 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1728 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1729 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1730 tmp = skb_queue_len(&pending);
1731 buffered += tmp - count;
1734 ieee80211_add_pending_skbs(local, &pending);
1736 /* now we're no longer in the deliver code */
1737 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1739 /* The station might have polled and then woken up before we responded,
1740 * so clear these flags now to avoid them sticking around.
1742 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1743 clear_sta_flag(sta, WLAN_STA_UAPSD);
1744 spin_unlock(&sta->ps_lock);
1746 atomic_dec(&ps->num_sta_ps);
1748 local->total_ps_buffered -= buffered;
1750 sta_info_recalc_tim(sta);
1753 "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1754 sta->sta.addr, sta->sta.aid, filtered, buffered);
1756 ieee80211_check_fast_xmit(sta);
1759 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1760 enum ieee80211_frame_release_type reason,
1761 bool call_driver, bool more_data)
1763 struct ieee80211_sub_if_data *sdata = sta->sdata;
1764 struct ieee80211_local *local = sdata->local;
1765 struct ieee80211_qos_hdr *nullfunc;
1766 struct sk_buff *skb;
1767 int size = sizeof(*nullfunc);
1769 bool qos = sta->sta.wme;
1770 struct ieee80211_tx_info *info;
1771 struct ieee80211_chanctx_conf *chanctx_conf;
1774 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1775 IEEE80211_STYPE_QOS_NULLFUNC |
1776 IEEE80211_FCTL_FROMDS);
1779 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1780 IEEE80211_STYPE_NULLFUNC |
1781 IEEE80211_FCTL_FROMDS);
1784 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1788 skb_reserve(skb, local->hw.extra_tx_headroom);
1790 nullfunc = skb_put(skb, size);
1791 nullfunc->frame_control = fc;
1792 nullfunc->duration_id = 0;
1793 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1794 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1795 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1796 nullfunc->seq_ctrl = 0;
1798 skb->priority = tid;
1799 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1801 nullfunc->qos_ctrl = cpu_to_le16(tid);
1803 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1804 nullfunc->qos_ctrl |=
1805 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1807 nullfunc->frame_control |=
1808 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1812 info = IEEE80211_SKB_CB(skb);
1815 * Tell TX path to send this frame even though the
1816 * STA may still remain is PS mode after this frame
1817 * exchange. Also set EOSP to indicate this packet
1818 * ends the poll/service period.
1820 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1821 IEEE80211_TX_STATUS_EOSP |
1822 IEEE80211_TX_CTL_REQ_TX_STATUS;
1824 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1827 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1830 skb->dev = sdata->dev;
1833 chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
1834 if (WARN_ON(!chanctx_conf)) {
1840 info->band = chanctx_conf->def.chan->band;
1841 ieee80211_xmit(sdata, sta, skb);
1845 static int find_highest_prio_tid(unsigned long tids)
1847 /* lower 3 TIDs aren't ordered perfectly */
1849 return fls(tids) - 1;
1850 /* TID 0 is BE just like TID 3 */
1853 return fls(tids) - 1;
1856 /* Indicates if the MORE_DATA bit should be set in the last
1857 * frame obtained by ieee80211_sta_ps_get_frames.
1858 * Note that driver_release_tids is relevant only if
1859 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1862 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1863 enum ieee80211_frame_release_type reason,
1864 unsigned long driver_release_tids)
1868 /* If the driver has data on more than one TID then
1869 * certainly there's more data if we release just a
1870 * single frame now (from a single TID). This will
1871 * only happen for PS-Poll.
1873 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1874 hweight16(driver_release_tids) > 1)
1877 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1878 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1881 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1882 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1890 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1891 enum ieee80211_frame_release_type reason,
1892 struct sk_buff_head *frames,
1893 unsigned long *driver_release_tids)
1895 struct ieee80211_sub_if_data *sdata = sta->sdata;
1896 struct ieee80211_local *local = sdata->local;
1899 /* Get response frame(s) and more data bit for the last one. */
1900 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1903 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1906 tids = ieee80211_tids_for_ac(ac);
1908 /* if we already have frames from software, then we can't also
1909 * release from hardware queues
1911 if (skb_queue_empty(frames)) {
1912 *driver_release_tids |=
1913 sta->driver_buffered_tids & tids;
1914 *driver_release_tids |= sta->txq_buffered_tids & tids;
1917 if (!*driver_release_tids) {
1918 struct sk_buff *skb;
1920 while (n_frames > 0) {
1921 skb = skb_dequeue(&sta->tx_filtered[ac]);
1924 &sta->ps_tx_buf[ac]);
1926 local->total_ps_buffered--;
1931 __skb_queue_tail(frames, skb);
1935 /* If we have more frames buffered on this AC, then abort the
1936 * loop since we can't send more data from other ACs before
1937 * the buffered frames from this.
1939 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1940 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1946 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1947 int n_frames, u8 ignored_acs,
1948 enum ieee80211_frame_release_type reason)
1950 struct ieee80211_sub_if_data *sdata = sta->sdata;
1951 struct ieee80211_local *local = sdata->local;
1952 unsigned long driver_release_tids = 0;
1953 struct sk_buff_head frames;
1956 /* Service or PS-Poll period starts */
1957 set_sta_flag(sta, WLAN_STA_SP);
1959 __skb_queue_head_init(&frames);
1961 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1962 &frames, &driver_release_tids);
1964 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1966 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1967 driver_release_tids =
1968 BIT(find_highest_prio_tid(driver_release_tids));
1970 if (skb_queue_empty(&frames) && !driver_release_tids) {
1974 * For PS-Poll, this can only happen due to a race condition
1975 * when we set the TIM bit and the station notices it, but
1976 * before it can poll for the frame we expire it.
1978 * For uAPSD, this is said in the standard (11.2.1.5 h):
1979 * At each unscheduled SP for a non-AP STA, the AP shall
1980 * attempt to transmit at least one MSDU or MMPDU, but no
1981 * more than the value specified in the Max SP Length field
1982 * in the QoS Capability element from delivery-enabled ACs,
1983 * that are destined for the non-AP STA.
1985 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1988 /* This will evaluate to 1, 3, 5 or 7. */
1989 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1990 if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1994 ieee80211_send_null_response(sta, tid, reason, true, false);
1995 } else if (!driver_release_tids) {
1996 struct sk_buff_head pending;
1997 struct sk_buff *skb;
2000 bool need_null = false;
2002 skb_queue_head_init(&pending);
2004 while ((skb = __skb_dequeue(&frames))) {
2005 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2006 struct ieee80211_hdr *hdr = (void *) skb->data;
2012 * Tell TX path to send this frame even though the
2013 * STA may still remain is PS mode after this frame
2016 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
2017 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
2020 * Use MoreData flag to indicate whether there are
2021 * more buffered frames for this STA
2023 if (more_data || !skb_queue_empty(&frames))
2024 hdr->frame_control |=
2025 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2027 hdr->frame_control &=
2028 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
2030 if (ieee80211_is_data_qos(hdr->frame_control) ||
2031 ieee80211_is_qos_nullfunc(hdr->frame_control))
2032 qoshdr = ieee80211_get_qos_ctl(hdr);
2034 tids |= BIT(skb->priority);
2036 __skb_queue_tail(&pending, skb);
2038 /* end service period after last frame or add one */
2039 if (!skb_queue_empty(&frames))
2042 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
2043 /* for PS-Poll, there's only one frame */
2044 info->flags |= IEEE80211_TX_STATUS_EOSP |
2045 IEEE80211_TX_CTL_REQ_TX_STATUS;
2049 /* For uAPSD, things are a bit more complicated. If the
2050 * last frame has a QoS header (i.e. is a QoS-data or
2051 * QoS-nulldata frame) then just set the EOSP bit there
2053 * If the frame doesn't have a QoS header (which means
2054 * it should be a bufferable MMPDU) then we can't set
2055 * the EOSP bit in the QoS header; add a QoS-nulldata
2056 * frame to the list to send it after the MMPDU.
2058 * Note that this code is only in the mac80211-release
2059 * code path, we assume that the driver will not buffer
2060 * anything but QoS-data frames, or if it does, will
2061 * create the QoS-nulldata frame by itself if needed.
2063 * Cf. 802.11-2012 10.2.1.10 (c).
2066 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
2068 info->flags |= IEEE80211_TX_STATUS_EOSP |
2069 IEEE80211_TX_CTL_REQ_TX_STATUS;
2071 /* The standard isn't completely clear on this
2072 * as it says the more-data bit should be set
2073 * if there are more BUs. The QoS-Null frame
2074 * we're about to send isn't buffered yet, we
2075 * only create it below, but let's pretend it
2076 * was buffered just in case some clients only
2077 * expect more-data=0 when eosp=1.
2079 hdr->frame_control |=
2080 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2087 drv_allow_buffered_frames(local, sta, tids, num,
2090 ieee80211_add_pending_skbs(local, &pending);
2093 ieee80211_send_null_response(
2094 sta, find_highest_prio_tid(tids),
2095 reason, false, false);
2097 sta_info_recalc_tim(sta);
2102 * We need to release a frame that is buffered somewhere in the
2103 * driver ... it'll have to handle that.
2104 * Note that the driver also has to check the number of frames
2105 * on the TIDs we're releasing from - if there are more than
2106 * n_frames it has to set the more-data bit (if we didn't ask
2107 * it to set it anyway due to other buffered frames); if there
2108 * are fewer than n_frames it has to make sure to adjust that
2109 * to allow the service period to end properly.
2111 drv_release_buffered_frames(local, sta, driver_release_tids,
2112 n_frames, reason, more_data);
2115 * Note that we don't recalculate the TIM bit here as it would
2116 * most likely have no effect at all unless the driver told us
2117 * that the TID(s) became empty before returning here from the
2119 * Either way, however, when the driver tells us that the TID(s)
2120 * became empty or we find that a txq became empty, we'll do the
2121 * TIM recalculation.
2124 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
2125 if (!sta->sta.txq[tid] ||
2126 !(driver_release_tids & BIT(tid)) ||
2127 txq_has_queue(sta->sta.txq[tid]))
2130 sta_info_recalc_tim(sta);
2136 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
2138 u8 ignore_for_response = sta->sta.uapsd_queues;
2141 * If all ACs are delivery-enabled then we should reply
2142 * from any of them, if only some are enabled we reply
2143 * only from the non-enabled ones.
2145 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
2146 ignore_for_response = 0;
2148 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
2149 IEEE80211_FRAME_RELEASE_PSPOLL);
2152 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
2154 int n_frames = sta->sta.max_sp;
2155 u8 delivery_enabled = sta->sta.uapsd_queues;
2158 * If we ever grow support for TSPEC this might happen if
2159 * the TSPEC update from hostapd comes in between a trigger
2160 * frame setting WLAN_STA_UAPSD in the RX path and this
2161 * actually getting called.
2163 if (!delivery_enabled)
2166 switch (sta->sta.max_sp) {
2177 /* XXX: what is a good value? */
2182 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
2183 IEEE80211_FRAME_RELEASE_UAPSD);
2186 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
2187 struct ieee80211_sta *pubsta, bool block)
2189 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2191 trace_api_sta_block_awake(sta->local, pubsta, block);
2194 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
2195 ieee80211_clear_fast_xmit(sta);
2199 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
2202 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
2203 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
2204 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2205 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2206 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
2207 test_sta_flag(sta, WLAN_STA_UAPSD)) {
2208 /* must be asleep in this case */
2209 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2210 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2212 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2213 ieee80211_check_fast_xmit(sta);
2216 EXPORT_SYMBOL(ieee80211_sta_block_awake);
2218 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
2220 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2221 struct ieee80211_local *local = sta->local;
2223 trace_api_eosp(local, pubsta);
2225 clear_sta_flag(sta, WLAN_STA_SP);
2227 EXPORT_SYMBOL(ieee80211_sta_eosp);
2229 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
2231 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2232 enum ieee80211_frame_release_type reason;
2235 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
2237 reason = IEEE80211_FRAME_RELEASE_UAPSD;
2238 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
2241 ieee80211_send_null_response(sta, tid, reason, false, more_data);
2243 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
2245 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2246 u8 tid, bool buffered)
2248 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2250 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2253 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
2256 set_bit(tid, &sta->driver_buffered_tids);
2258 clear_bit(tid, &sta->driver_buffered_tids);
2260 sta_info_recalc_tim(sta);
2262 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2264 void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2265 u32 tx_airtime, u32 rx_airtime)
2267 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2268 struct ieee80211_local *local = sta->sdata->local;
2269 u8 ac = ieee80211_ac_from_tid(tid);
2273 if (sta->local->airtime_flags & AIRTIME_USE_TX)
2274 airtime += tx_airtime;
2275 if (sta->local->airtime_flags & AIRTIME_USE_RX)
2276 airtime += rx_airtime;
2278 spin_lock_bh(&local->active_txq_lock[ac]);
2279 sta->airtime[ac].tx_airtime += tx_airtime;
2280 sta->airtime[ac].rx_airtime += rx_airtime;
2282 diff = (u32)jiffies - sta->airtime[ac].last_active;
2283 if (diff <= AIRTIME_ACTIVE_DURATION)
2284 sta->airtime[ac].deficit -= airtime;
2286 spin_unlock_bh(&local->active_txq_lock[ac]);
2288 EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2290 void __ieee80211_sta_recalc_aggregates(struct sta_info *sta, u16 active_links)
2295 if (!sta->sta.valid_links || !sta->sta.mlo) {
2296 sta->sta.cur = &sta->sta.deflink.agg;
2301 for (link_id = 0; link_id < ARRAY_SIZE((sta)->link); link_id++) {
2302 struct ieee80211_link_sta *link_sta;
2305 if (!(active_links & BIT(link_id)))
2308 link_sta = rcu_dereference(sta->sta.link[link_id]);
2313 sta->cur = sta->sta.deflink.agg;
2318 sta->cur.max_amsdu_len =
2319 min(sta->cur.max_amsdu_len,
2320 link_sta->agg.max_amsdu_len);
2321 sta->cur.max_rc_amsdu_len =
2322 min(sta->cur.max_rc_amsdu_len,
2323 link_sta->agg.max_rc_amsdu_len);
2325 for (i = 0; i < ARRAY_SIZE(sta->cur.max_tid_amsdu_len); i++)
2326 sta->cur.max_tid_amsdu_len[i] =
2327 min(sta->cur.max_tid_amsdu_len[i],
2328 link_sta->agg.max_tid_amsdu_len[i]);
2332 sta->sta.cur = &sta->cur;
2335 void ieee80211_sta_recalc_aggregates(struct ieee80211_sta *pubsta)
2337 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2339 __ieee80211_sta_recalc_aggregates(sta, sta->sdata->vif.active_links);
2341 EXPORT_SYMBOL(ieee80211_sta_recalc_aggregates);
2343 void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
2344 struct sta_info *sta, u8 ac,
2345 u16 tx_airtime, bool tx_completed)
2349 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2352 if (!tx_completed) {
2354 atomic_add(tx_airtime,
2355 &sta->airtime[ac].aql_tx_pending);
2357 atomic_add(tx_airtime, &local->aql_total_pending_airtime);
2358 atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
2363 tx_pending = atomic_sub_return(tx_airtime,
2364 &sta->airtime[ac].aql_tx_pending);
2366 atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
2370 atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
2371 tx_pending = atomic_sub_return(tx_airtime,
2372 &local->aql_ac_pending_airtime[ac]);
2373 if (WARN_ONCE(tx_pending < 0,
2374 "Device %s AC %d pending airtime underflow: %u, %u",
2375 wiphy_name(local->hw.wiphy), ac, tx_pending,
2377 atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
2379 atomic_sub(tx_pending, &local->aql_total_pending_airtime);
2383 static struct ieee80211_sta_rx_stats *
2384 sta_get_last_rx_stats(struct sta_info *sta)
2386 struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2389 if (!sta->deflink.pcpu_rx_stats)
2392 for_each_possible_cpu(cpu) {
2393 struct ieee80211_sta_rx_stats *cpustats;
2395 cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2397 if (time_after(cpustats->last_rx, stats->last_rx))
2404 static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
2405 struct rate_info *rinfo)
2407 rinfo->bw = STA_STATS_GET(BW, rate);
2409 switch (STA_STATS_GET(TYPE, rate)) {
2410 case STA_STATS_RATE_TYPE_VHT:
2411 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2412 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2413 rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2414 if (STA_STATS_GET(SGI, rate))
2415 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2417 case STA_STATS_RATE_TYPE_HT:
2418 rinfo->flags = RATE_INFO_FLAGS_MCS;
2419 rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2420 if (STA_STATS_GET(SGI, rate))
2421 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2423 case STA_STATS_RATE_TYPE_LEGACY: {
2424 struct ieee80211_supported_band *sband;
2427 int band = STA_STATS_GET(LEGACY_BAND, rate);
2428 int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2430 sband = local->hw.wiphy->bands[band];
2432 if (WARN_ON_ONCE(!sband->bitrates))
2435 brate = sband->bitrates[rate_idx].bitrate;
2436 if (rinfo->bw == RATE_INFO_BW_5)
2438 else if (rinfo->bw == RATE_INFO_BW_10)
2442 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
2445 case STA_STATS_RATE_TYPE_HE:
2446 rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
2447 rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
2448 rinfo->nss = STA_STATS_GET(HE_NSS, rate);
2449 rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
2450 rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
2451 rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
2453 case STA_STATS_RATE_TYPE_EHT:
2454 rinfo->flags = RATE_INFO_FLAGS_EHT_MCS;
2455 rinfo->mcs = STA_STATS_GET(EHT_MCS, rate);
2456 rinfo->nss = STA_STATS_GET(EHT_NSS, rate);
2457 rinfo->eht_gi = STA_STATS_GET(EHT_GI, rate);
2458 rinfo->eht_ru_alloc = STA_STATS_GET(EHT_RU, rate);
2463 static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
2465 u32 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
2467 if (rate == STA_STATS_RATE_INVALID)
2470 sta_stats_decode_rate(sta->local, rate, rinfo);
2474 static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2481 start = u64_stats_fetch_begin(&rxstats->syncp);
2482 value = rxstats->msdu[tid];
2483 } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2488 static void sta_set_tidstats(struct sta_info *sta,
2489 struct cfg80211_tid_stats *tidstats,
2492 struct ieee80211_local *local = sta->local;
2495 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2496 tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2499 if (sta->deflink.pcpu_rx_stats) {
2500 for_each_possible_cpu(cpu) {
2501 struct ieee80211_sta_rx_stats *cpurxs;
2503 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2505 tidstats->rx_msdu +=
2506 sta_get_tidstats_msdu(cpurxs, tid);
2510 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2513 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2514 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2515 tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
2518 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2519 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2520 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2521 tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
2524 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2525 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2526 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2527 tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
2530 if (tid < IEEE80211_NUM_TIDS) {
2531 spin_lock_bh(&local->fq.lock);
2534 tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
2535 ieee80211_fill_txq_stats(&tidstats->txq_stats,
2536 to_txq_info(sta->sta.txq[tid]));
2539 spin_unlock_bh(&local->fq.lock);
2543 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2549 start = u64_stats_fetch_begin(&rxstats->syncp);
2550 value = rxstats->bytes;
2551 } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2556 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
2559 struct ieee80211_sub_if_data *sdata = sta->sdata;
2560 struct ieee80211_local *local = sdata->local;
2563 struct ieee80211_sta_rx_stats *last_rxstats;
2565 last_rxstats = sta_get_last_rx_stats(sta);
2567 sinfo->generation = sdata->local->sta_generation;
2569 /* do before driver, so beacon filtering drivers have a
2570 * chance to e.g. just add the number of filtered beacons
2571 * (or just modify the value entirely, of course)
2573 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2574 sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2576 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2577 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2578 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2579 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2580 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
2581 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2582 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2584 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2585 sinfo->beacon_loss_count =
2586 sdata->deflink.u.mgd.beacon_loss_count;
2587 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2590 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2591 sinfo->assoc_at = sta->assoc_at;
2592 sinfo->inactive_time =
2593 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2595 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2596 BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2597 sinfo->tx_bytes = 0;
2598 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2599 sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2600 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2603 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
2604 sinfo->tx_packets = 0;
2605 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2606 sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2607 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
2610 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2611 BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2612 sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
2614 if (sta->deflink.pcpu_rx_stats) {
2615 for_each_possible_cpu(cpu) {
2616 struct ieee80211_sta_rx_stats *cpurxs;
2618 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2620 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2624 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
2627 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2628 sinfo->rx_packets = sta->deflink.rx_stats.packets;
2629 if (sta->deflink.pcpu_rx_stats) {
2630 for_each_possible_cpu(cpu) {
2631 struct ieee80211_sta_rx_stats *cpurxs;
2633 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2635 sinfo->rx_packets += cpurxs->packets;
2638 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
2641 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2642 sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2643 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
2646 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2647 sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2648 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
2651 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2652 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2653 sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2654 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2657 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2658 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2659 sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2660 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2663 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2664 sinfo->airtime_weight = sta->airtime_weight;
2665 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2668 sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2669 if (sta->deflink.pcpu_rx_stats) {
2670 for_each_possible_cpu(cpu) {
2671 struct ieee80211_sta_rx_stats *cpurxs;
2673 cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2674 sinfo->rx_dropped_misc += cpurxs->dropped;
2678 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2679 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2680 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2681 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2682 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2685 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2686 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2687 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2688 sinfo->signal = (s8)last_rxstats->last_signal;
2689 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2692 if (!sta->deflink.pcpu_rx_stats &&
2693 !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
2695 -ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2696 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
2700 /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2701 * the sta->rx_stats struct, so the check here is fine with and without
2704 if (last_rxstats->chains &&
2705 !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2706 BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2707 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2708 if (!sta->deflink.pcpu_rx_stats)
2709 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2711 sinfo->chains = last_rxstats->chains;
2713 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2714 sinfo->chain_signal[i] =
2715 last_rxstats->chain_signal_last[i];
2716 sinfo->chain_signal_avg[i] =
2717 -ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2721 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
2722 !sta->sta.valid_links &&
2723 ieee80211_rate_valid(&sta->deflink.tx_stats.last_rate)) {
2724 sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2726 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2729 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
2730 !sta->sta.valid_links) {
2731 if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2732 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
2735 if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
2736 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
2737 sta_set_tidstats(sta, &sinfo->pertid[i], i);
2740 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2741 #ifdef CONFIG_MAC80211_MESH
2742 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2743 BIT_ULL(NL80211_STA_INFO_PLID) |
2744 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2745 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2746 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2747 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
2748 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
2749 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2751 sinfo->llid = sta->mesh->llid;
2752 sinfo->plid = sta->mesh->plid;
2753 sinfo->plink_state = sta->mesh->plink_state;
2754 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2755 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2756 sinfo->t_offset = sta->mesh->t_offset;
2758 sinfo->local_pm = sta->mesh->local_pm;
2759 sinfo->peer_pm = sta->mesh->peer_pm;
2760 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2761 sinfo->connected_to_gate = sta->mesh->connected_to_gate;
2762 sinfo->connected_to_as = sta->mesh->connected_to_as;
2766 sinfo->bss_param.flags = 0;
2767 if (sdata->vif.bss_conf.use_cts_prot)
2768 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2769 if (sdata->vif.bss_conf.use_short_preamble)
2770 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2771 if (sdata->vif.bss_conf.use_short_slot)
2772 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2773 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2774 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2776 sinfo->sta_flags.set = 0;
2777 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2778 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2779 BIT(NL80211_STA_FLAG_WME) |
2780 BIT(NL80211_STA_FLAG_MFP) |
2781 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2782 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2783 BIT(NL80211_STA_FLAG_TDLS_PEER);
2784 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2785 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2786 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2787 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2789 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2790 if (test_sta_flag(sta, WLAN_STA_MFP))
2791 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2792 if (test_sta_flag(sta, WLAN_STA_AUTH))
2793 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2794 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2795 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2796 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2797 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2799 thr = sta_get_expected_throughput(sta);
2802 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2803 sinfo->expected_throughput = thr;
2806 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2807 sta->deflink.status_stats.ack_signal_filled) {
2808 sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2809 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2812 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2813 sta->deflink.status_stats.ack_signal_filled) {
2814 sinfo->avg_ack_signal =
2815 -(s8)ewma_avg_signal_read(
2816 &sta->deflink.status_stats.avg_ack_signal);
2818 BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2821 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2822 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2823 sinfo->airtime_link_metric =
2824 airtime_link_metric_get(local, sta);
2828 u32 sta_get_expected_throughput(struct sta_info *sta)
2830 struct ieee80211_sub_if_data *sdata = sta->sdata;
2831 struct ieee80211_local *local = sdata->local;
2832 struct rate_control_ref *ref = NULL;
2835 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2836 ref = local->rate_ctrl;
2838 /* check if the driver has a SW RC implementation */
2839 if (ref && ref->ops->get_expected_throughput)
2840 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2842 thr = drv_get_expected_throughput(local, sta);
2847 unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2849 struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2851 if (!sta->deflink.status_stats.last_ack ||
2852 time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2853 return stats->last_rx;
2854 return sta->deflink.status_stats.last_ack;
2857 static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2859 if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2860 sta->cparams.target = MS2TIME(50);
2861 sta->cparams.interval = MS2TIME(300);
2862 sta->cparams.ecn = false;
2864 sta->cparams.target = MS2TIME(20);
2865 sta->cparams.interval = MS2TIME(100);
2866 sta->cparams.ecn = true;
2870 void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2873 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2875 sta_update_codel_params(sta, thr);
2878 int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2880 struct ieee80211_sub_if_data *sdata = sta->sdata;
2881 struct sta_link_alloc *alloc;
2884 lockdep_assert_wiphy(sdata->local->hw.wiphy);
2886 WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED));
2888 /* must represent an MLD from the start */
2889 if (WARN_ON(!sta->sta.valid_links))
2892 if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2893 sta->link[link_id]))
2896 alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2900 ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2906 sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2908 ieee80211_link_sta_debugfs_add(&alloc->info);
2913 void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
2915 lockdep_assert_wiphy(sta->sdata->local->hw.wiphy);
2917 WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED));
2919 sta_remove_link(sta, link_id, false);
2922 int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2924 struct ieee80211_sub_if_data *sdata = sta->sdata;
2925 struct link_sta_info *link_sta;
2926 u16 old_links = sta->sta.valid_links;
2927 u16 new_links = old_links | BIT(link_id);
2930 link_sta = rcu_dereference_protected(sta->link[link_id],
2931 lockdep_is_held(&sdata->local->hw.wiphy->mtx));
2933 if (WARN_ON(old_links == new_links || !link_sta))
2937 if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) {
2941 /* we only modify under the mutex so this is fine */
2944 sta->sta.valid_links = new_links;
2946 if (WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED)))
2949 ieee80211_recalc_min_chandef(sdata, link_id);
2951 /* Ensure the values are updated for the driver,
2952 * redone by sta_remove_link on failure.
2954 ieee80211_sta_recalc_aggregates(&sta->sta);
2956 ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2957 old_links, new_links);
2959 sta->sta.valid_links = old_links;
2960 sta_remove_link(sta, link_id, false);
2965 ret = link_sta_info_hash_add(sdata->local, link_sta);
2970 void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2972 struct ieee80211_sub_if_data *sdata = sta->sdata;
2973 u16 old_links = sta->sta.valid_links;
2975 lockdep_assert_wiphy(sdata->local->hw.wiphy);
2977 sta->sta.valid_links &= ~BIT(link_id);
2979 if (!WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED)))
2980 drv_change_sta_links(sdata->local, sdata, &sta->sta,
2981 old_links, sta->sta.valid_links);
2983 sta_remove_link(sta, link_id, true);
2986 void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
2987 const u8 *ext_capab,
2988 unsigned int ext_capab_len)
2992 sta->sta.max_amsdu_subframes = 0;
2994 if (ext_capab_len < 8)
2997 /* The sender might not have sent the last bit, consider it to be 0 */
2998 val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
3000 /* we did get all the bits, take the MSB as well */
3001 if (ext_capab_len >= 9)
3002 val |= u8_get_bits(ext_capab[8],
3003 WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
3006 sta->sta.max_amsdu_subframes = 4 << (4 - val);
3009 #ifdef CONFIG_LOCKDEP
3010 bool lockdep_sta_mutex_held(struct ieee80211_sta *pubsta)
3012 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
3014 return lockdep_is_held(&sta->local->hw.wiphy->mtx);
3016 EXPORT_SYMBOL(lockdep_sta_mutex_held);