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-2021 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 static const struct rhashtable_params sta_rht_params = {
68 .nelem_hint = 3, /* start small */
69 .automatic_shrinking = true,
70 .head_offset = offsetof(struct sta_info, hash_node),
71 .key_offset = offsetof(struct sta_info, addr),
73 .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
76 /* Caller must hold local->sta_mtx */
77 static int sta_info_hash_del(struct ieee80211_local *local,
80 return rhltable_remove(&local->sta_hash, &sta->hash_node,
84 static void __cleanup_single_sta(struct sta_info *sta)
87 struct tid_ampdu_tx *tid_tx;
88 struct ieee80211_sub_if_data *sdata = sta->sdata;
89 struct ieee80211_local *local = sdata->local;
92 if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
93 test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
94 test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
95 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
96 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
98 else if (ieee80211_vif_is_mesh(&sdata->vif))
99 ps = &sdata->u.mesh.ps;
103 clear_sta_flag(sta, WLAN_STA_PS_STA);
104 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
105 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
107 atomic_dec(&ps->num_sta_ps);
110 if (sta->sta.txq[0]) {
111 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
112 struct txq_info *txqi;
114 if (!sta->sta.txq[i])
117 txqi = to_txq_info(sta->sta.txq[i]);
119 ieee80211_txq_purge(local, txqi);
123 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
124 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
125 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
126 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
129 if (ieee80211_vif_is_mesh(&sdata->vif))
130 mesh_sta_cleanup(sta);
132 cancel_work_sync(&sta->drv_deliver_wk);
135 * Destroy aggregation state here. It would be nice to wait for the
136 * driver to finish aggregation stop and then clean up, but for now
137 * drivers have to handle aggregation stop being requested, followed
138 * directly by station destruction.
140 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
141 kfree(sta->ampdu_mlme.tid_start_tx[i]);
142 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
145 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
150 static void cleanup_single_sta(struct sta_info *sta)
152 struct ieee80211_sub_if_data *sdata = sta->sdata;
153 struct ieee80211_local *local = sdata->local;
155 __cleanup_single_sta(sta);
156 sta_info_free(local, sta);
159 struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
162 return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
165 /* protected by RCU */
166 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
169 struct ieee80211_local *local = sdata->local;
170 struct rhlist_head *tmp;
171 struct sta_info *sta;
174 for_each_sta_info(local, addr, sta, tmp) {
175 if (sta->sdata == sdata) {
177 /* this is safe as the caller must already hold
178 * another rcu read section or the mutex
188 * Get sta info either from the specified interface
189 * or from one of its vlans
191 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
194 struct ieee80211_local *local = sdata->local;
195 struct rhlist_head *tmp;
196 struct sta_info *sta;
199 for_each_sta_info(local, addr, sta, tmp) {
200 if (sta->sdata == sdata ||
201 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
203 /* this is safe as the caller must already hold
204 * another rcu read section or the mutex
213 struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
214 const u8 *sta_addr, const u8 *vif_addr)
216 struct rhlist_head *tmp;
217 struct sta_info *sta;
219 for_each_sta_info(local, sta_addr, sta, tmp) {
220 if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
227 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
230 struct ieee80211_local *local = sdata->local;
231 struct sta_info *sta;
234 list_for_each_entry_rcu(sta, &local->sta_list, list,
235 lockdep_is_held(&local->sta_mtx)) {
236 if (sdata != sta->sdata)
249 * sta_info_free - free STA
251 * @local: pointer to the global information
252 * @sta: STA info to free
254 * This function must undo everything done by sta_info_alloc()
255 * that may happen before sta_info_insert(). It may only be
256 * called when sta_info_insert() has not been attempted (and
257 * if that fails, the station is freed anyway.)
259 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
262 * If we had used sta_info_pre_move_state() then we might not
263 * have gone through the state transitions down again, so do
264 * it here now (and warn if it's inserted).
266 * This will clear state such as fast TX/RX that may have been
267 * allocated during state transitions.
269 while (sta->sta_state > IEEE80211_STA_NONE) {
272 WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
274 ret = sta_info_move_state(sta, sta->sta_state - 1);
275 if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
280 rate_control_free_sta(sta);
282 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
285 kfree(to_txq_info(sta->sta.txq[0]));
286 kfree(rcu_dereference_raw(sta->sta.rates));
287 #ifdef CONFIG_MAC80211_MESH
290 free_percpu(sta->pcpu_rx_stats);
294 /* Caller must hold local->sta_mtx */
295 static int sta_info_hash_add(struct ieee80211_local *local,
296 struct sta_info *sta)
298 return rhltable_insert(&local->sta_hash, &sta->hash_node,
302 static void sta_deliver_ps_frames(struct work_struct *wk)
304 struct sta_info *sta;
306 sta = container_of(wk, struct sta_info, drv_deliver_wk);
312 if (!test_sta_flag(sta, WLAN_STA_PS_STA))
313 ieee80211_sta_ps_deliver_wakeup(sta);
314 else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
315 ieee80211_sta_ps_deliver_poll_response(sta);
316 else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
317 ieee80211_sta_ps_deliver_uapsd(sta);
321 static int sta_prepare_rate_control(struct ieee80211_local *local,
322 struct sta_info *sta, gfp_t gfp)
324 if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
327 sta->rate_ctrl = local->rate_ctrl;
328 sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
330 if (!sta->rate_ctrl_priv)
336 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
337 const u8 *addr, gfp_t gfp)
339 struct ieee80211_local *local = sdata->local;
340 struct ieee80211_hw *hw = &local->hw;
341 struct sta_info *sta;
344 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
348 if (ieee80211_hw_check(hw, USES_RSS)) {
350 alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
351 if (!sta->pcpu_rx_stats)
355 spin_lock_init(&sta->lock);
356 spin_lock_init(&sta->ps_lock);
357 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
358 INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
359 mutex_init(&sta->ampdu_mlme.mtx);
360 #ifdef CONFIG_MAC80211_MESH
361 if (ieee80211_vif_is_mesh(&sdata->vif)) {
362 sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
365 sta->mesh->plink_sta = sta;
366 spin_lock_init(&sta->mesh->plink_lock);
367 if (ieee80211_vif_is_mesh(&sdata->vif) &&
368 !sdata->u.mesh.user_mpm)
369 timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
371 sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
375 memcpy(sta->addr, addr, ETH_ALEN);
376 memcpy(sta->sta.addr, addr, ETH_ALEN);
377 sta->sta.max_rx_aggregation_subframes =
378 local->hw.max_rx_aggregation_subframes;
380 /* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
381 * The Tx path starts to use a key as soon as the key slot ptk_idx
382 * references to is not NULL. To not use the initial Rx-only key
383 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
384 * which always will refer to a NULL key.
386 BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
387 sta->ptk_idx = INVALID_PTK_KEYIDX;
391 sta->rx_stats.last_rx = jiffies;
393 u64_stats_init(&sta->rx_stats.syncp);
395 ieee80211_init_frag_cache(&sta->frags);
397 sta->sta_state = IEEE80211_STA_NONE;
399 /* Mark TID as unreserved */
400 sta->reserved_tid = IEEE80211_TID_UNRESERVED;
402 sta->last_connected = ktime_get_seconds();
403 ewma_signal_init(&sta->rx_stats_avg.signal);
404 ewma_avg_signal_init(&sta->status_stats.avg_ack_signal);
405 for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
406 ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
408 if (local->ops->wake_tx_queue) {
410 int size = sizeof(struct txq_info) +
411 ALIGN(hw->txq_data_size, sizeof(void *));
413 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
417 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
418 struct txq_info *txq = txq_data + i * size;
420 /* might not do anything for the bufferable MMPDU TXQ */
421 ieee80211_txq_init(sdata, sta, txq, i);
425 if (sta_prepare_rate_control(local, sta, gfp))
429 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
430 skb_queue_head_init(&sta->ps_tx_buf[i]);
431 skb_queue_head_init(&sta->tx_filtered[i]);
432 init_airtime_info(&sta->airtime[i], &local->airtime[i]);
435 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
436 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
438 for (i = 0; i < NUM_NL80211_BANDS; i++) {
442 if (!hw->wiphy->bands[i])
446 case NL80211_BAND_2GHZ:
447 case NL80211_BAND_LC:
449 * We use both here, even if we cannot really know for
450 * sure the station will support both, but the only use
451 * for this is when we don't know anything yet and send
452 * management frames, and then we'll pick the lowest
453 * possible rate anyway.
454 * If we don't include _G here, we cannot find a rate
455 * in P2P, and thus trigger the WARN_ONCE() in rate.c
457 mandatory = IEEE80211_RATE_MANDATORY_B |
458 IEEE80211_RATE_MANDATORY_G;
460 case NL80211_BAND_5GHZ:
461 mandatory = IEEE80211_RATE_MANDATORY_A;
463 case NL80211_BAND_60GHZ:
469 for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
470 struct ieee80211_rate *rate;
472 rate = &hw->wiphy->bands[i]->bitrates[r];
474 if (!(rate->flags & mandatory))
476 sta->sta.supp_rates[i] |= BIT(r);
480 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
481 if (sdata->vif.type == NL80211_IFTYPE_AP ||
482 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
483 struct ieee80211_supported_band *sband;
486 sband = ieee80211_get_sband(sdata);
490 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
491 IEEE80211_HT_CAP_SM_PS_SHIFT;
493 * Assume that hostapd advertises our caps in the beacon and
494 * this is the known_smps_mode for a station that just assciated
497 case WLAN_HT_SMPS_CONTROL_DISABLED:
498 sta->known_smps_mode = IEEE80211_SMPS_OFF;
500 case WLAN_HT_SMPS_CONTROL_STATIC:
501 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
503 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
504 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
511 sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
513 sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
514 sta->cparams.target = MS2TIME(20);
515 sta->cparams.interval = MS2TIME(100);
516 sta->cparams.ecn = true;
517 sta->cparams.ce_threshold_selector = 0;
518 sta->cparams.ce_threshold_mask = 0;
520 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
526 kfree(to_txq_info(sta->sta.txq[0]));
528 free_percpu(sta->pcpu_rx_stats);
529 #ifdef CONFIG_MAC80211_MESH
536 static int sta_info_insert_check(struct sta_info *sta)
538 struct ieee80211_sub_if_data *sdata = sta->sdata;
541 * Can't be a WARN_ON because it can be triggered through a race:
542 * something inserts a STA (on one CPU) without holding the RTNL
543 * and another CPU turns off the net device.
545 if (unlikely(!ieee80211_sdata_running(sdata)))
548 if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
549 !is_valid_ether_addr(sta->sta.addr)))
552 /* The RCU read lock is required by rhashtable due to
553 * asynchronous resize/rehash. We also require the mutex
557 lockdep_assert_held(&sdata->local->sta_mtx);
558 if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
559 ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
568 static int sta_info_insert_drv_state(struct ieee80211_local *local,
569 struct ieee80211_sub_if_data *sdata,
570 struct sta_info *sta)
572 enum ieee80211_sta_state state;
575 for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
576 err = drv_sta_state(local, sdata, sta, state, state + 1);
583 * Drivers using legacy sta_add/sta_remove callbacks only
584 * get uploaded set to true after sta_add is called.
586 if (!local->ops->sta_add)
587 sta->uploaded = true;
591 if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
593 "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
594 sta->sta.addr, state + 1, err);
598 /* unwind on error */
599 for (; state > IEEE80211_STA_NOTEXIST; state--)
600 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
606 ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
608 struct ieee80211_local *local = sdata->local;
609 bool allow_p2p_go_ps = sdata->vif.p2p;
610 struct sta_info *sta;
613 list_for_each_entry_rcu(sta, &local->sta_list, list) {
614 if (sdata != sta->sdata ||
615 !test_sta_flag(sta, WLAN_STA_ASSOC))
617 if (!sta->sta.support_p2p_ps) {
618 allow_p2p_go_ps = false;
624 if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
625 sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
626 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
631 * should be called with sta_mtx locked
632 * this function replaces the mutex lock
635 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
637 struct ieee80211_local *local = sta->local;
638 struct ieee80211_sub_if_data *sdata = sta->sdata;
639 struct station_info *sinfo = NULL;
642 lockdep_assert_held(&local->sta_mtx);
644 /* check if STA exists already */
645 if (sta_info_get_bss(sdata, sta->sta.addr)) {
650 sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
657 local->sta_generation++;
660 /* simplify things and don't accept BA sessions yet */
661 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
663 /* make the station visible */
664 err = sta_info_hash_add(local, sta);
668 list_add_tail_rcu(&sta->list, &local->sta_list);
671 err = sta_info_insert_drv_state(local, sdata, sta);
675 set_sta_flag(sta, WLAN_STA_INSERTED);
677 if (sta->sta_state >= IEEE80211_STA_ASSOC) {
678 ieee80211_recalc_min_chandef(sta->sdata);
679 if (!sta->sta.support_p2p_ps)
680 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
683 /* accept BA sessions now */
684 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
686 ieee80211_sta_debugfs_add(sta);
687 rate_control_add_sta_debugfs(sta);
689 sinfo->generation = local->sta_generation;
690 cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
693 sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
695 /* move reference to rcu-protected */
697 mutex_unlock(&local->sta_mtx);
699 if (ieee80211_vif_is_mesh(&sdata->vif))
700 mesh_accept_plinks_update(sdata);
704 sta_info_hash_del(local, sta);
705 list_del_rcu(&sta->list);
709 cleanup_single_sta(sta);
711 mutex_unlock(&local->sta_mtx);
717 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
719 struct ieee80211_local *local = sta->local;
724 mutex_lock(&local->sta_mtx);
726 err = sta_info_insert_check(sta);
728 sta_info_free(local, sta);
729 mutex_unlock(&local->sta_mtx);
734 return sta_info_insert_finish(sta);
737 int sta_info_insert(struct sta_info *sta)
739 int err = sta_info_insert_rcu(sta);
746 static inline void __bss_tim_set(u8 *tim, u16 id)
749 * This format has been mandated by the IEEE specifications,
750 * so this line may not be changed to use the __set_bit() format.
752 tim[id / 8] |= (1 << (id % 8));
755 static inline void __bss_tim_clear(u8 *tim, u16 id)
758 * This format has been mandated by the IEEE specifications,
759 * so this line may not be changed to use the __clear_bit() format.
761 tim[id / 8] &= ~(1 << (id % 8));
764 static inline bool __bss_tim_get(u8 *tim, u16 id)
767 * This format has been mandated by the IEEE specifications,
768 * so this line may not be changed to use the test_bit() format.
770 return tim[id / 8] & (1 << (id % 8));
773 static unsigned long ieee80211_tids_for_ac(int ac)
775 /* If we ever support TIDs > 7, this obviously needs to be adjusted */
777 case IEEE80211_AC_VO:
778 return BIT(6) | BIT(7);
779 case IEEE80211_AC_VI:
780 return BIT(4) | BIT(5);
781 case IEEE80211_AC_BE:
782 return BIT(0) | BIT(3);
783 case IEEE80211_AC_BK:
784 return BIT(1) | BIT(2);
791 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
793 struct ieee80211_local *local = sta->local;
795 bool indicate_tim = false;
796 u8 ignore_for_tim = sta->sta.uapsd_queues;
798 u16 id = sta->sta.aid;
800 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
801 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
802 if (WARN_ON_ONCE(!sta->sdata->bss))
805 ps = &sta->sdata->bss->ps;
806 #ifdef CONFIG_MAC80211_MESH
807 } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
808 ps = &sta->sdata->u.mesh.ps;
814 /* No need to do anything if the driver does all */
815 if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
822 * If all ACs are delivery-enabled then we should build
823 * the TIM bit for all ACs anyway; if only some are then
824 * we ignore those and build the TIM bit using only the
827 if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
831 ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
833 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
836 if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
839 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
840 !skb_queue_empty(&sta->ps_tx_buf[ac]);
844 tids = ieee80211_tids_for_ac(ac);
847 sta->driver_buffered_tids & tids;
849 sta->txq_buffered_tids & tids;
853 spin_lock_bh(&local->tim_lock);
855 if (indicate_tim == __bss_tim_get(ps->tim, id))
859 __bss_tim_set(ps->tim, id);
861 __bss_tim_clear(ps->tim, id);
863 if (local->ops->set_tim && !WARN_ON(sta->dead)) {
864 local->tim_in_locked_section = true;
865 drv_set_tim(local, &sta->sta, indicate_tim);
866 local->tim_in_locked_section = false;
870 spin_unlock_bh(&local->tim_lock);
873 void sta_info_recalc_tim(struct sta_info *sta)
875 __sta_info_recalc_tim(sta, false);
878 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
880 struct ieee80211_tx_info *info;
886 info = IEEE80211_SKB_CB(skb);
888 /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
889 timeout = (sta->listen_interval *
890 sta->sdata->vif.bss_conf.beacon_int *
892 if (timeout < STA_TX_BUFFER_EXPIRE)
893 timeout = STA_TX_BUFFER_EXPIRE;
894 return time_after(jiffies, info->control.jiffies + timeout);
898 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
899 struct sta_info *sta, int ac)
905 * First check for frames that should expire on the filtered
906 * queue. Frames here were rejected by the driver and are on
907 * a separate queue to avoid reordering with normal PS-buffered
908 * frames. They also aren't accounted for right now in the
909 * total_ps_buffered counter.
912 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
913 skb = skb_peek(&sta->tx_filtered[ac]);
914 if (sta_info_buffer_expired(sta, skb))
915 skb = __skb_dequeue(&sta->tx_filtered[ac]);
918 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
921 * Frames are queued in order, so if this one
922 * hasn't expired yet we can stop testing. If
923 * we actually reached the end of the queue we
924 * also need to stop, of course.
928 ieee80211_free_txskb(&local->hw, skb);
932 * Now also check the normal PS-buffered queue, this will
933 * only find something if the filtered queue was emptied
934 * since the filtered frames are all before the normal PS
938 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
939 skb = skb_peek(&sta->ps_tx_buf[ac]);
940 if (sta_info_buffer_expired(sta, skb))
941 skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
944 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
947 * frames are queued in order, so if this one
948 * hasn't expired yet (or we reached the end of
949 * the queue) we can stop testing
954 local->total_ps_buffered--;
955 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
957 ieee80211_free_txskb(&local->hw, skb);
961 * Finally, recalculate the TIM bit for this station -- it might
962 * now be clear because the station was too slow to retrieve its
965 sta_info_recalc_tim(sta);
968 * Return whether there are any frames still buffered, this is
969 * used to check whether the cleanup timer still needs to run,
970 * if there are no frames we don't need to rearm the timer.
972 return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
973 skb_queue_empty(&sta->tx_filtered[ac]));
976 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
977 struct sta_info *sta)
979 bool have_buffered = false;
982 /* This is only necessary for stations on BSS/MBSS interfaces */
983 if (!sta->sdata->bss &&
984 !ieee80211_vif_is_mesh(&sta->sdata->vif))
987 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
989 sta_info_cleanup_expire_buffered_ac(local, sta, ac);
991 return have_buffered;
994 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
996 struct ieee80211_local *local;
997 struct ieee80211_sub_if_data *sdata;
1008 lockdep_assert_held(&local->sta_mtx);
1011 * Before removing the station from the driver and
1012 * rate control, it might still start new aggregation
1013 * sessions -- block that to make sure the tear-down
1014 * will be sufficient.
1016 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1017 ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1020 * Before removing the station from the driver there might be pending
1021 * rx frames on RSS queues sent prior to the disassociation - wait for
1022 * all such frames to be processed.
1024 drv_sync_rx_queues(local, sta);
1026 ret = sta_info_hash_del(local, sta);
1031 * for TDLS peers, make sure to return to the base channel before
1034 if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1035 drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1036 clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1039 list_del_rcu(&sta->list);
1040 sta->removed = true;
1042 drv_sta_pre_rcu_remove(local, sta->sdata, sta);
1044 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1045 rcu_access_pointer(sdata->u.vlan.sta) == sta)
1046 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1051 static void __sta_info_destroy_part2(struct sta_info *sta)
1053 struct ieee80211_local *local = sta->local;
1054 struct ieee80211_sub_if_data *sdata = sta->sdata;
1055 struct station_info *sinfo;
1059 * NOTE: This assumes at least synchronize_net() was done
1060 * after _part1 and before _part2!
1064 lockdep_assert_held(&local->sta_mtx);
1066 if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1067 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1071 /* now keys can no longer be reached */
1072 ieee80211_free_sta_keys(local, sta);
1074 /* disable TIM bit - last chance to tell driver */
1075 __sta_info_recalc_tim(sta, true);
1080 local->sta_generation++;
1082 while (sta->sta_state > IEEE80211_STA_NONE) {
1083 ret = sta_info_move_state(sta, sta->sta_state - 1);
1090 if (sta->uploaded) {
1091 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1092 IEEE80211_STA_NOTEXIST);
1093 WARN_ON_ONCE(ret != 0);
1096 sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1098 sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
1100 sta_set_sinfo(sta, sinfo, true);
1101 cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
1104 ieee80211_sta_debugfs_remove(sta);
1106 ieee80211_destroy_frag_cache(&sta->frags);
1108 cleanup_single_sta(sta);
1111 int __must_check __sta_info_destroy(struct sta_info *sta)
1113 int err = __sta_info_destroy_part1(sta);
1120 __sta_info_destroy_part2(sta);
1125 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1127 struct sta_info *sta;
1130 mutex_lock(&sdata->local->sta_mtx);
1131 sta = sta_info_get(sdata, addr);
1132 ret = __sta_info_destroy(sta);
1133 mutex_unlock(&sdata->local->sta_mtx);
1138 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1141 struct sta_info *sta;
1144 mutex_lock(&sdata->local->sta_mtx);
1145 sta = sta_info_get_bss(sdata, addr);
1146 ret = __sta_info_destroy(sta);
1147 mutex_unlock(&sdata->local->sta_mtx);
1152 static void sta_info_cleanup(struct timer_list *t)
1154 struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1155 struct sta_info *sta;
1156 bool timer_needed = false;
1159 list_for_each_entry_rcu(sta, &local->sta_list, list)
1160 if (sta_info_cleanup_expire_buffered(local, sta))
1161 timer_needed = true;
1164 if (local->quiescing)
1170 mod_timer(&local->sta_cleanup,
1171 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1174 int sta_info_init(struct ieee80211_local *local)
1178 err = rhltable_init(&local->sta_hash, &sta_rht_params);
1182 spin_lock_init(&local->tim_lock);
1183 mutex_init(&local->sta_mtx);
1184 INIT_LIST_HEAD(&local->sta_list);
1186 timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
1190 void sta_info_stop(struct ieee80211_local *local)
1192 del_timer_sync(&local->sta_cleanup);
1193 rhltable_destroy(&local->sta_hash);
1197 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1199 struct ieee80211_local *local = sdata->local;
1200 struct sta_info *sta, *tmp;
1201 LIST_HEAD(free_list);
1206 WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1207 WARN_ON(vlans && !sdata->bss);
1209 mutex_lock(&local->sta_mtx);
1210 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1211 if (sdata == sta->sdata ||
1212 (vlans && sdata->bss == sta->sdata->bss)) {
1213 if (!WARN_ON(__sta_info_destroy_part1(sta)))
1214 list_add(&sta->free_list, &free_list);
1219 if (!list_empty(&free_list)) {
1221 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1222 __sta_info_destroy_part2(sta);
1224 mutex_unlock(&local->sta_mtx);
1229 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1230 unsigned long exp_time)
1232 struct ieee80211_local *local = sdata->local;
1233 struct sta_info *sta, *tmp;
1235 mutex_lock(&local->sta_mtx);
1237 list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1238 unsigned long last_active = ieee80211_sta_last_active(sta);
1240 if (sdata != sta->sdata)
1243 if (time_is_before_jiffies(last_active + exp_time)) {
1244 sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1247 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1248 test_sta_flag(sta, WLAN_STA_PS_STA))
1249 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1251 WARN_ON(__sta_info_destroy(sta));
1255 mutex_unlock(&local->sta_mtx);
1258 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1260 const u8 *localaddr)
1262 struct ieee80211_local *local = hw_to_local(hw);
1263 struct rhlist_head *tmp;
1264 struct sta_info *sta;
1267 * Just return a random station if localaddr is NULL
1268 * ... first in list.
1270 for_each_sta_info(local, addr, sta, tmp) {
1272 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1281 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1283 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1286 struct sta_info *sta;
1291 sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1300 EXPORT_SYMBOL(ieee80211_find_sta);
1302 /* powersave support code */
1303 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1305 struct ieee80211_sub_if_data *sdata = sta->sdata;
1306 struct ieee80211_local *local = sdata->local;
1307 struct sk_buff_head pending;
1308 int filtered = 0, buffered = 0, ac, i;
1309 unsigned long flags;
1312 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1313 sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1316 if (sdata->vif.type == NL80211_IFTYPE_AP)
1317 ps = &sdata->bss->ps;
1318 else if (ieee80211_vif_is_mesh(&sdata->vif))
1319 ps = &sdata->u.mesh.ps;
1323 clear_sta_flag(sta, WLAN_STA_SP);
1325 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1326 sta->driver_buffered_tids = 0;
1327 sta->txq_buffered_tids = 0;
1329 if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1330 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1332 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1333 if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1336 schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1339 skb_queue_head_init(&pending);
1341 /* sync with ieee80211_tx_h_unicast_ps_buf */
1342 spin_lock(&sta->ps_lock);
1343 /* Send all buffered frames to the station */
1344 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1345 int count = skb_queue_len(&pending), tmp;
1347 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1348 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1349 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1350 tmp = skb_queue_len(&pending);
1351 filtered += tmp - count;
1354 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1355 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1356 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1357 tmp = skb_queue_len(&pending);
1358 buffered += tmp - count;
1361 ieee80211_add_pending_skbs(local, &pending);
1363 /* now we're no longer in the deliver code */
1364 clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1366 /* The station might have polled and then woken up before we responded,
1367 * so clear these flags now to avoid them sticking around.
1369 clear_sta_flag(sta, WLAN_STA_PSPOLL);
1370 clear_sta_flag(sta, WLAN_STA_UAPSD);
1371 spin_unlock(&sta->ps_lock);
1373 atomic_dec(&ps->num_sta_ps);
1375 local->total_ps_buffered -= buffered;
1377 sta_info_recalc_tim(sta);
1380 "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1381 sta->sta.addr, sta->sta.aid, filtered, buffered);
1383 ieee80211_check_fast_xmit(sta);
1386 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1387 enum ieee80211_frame_release_type reason,
1388 bool call_driver, bool more_data)
1390 struct ieee80211_sub_if_data *sdata = sta->sdata;
1391 struct ieee80211_local *local = sdata->local;
1392 struct ieee80211_qos_hdr *nullfunc;
1393 struct sk_buff *skb;
1394 int size = sizeof(*nullfunc);
1396 bool qos = sta->sta.wme;
1397 struct ieee80211_tx_info *info;
1398 struct ieee80211_chanctx_conf *chanctx_conf;
1401 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1402 IEEE80211_STYPE_QOS_NULLFUNC |
1403 IEEE80211_FCTL_FROMDS);
1406 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1407 IEEE80211_STYPE_NULLFUNC |
1408 IEEE80211_FCTL_FROMDS);
1411 skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1415 skb_reserve(skb, local->hw.extra_tx_headroom);
1417 nullfunc = skb_put(skb, size);
1418 nullfunc->frame_control = fc;
1419 nullfunc->duration_id = 0;
1420 memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1421 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1422 memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1423 nullfunc->seq_ctrl = 0;
1425 skb->priority = tid;
1426 skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1428 nullfunc->qos_ctrl = cpu_to_le16(tid);
1430 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1431 nullfunc->qos_ctrl |=
1432 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1434 nullfunc->frame_control |=
1435 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1439 info = IEEE80211_SKB_CB(skb);
1442 * Tell TX path to send this frame even though the
1443 * STA may still remain is PS mode after this frame
1444 * exchange. Also set EOSP to indicate this packet
1445 * ends the poll/service period.
1447 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1448 IEEE80211_TX_STATUS_EOSP |
1449 IEEE80211_TX_CTL_REQ_TX_STATUS;
1451 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1454 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1457 skb->dev = sdata->dev;
1460 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1461 if (WARN_ON(!chanctx_conf)) {
1467 info->band = chanctx_conf->def.chan->band;
1468 ieee80211_xmit(sdata, sta, skb);
1472 static int find_highest_prio_tid(unsigned long tids)
1474 /* lower 3 TIDs aren't ordered perfectly */
1476 return fls(tids) - 1;
1477 /* TID 0 is BE just like TID 3 */
1480 return fls(tids) - 1;
1483 /* Indicates if the MORE_DATA bit should be set in the last
1484 * frame obtained by ieee80211_sta_ps_get_frames.
1485 * Note that driver_release_tids is relevant only if
1486 * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1489 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1490 enum ieee80211_frame_release_type reason,
1491 unsigned long driver_release_tids)
1495 /* If the driver has data on more than one TID then
1496 * certainly there's more data if we release just a
1497 * single frame now (from a single TID). This will
1498 * only happen for PS-Poll.
1500 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1501 hweight16(driver_release_tids) > 1)
1504 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1505 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1508 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1509 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1517 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1518 enum ieee80211_frame_release_type reason,
1519 struct sk_buff_head *frames,
1520 unsigned long *driver_release_tids)
1522 struct ieee80211_sub_if_data *sdata = sta->sdata;
1523 struct ieee80211_local *local = sdata->local;
1526 /* Get response frame(s) and more data bit for the last one. */
1527 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1530 if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1533 tids = ieee80211_tids_for_ac(ac);
1535 /* if we already have frames from software, then we can't also
1536 * release from hardware queues
1538 if (skb_queue_empty(frames)) {
1539 *driver_release_tids |=
1540 sta->driver_buffered_tids & tids;
1541 *driver_release_tids |= sta->txq_buffered_tids & tids;
1544 if (!*driver_release_tids) {
1545 struct sk_buff *skb;
1547 while (n_frames > 0) {
1548 skb = skb_dequeue(&sta->tx_filtered[ac]);
1551 &sta->ps_tx_buf[ac]);
1553 local->total_ps_buffered--;
1558 __skb_queue_tail(frames, skb);
1562 /* If we have more frames buffered on this AC, then abort the
1563 * loop since we can't send more data from other ACs before
1564 * the buffered frames from this.
1566 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1567 !skb_queue_empty(&sta->ps_tx_buf[ac]))
1573 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1574 int n_frames, u8 ignored_acs,
1575 enum ieee80211_frame_release_type reason)
1577 struct ieee80211_sub_if_data *sdata = sta->sdata;
1578 struct ieee80211_local *local = sdata->local;
1579 unsigned long driver_release_tids = 0;
1580 struct sk_buff_head frames;
1583 /* Service or PS-Poll period starts */
1584 set_sta_flag(sta, WLAN_STA_SP);
1586 __skb_queue_head_init(&frames);
1588 ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1589 &frames, &driver_release_tids);
1591 more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1593 if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1594 driver_release_tids =
1595 BIT(find_highest_prio_tid(driver_release_tids));
1597 if (skb_queue_empty(&frames) && !driver_release_tids) {
1601 * For PS-Poll, this can only happen due to a race condition
1602 * when we set the TIM bit and the station notices it, but
1603 * before it can poll for the frame we expire it.
1605 * For uAPSD, this is said in the standard (11.2.1.5 h):
1606 * At each unscheduled SP for a non-AP STA, the AP shall
1607 * attempt to transmit at least one MSDU or MMPDU, but no
1608 * more than the value specified in the Max SP Length field
1609 * in the QoS Capability element from delivery-enabled ACs,
1610 * that are destined for the non-AP STA.
1612 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1615 /* This will evaluate to 1, 3, 5 or 7. */
1616 for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1617 if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1621 ieee80211_send_null_response(sta, tid, reason, true, false);
1622 } else if (!driver_release_tids) {
1623 struct sk_buff_head pending;
1624 struct sk_buff *skb;
1627 bool need_null = false;
1629 skb_queue_head_init(&pending);
1631 while ((skb = __skb_dequeue(&frames))) {
1632 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1633 struct ieee80211_hdr *hdr = (void *) skb->data;
1639 * Tell TX path to send this frame even though the
1640 * STA may still remain is PS mode after this frame
1643 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1644 info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1647 * Use MoreData flag to indicate whether there are
1648 * more buffered frames for this STA
1650 if (more_data || !skb_queue_empty(&frames))
1651 hdr->frame_control |=
1652 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1654 hdr->frame_control &=
1655 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1657 if (ieee80211_is_data_qos(hdr->frame_control) ||
1658 ieee80211_is_qos_nullfunc(hdr->frame_control))
1659 qoshdr = ieee80211_get_qos_ctl(hdr);
1661 tids |= BIT(skb->priority);
1663 __skb_queue_tail(&pending, skb);
1665 /* end service period after last frame or add one */
1666 if (!skb_queue_empty(&frames))
1669 if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1670 /* for PS-Poll, there's only one frame */
1671 info->flags |= IEEE80211_TX_STATUS_EOSP |
1672 IEEE80211_TX_CTL_REQ_TX_STATUS;
1676 /* For uAPSD, things are a bit more complicated. If the
1677 * last frame has a QoS header (i.e. is a QoS-data or
1678 * QoS-nulldata frame) then just set the EOSP bit there
1680 * If the frame doesn't have a QoS header (which means
1681 * it should be a bufferable MMPDU) then we can't set
1682 * the EOSP bit in the QoS header; add a QoS-nulldata
1683 * frame to the list to send it after the MMPDU.
1685 * Note that this code is only in the mac80211-release
1686 * code path, we assume that the driver will not buffer
1687 * anything but QoS-data frames, or if it does, will
1688 * create the QoS-nulldata frame by itself if needed.
1690 * Cf. 802.11-2012 10.2.1.10 (c).
1693 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1695 info->flags |= IEEE80211_TX_STATUS_EOSP |
1696 IEEE80211_TX_CTL_REQ_TX_STATUS;
1698 /* The standard isn't completely clear on this
1699 * as it says the more-data bit should be set
1700 * if there are more BUs. The QoS-Null frame
1701 * we're about to send isn't buffered yet, we
1702 * only create it below, but let's pretend it
1703 * was buffered just in case some clients only
1704 * expect more-data=0 when eosp=1.
1706 hdr->frame_control |=
1707 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1714 drv_allow_buffered_frames(local, sta, tids, num,
1717 ieee80211_add_pending_skbs(local, &pending);
1720 ieee80211_send_null_response(
1721 sta, find_highest_prio_tid(tids),
1722 reason, false, false);
1724 sta_info_recalc_tim(sta);
1729 * We need to release a frame that is buffered somewhere in the
1730 * driver ... it'll have to handle that.
1731 * Note that the driver also has to check the number of frames
1732 * on the TIDs we're releasing from - if there are more than
1733 * n_frames it has to set the more-data bit (if we didn't ask
1734 * it to set it anyway due to other buffered frames); if there
1735 * are fewer than n_frames it has to make sure to adjust that
1736 * to allow the service period to end properly.
1738 drv_release_buffered_frames(local, sta, driver_release_tids,
1739 n_frames, reason, more_data);
1742 * Note that we don't recalculate the TIM bit here as it would
1743 * most likely have no effect at all unless the driver told us
1744 * that the TID(s) became empty before returning here from the
1746 * Either way, however, when the driver tells us that the TID(s)
1747 * became empty or we find that a txq became empty, we'll do the
1748 * TIM recalculation.
1751 if (!sta->sta.txq[0])
1754 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1755 if (!sta->sta.txq[tid] ||
1756 !(driver_release_tids & BIT(tid)) ||
1757 txq_has_queue(sta->sta.txq[tid]))
1760 sta_info_recalc_tim(sta);
1766 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1768 u8 ignore_for_response = sta->sta.uapsd_queues;
1771 * If all ACs are delivery-enabled then we should reply
1772 * from any of them, if only some are enabled we reply
1773 * only from the non-enabled ones.
1775 if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1776 ignore_for_response = 0;
1778 ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1779 IEEE80211_FRAME_RELEASE_PSPOLL);
1782 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1784 int n_frames = sta->sta.max_sp;
1785 u8 delivery_enabled = sta->sta.uapsd_queues;
1788 * If we ever grow support for TSPEC this might happen if
1789 * the TSPEC update from hostapd comes in between a trigger
1790 * frame setting WLAN_STA_UAPSD in the RX path and this
1791 * actually getting called.
1793 if (!delivery_enabled)
1796 switch (sta->sta.max_sp) {
1807 /* XXX: what is a good value? */
1812 ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1813 IEEE80211_FRAME_RELEASE_UAPSD);
1816 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1817 struct ieee80211_sta *pubsta, bool block)
1819 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1821 trace_api_sta_block_awake(sta->local, pubsta, block);
1824 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1825 ieee80211_clear_fast_xmit(sta);
1829 if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1832 if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1833 set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1834 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1835 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1836 } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1837 test_sta_flag(sta, WLAN_STA_UAPSD)) {
1838 /* must be asleep in this case */
1839 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1840 ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1842 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1843 ieee80211_check_fast_xmit(sta);
1846 EXPORT_SYMBOL(ieee80211_sta_block_awake);
1848 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
1850 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1851 struct ieee80211_local *local = sta->local;
1853 trace_api_eosp(local, pubsta);
1855 clear_sta_flag(sta, WLAN_STA_SP);
1857 EXPORT_SYMBOL(ieee80211_sta_eosp);
1859 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1861 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1862 enum ieee80211_frame_release_type reason;
1865 trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1867 reason = IEEE80211_FRAME_RELEASE_UAPSD;
1868 more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1871 ieee80211_send_null_response(sta, tid, reason, false, more_data);
1873 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1875 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1876 u8 tid, bool buffered)
1878 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1880 if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1883 trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1886 set_bit(tid, &sta->driver_buffered_tids);
1888 clear_bit(tid, &sta->driver_buffered_tids);
1890 sta_info_recalc_tim(sta);
1892 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1894 void ieee80211_register_airtime(struct ieee80211_txq *txq,
1895 u32 tx_airtime, u32 rx_airtime)
1897 struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif);
1898 struct ieee80211_local *local = sdata->local;
1899 u64 weight_sum, weight_sum_reciprocal;
1900 struct airtime_sched_info *air_sched;
1901 struct airtime_info *air_info;
1904 air_sched = &local->airtime[txq->ac];
1905 air_info = to_airtime_info(txq);
1907 if (local->airtime_flags & AIRTIME_USE_TX)
1908 airtime += tx_airtime;
1909 if (local->airtime_flags & AIRTIME_USE_RX)
1910 airtime += rx_airtime;
1912 /* Weights scale so the unit weight is 256 */
1915 spin_lock_bh(&air_sched->lock);
1917 air_info->tx_airtime += tx_airtime;
1918 air_info->rx_airtime += rx_airtime;
1920 if (air_sched->weight_sum) {
1921 weight_sum = air_sched->weight_sum;
1922 weight_sum_reciprocal = air_sched->weight_sum_reciprocal;
1924 weight_sum = air_info->weight;
1925 weight_sum_reciprocal = air_info->weight_reciprocal;
1928 /* Round the calculation of global vt */
1929 air_sched->v_t += (u64)((airtime + (weight_sum >> 1)) *
1930 weight_sum_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_64;
1931 air_info->v_t += (u32)((airtime + (air_info->weight >> 1)) *
1932 air_info->weight_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_32;
1933 ieee80211_resort_txq(&local->hw, txq);
1935 spin_unlock_bh(&air_sched->lock);
1938 void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
1939 u32 tx_airtime, u32 rx_airtime)
1941 struct ieee80211_txq *txq = pubsta->txq[tid];
1946 ieee80211_register_airtime(txq, tx_airtime, rx_airtime);
1948 EXPORT_SYMBOL(ieee80211_sta_register_airtime);
1950 void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
1951 struct sta_info *sta, u8 ac,
1952 u16 tx_airtime, bool tx_completed)
1956 if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
1959 if (!tx_completed) {
1961 atomic_add(tx_airtime,
1962 &sta->airtime[ac].aql_tx_pending);
1964 atomic_add(tx_airtime, &local->aql_total_pending_airtime);
1969 tx_pending = atomic_sub_return(tx_airtime,
1970 &sta->airtime[ac].aql_tx_pending);
1972 atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
1976 tx_pending = atomic_sub_return(tx_airtime,
1977 &local->aql_total_pending_airtime);
1978 if (WARN_ONCE(tx_pending < 0,
1979 "Device %s AC %d pending airtime underflow: %u, %u",
1980 wiphy_name(local->hw.wiphy), ac, tx_pending,
1982 atomic_cmpxchg(&local->aql_total_pending_airtime,
1986 int sta_info_move_state(struct sta_info *sta,
1987 enum ieee80211_sta_state new_state)
1991 if (sta->sta_state == new_state)
1994 /* check allowed transitions first */
1996 switch (new_state) {
1997 case IEEE80211_STA_NONE:
1998 if (sta->sta_state != IEEE80211_STA_AUTH)
2001 case IEEE80211_STA_AUTH:
2002 if (sta->sta_state != IEEE80211_STA_NONE &&
2003 sta->sta_state != IEEE80211_STA_ASSOC)
2006 case IEEE80211_STA_ASSOC:
2007 if (sta->sta_state != IEEE80211_STA_AUTH &&
2008 sta->sta_state != IEEE80211_STA_AUTHORIZED)
2011 case IEEE80211_STA_AUTHORIZED:
2012 if (sta->sta_state != IEEE80211_STA_ASSOC)
2016 WARN(1, "invalid state %d", new_state);
2020 sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2021 sta->sta.addr, new_state);
2024 * notify the driver before the actual changes so it can
2025 * fail the transition
2027 if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2028 int err = drv_sta_state(sta->local, sta->sdata, sta,
2029 sta->sta_state, new_state);
2034 /* reflect the change in all state variables */
2036 switch (new_state) {
2037 case IEEE80211_STA_NONE:
2038 if (sta->sta_state == IEEE80211_STA_AUTH)
2039 clear_bit(WLAN_STA_AUTH, &sta->_flags);
2041 case IEEE80211_STA_AUTH:
2042 if (sta->sta_state == IEEE80211_STA_NONE) {
2043 set_bit(WLAN_STA_AUTH, &sta->_flags);
2044 } else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2045 clear_bit(WLAN_STA_ASSOC, &sta->_flags);
2046 ieee80211_recalc_min_chandef(sta->sdata);
2047 if (!sta->sta.support_p2p_ps)
2048 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2051 case IEEE80211_STA_ASSOC:
2052 if (sta->sta_state == IEEE80211_STA_AUTH) {
2053 set_bit(WLAN_STA_ASSOC, &sta->_flags);
2054 sta->assoc_at = ktime_get_boottime_ns();
2055 ieee80211_recalc_min_chandef(sta->sdata);
2056 if (!sta->sta.support_p2p_ps)
2057 ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2058 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
2059 ieee80211_vif_dec_num_mcast(sta->sdata);
2060 clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
2061 ieee80211_clear_fast_xmit(sta);
2062 ieee80211_clear_fast_rx(sta);
2065 case IEEE80211_STA_AUTHORIZED:
2066 if (sta->sta_state == IEEE80211_STA_ASSOC) {
2067 ieee80211_vif_inc_num_mcast(sta->sdata);
2068 set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
2069 ieee80211_check_fast_xmit(sta);
2070 ieee80211_check_fast_rx(sta);
2072 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2073 sta->sdata->vif.type == NL80211_IFTYPE_AP)
2074 cfg80211_send_layer2_update(sta->sdata->dev,
2081 sta->sta_state = new_state;
2086 u8 sta_info_tx_streams(struct sta_info *sta)
2088 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
2091 if (!sta->sta.ht_cap.ht_supported)
2094 if (sta->sta.vht_cap.vht_supported) {
2097 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
2099 for (i = 7; i >= 0; i--)
2100 if ((tx_mcs_map & (0x3 << (i * 2))) !=
2101 IEEE80211_VHT_MCS_NOT_SUPPORTED)
2105 if (ht_cap->mcs.rx_mask[3])
2107 else if (ht_cap->mcs.rx_mask[2])
2109 else if (ht_cap->mcs.rx_mask[1])
2114 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
2117 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
2118 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
2121 static struct ieee80211_sta_rx_stats *
2122 sta_get_last_rx_stats(struct sta_info *sta)
2124 struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
2127 if (!sta->pcpu_rx_stats)
2130 for_each_possible_cpu(cpu) {
2131 struct ieee80211_sta_rx_stats *cpustats;
2133 cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2135 if (time_after(cpustats->last_rx, stats->last_rx))
2142 static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
2143 struct rate_info *rinfo)
2145 rinfo->bw = STA_STATS_GET(BW, rate);
2147 switch (STA_STATS_GET(TYPE, rate)) {
2148 case STA_STATS_RATE_TYPE_VHT:
2149 rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2150 rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2151 rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2152 if (STA_STATS_GET(SGI, rate))
2153 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2155 case STA_STATS_RATE_TYPE_HT:
2156 rinfo->flags = RATE_INFO_FLAGS_MCS;
2157 rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2158 if (STA_STATS_GET(SGI, rate))
2159 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2161 case STA_STATS_RATE_TYPE_LEGACY: {
2162 struct ieee80211_supported_band *sband;
2165 int band = STA_STATS_GET(LEGACY_BAND, rate);
2166 int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2168 sband = local->hw.wiphy->bands[band];
2170 if (WARN_ON_ONCE(!sband->bitrates))
2173 brate = sband->bitrates[rate_idx].bitrate;
2174 if (rinfo->bw == RATE_INFO_BW_5)
2176 else if (rinfo->bw == RATE_INFO_BW_10)
2180 rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
2183 case STA_STATS_RATE_TYPE_HE:
2184 rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
2185 rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
2186 rinfo->nss = STA_STATS_GET(HE_NSS, rate);
2187 rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
2188 rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
2189 rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
2194 static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
2196 u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
2198 if (rate == STA_STATS_RATE_INVALID)
2201 sta_stats_decode_rate(sta->local, rate, rinfo);
2205 static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2212 start = u64_stats_fetch_begin(&rxstats->syncp);
2213 value = rxstats->msdu[tid];
2214 } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2219 static void sta_set_tidstats(struct sta_info *sta,
2220 struct cfg80211_tid_stats *tidstats,
2223 struct ieee80211_local *local = sta->local;
2226 if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2227 tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->rx_stats, tid);
2229 if (sta->pcpu_rx_stats) {
2230 for_each_possible_cpu(cpu) {
2231 struct ieee80211_sta_rx_stats *cpurxs;
2233 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2234 tidstats->rx_msdu +=
2235 sta_get_tidstats_msdu(cpurxs, tid);
2239 tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2242 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2243 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2244 tidstats->tx_msdu = sta->tx_stats.msdu[tid];
2247 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2248 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2249 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2250 tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
2253 if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2254 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2255 tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2256 tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
2259 if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
2260 spin_lock_bh(&local->fq.lock);
2263 tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
2264 ieee80211_fill_txq_stats(&tidstats->txq_stats,
2265 to_txq_info(sta->sta.txq[tid]));
2268 spin_unlock_bh(&local->fq.lock);
2272 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2278 start = u64_stats_fetch_begin(&rxstats->syncp);
2279 value = rxstats->bytes;
2280 } while (u64_stats_fetch_retry(&rxstats->syncp, start));
2285 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
2288 struct ieee80211_sub_if_data *sdata = sta->sdata;
2289 struct ieee80211_local *local = sdata->local;
2292 struct ieee80211_sta_rx_stats *last_rxstats;
2294 last_rxstats = sta_get_last_rx_stats(sta);
2296 sinfo->generation = sdata->local->sta_generation;
2298 /* do before driver, so beacon filtering drivers have a
2299 * chance to e.g. just add the number of filtered beacons
2300 * (or just modify the value entirely, of course)
2302 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2303 sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
2305 drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2306 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2307 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2308 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2309 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
2310 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2311 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2313 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2314 sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
2315 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2318 sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
2319 sinfo->assoc_at = sta->assoc_at;
2320 sinfo->inactive_time =
2321 jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
2323 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2324 BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2325 sinfo->tx_bytes = 0;
2326 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2327 sinfo->tx_bytes += sta->tx_stats.bytes[ac];
2328 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2331 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
2332 sinfo->tx_packets = 0;
2333 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2334 sinfo->tx_packets += sta->tx_stats.packets[ac];
2335 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
2338 if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2339 BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2340 sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats);
2342 if (sta->pcpu_rx_stats) {
2343 for_each_possible_cpu(cpu) {
2344 struct ieee80211_sta_rx_stats *cpurxs;
2346 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2347 sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2351 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
2354 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2355 sinfo->rx_packets = sta->rx_stats.packets;
2356 if (sta->pcpu_rx_stats) {
2357 for_each_possible_cpu(cpu) {
2358 struct ieee80211_sta_rx_stats *cpurxs;
2360 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2361 sinfo->rx_packets += cpurxs->packets;
2364 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
2367 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2368 sinfo->tx_retries = sta->status_stats.retry_count;
2369 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
2372 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2373 sinfo->tx_failed = sta->status_stats.retry_failed;
2374 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
2377 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2378 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2379 sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2380 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2383 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2384 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2385 sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2386 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2389 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2390 sinfo->airtime_weight = sta->airtime[0].weight;
2391 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2394 sinfo->rx_dropped_misc = sta->rx_stats.dropped;
2395 if (sta->pcpu_rx_stats) {
2396 for_each_possible_cpu(cpu) {
2397 struct ieee80211_sta_rx_stats *cpurxs;
2399 cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
2400 sinfo->rx_dropped_misc += cpurxs->dropped;
2404 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2405 !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2406 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2407 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2408 sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2411 if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2412 ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2413 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2414 sinfo->signal = (s8)last_rxstats->last_signal;
2415 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2418 if (!sta->pcpu_rx_stats &&
2419 !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
2421 -ewma_signal_read(&sta->rx_stats_avg.signal);
2422 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
2426 /* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2427 * the sta->rx_stats struct, so the check here is fine with and without
2430 if (last_rxstats->chains &&
2431 !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2432 BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2433 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2434 if (!sta->pcpu_rx_stats)
2435 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2437 sinfo->chains = last_rxstats->chains;
2439 for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2440 sinfo->chain_signal[i] =
2441 last_rxstats->chain_signal_last[i];
2442 sinfo->chain_signal_avg[i] =
2443 -ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
2447 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
2448 sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2450 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2453 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
2454 if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2455 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
2458 if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
2459 for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
2460 sta_set_tidstats(sta, &sinfo->pertid[i], i);
2463 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2464 #ifdef CONFIG_MAC80211_MESH
2465 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2466 BIT_ULL(NL80211_STA_INFO_PLID) |
2467 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2468 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2469 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2470 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
2471 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
2472 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2474 sinfo->llid = sta->mesh->llid;
2475 sinfo->plid = sta->mesh->plid;
2476 sinfo->plink_state = sta->mesh->plink_state;
2477 if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2478 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2479 sinfo->t_offset = sta->mesh->t_offset;
2481 sinfo->local_pm = sta->mesh->local_pm;
2482 sinfo->peer_pm = sta->mesh->peer_pm;
2483 sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2484 sinfo->connected_to_gate = sta->mesh->connected_to_gate;
2485 sinfo->connected_to_as = sta->mesh->connected_to_as;
2489 sinfo->bss_param.flags = 0;
2490 if (sdata->vif.bss_conf.use_cts_prot)
2491 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2492 if (sdata->vif.bss_conf.use_short_preamble)
2493 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2494 if (sdata->vif.bss_conf.use_short_slot)
2495 sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2496 sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2497 sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2499 sinfo->sta_flags.set = 0;
2500 sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2501 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2502 BIT(NL80211_STA_FLAG_WME) |
2503 BIT(NL80211_STA_FLAG_MFP) |
2504 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2505 BIT(NL80211_STA_FLAG_ASSOCIATED) |
2506 BIT(NL80211_STA_FLAG_TDLS_PEER);
2507 if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2508 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2509 if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2510 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2512 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2513 if (test_sta_flag(sta, WLAN_STA_MFP))
2514 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2515 if (test_sta_flag(sta, WLAN_STA_AUTH))
2516 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2517 if (test_sta_flag(sta, WLAN_STA_ASSOC))
2518 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2519 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2520 sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2522 thr = sta_get_expected_throughput(sta);
2525 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2526 sinfo->expected_throughput = thr;
2529 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2530 sta->status_stats.ack_signal_filled) {
2531 sinfo->ack_signal = sta->status_stats.last_ack_signal;
2532 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2535 if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2536 sta->status_stats.ack_signal_filled) {
2537 sinfo->avg_ack_signal =
2538 -(s8)ewma_avg_signal_read(
2539 &sta->status_stats.avg_ack_signal);
2541 BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2544 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2545 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2546 sinfo->airtime_link_metric =
2547 airtime_link_metric_get(local, sta);
2551 u32 sta_get_expected_throughput(struct sta_info *sta)
2553 struct ieee80211_sub_if_data *sdata = sta->sdata;
2554 struct ieee80211_local *local = sdata->local;
2555 struct rate_control_ref *ref = NULL;
2558 if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
2559 ref = local->rate_ctrl;
2561 /* check if the driver has a SW RC implementation */
2562 if (ref && ref->ops->get_expected_throughput)
2563 thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2565 thr = drv_get_expected_throughput(local, sta);
2570 unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2572 struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2574 if (!sta->status_stats.last_ack ||
2575 time_after(stats->last_rx, sta->status_stats.last_ack))
2576 return stats->last_rx;
2577 return sta->status_stats.last_ack;
2580 static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2582 if (!sta->sdata->local->ops->wake_tx_queue)
2585 if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2586 sta->cparams.target = MS2TIME(50);
2587 sta->cparams.interval = MS2TIME(300);
2588 sta->cparams.ecn = false;
2590 sta->cparams.target = MS2TIME(20);
2591 sta->cparams.interval = MS2TIME(100);
2592 sta->cparams.ecn = true;
2596 void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2599 struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2601 sta_update_codel_params(sta, thr);