18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2002-2005, Instant802 Networks, Inc.
48c2ecf20Sopenharmony_ci * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
58c2ecf20Sopenharmony_ci * Copyright 2013-2014  Intel Mobile Communications GmbH
68c2ecf20Sopenharmony_ci * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
78c2ecf20Sopenharmony_ci * Copyright (C) 2018-2021 Intel Corporation
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/init.h>
128c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
138c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
148c2ecf20Sopenharmony_ci#include <linux/types.h>
158c2ecf20Sopenharmony_ci#include <linux/slab.h>
168c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
178c2ecf20Sopenharmony_ci#include <linux/if_arp.h>
188c2ecf20Sopenharmony_ci#include <linux/timer.h>
198c2ecf20Sopenharmony_ci#include <linux/rtnetlink.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include <net/codel.h>
228c2ecf20Sopenharmony_ci#include <net/mac80211.h>
238c2ecf20Sopenharmony_ci#include "ieee80211_i.h"
248c2ecf20Sopenharmony_ci#include "driver-ops.h"
258c2ecf20Sopenharmony_ci#include "rate.h"
268c2ecf20Sopenharmony_ci#include "sta_info.h"
278c2ecf20Sopenharmony_ci#include "debugfs_sta.h"
288c2ecf20Sopenharmony_ci#include "mesh.h"
298c2ecf20Sopenharmony_ci#include "wme.h"
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/**
328c2ecf20Sopenharmony_ci * DOC: STA information lifetime rules
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * STA info structures (&struct sta_info) are managed in a hash table
358c2ecf20Sopenharmony_ci * for faster lookup and a list for iteration. They are managed using
368c2ecf20Sopenharmony_ci * RCU, i.e. access to the list and hash table is protected by RCU.
378c2ecf20Sopenharmony_ci *
388c2ecf20Sopenharmony_ci * Upon allocating a STA info structure with sta_info_alloc(), the caller
398c2ecf20Sopenharmony_ci * owns that structure. It must then insert it into the hash table using
408c2ecf20Sopenharmony_ci * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
418c2ecf20Sopenharmony_ci * case (which acquires an rcu read section but must not be called from
428c2ecf20Sopenharmony_ci * within one) will the pointer still be valid after the call. Note that
438c2ecf20Sopenharmony_ci * the caller may not do much with the STA info before inserting it, in
448c2ecf20Sopenharmony_ci * particular, it may not start any mesh peer link management or add
458c2ecf20Sopenharmony_ci * encryption keys.
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci * When the insertion fails (sta_info_insert()) returns non-zero), the
488c2ecf20Sopenharmony_ci * structure will have been freed by sta_info_insert()!
498c2ecf20Sopenharmony_ci *
508c2ecf20Sopenharmony_ci * Station entries are added by mac80211 when you establish a link with a
518c2ecf20Sopenharmony_ci * peer. This means different things for the different type of interfaces
528c2ecf20Sopenharmony_ci * we support. For a regular station this mean we add the AP sta when we
538c2ecf20Sopenharmony_ci * receive an association response from the AP. For IBSS this occurs when
548c2ecf20Sopenharmony_ci * get to know about a peer on the same IBSS. For WDS we add the sta for
558c2ecf20Sopenharmony_ci * the peer immediately upon device open. When using AP mode we add stations
568c2ecf20Sopenharmony_ci * for each respective station upon request from userspace through nl80211.
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci * In order to remove a STA info structure, various sta_info_destroy_*()
598c2ecf20Sopenharmony_ci * calls are available.
608c2ecf20Sopenharmony_ci *
618c2ecf20Sopenharmony_ci * There is no concept of ownership on a STA entry, each structure is
628c2ecf20Sopenharmony_ci * owned by the global hash table/list until it is removed. All users of
638c2ecf20Sopenharmony_ci * the structure need to be RCU protected so that the structure won't be
648c2ecf20Sopenharmony_ci * freed before they are done using it.
658c2ecf20Sopenharmony_ci */
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic const struct rhashtable_params sta_rht_params = {
688c2ecf20Sopenharmony_ci	.nelem_hint = 3, /* start small */
698c2ecf20Sopenharmony_ci	.automatic_shrinking = true,
708c2ecf20Sopenharmony_ci	.head_offset = offsetof(struct sta_info, hash_node),
718c2ecf20Sopenharmony_ci	.key_offset = offsetof(struct sta_info, addr),
728c2ecf20Sopenharmony_ci	.key_len = ETH_ALEN,
738c2ecf20Sopenharmony_ci	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
748c2ecf20Sopenharmony_ci};
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/* Caller must hold local->sta_mtx */
778c2ecf20Sopenharmony_cistatic int sta_info_hash_del(struct ieee80211_local *local,
788c2ecf20Sopenharmony_ci			     struct sta_info *sta)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	return rhltable_remove(&local->sta_hash, &sta->hash_node,
818c2ecf20Sopenharmony_ci			       sta_rht_params);
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic void __cleanup_single_sta(struct sta_info *sta)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	int ac, i;
878c2ecf20Sopenharmony_ci	struct tid_ampdu_tx *tid_tx;
888c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata = sta->sdata;
898c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
908c2ecf20Sopenharmony_ci	struct ps_data *ps;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
938c2ecf20Sopenharmony_ci	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
948c2ecf20Sopenharmony_ci	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
958c2ecf20Sopenharmony_ci		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
968c2ecf20Sopenharmony_ci		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
978c2ecf20Sopenharmony_ci			ps = &sdata->bss->ps;
988c2ecf20Sopenharmony_ci		else if (ieee80211_vif_is_mesh(&sdata->vif))
998c2ecf20Sopenharmony_ci			ps = &sdata->u.mesh.ps;
1008c2ecf20Sopenharmony_ci		else
1018c2ecf20Sopenharmony_ci			return;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci		clear_sta_flag(sta, WLAN_STA_PS_STA);
1048c2ecf20Sopenharmony_ci		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1058c2ecf20Sopenharmony_ci		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci		atomic_dec(&ps->num_sta_ps);
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	if (sta->sta.txq[0]) {
1118c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1128c2ecf20Sopenharmony_ci			struct txq_info *txqi;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci			if (!sta->sta.txq[i])
1158c2ecf20Sopenharmony_ci				continue;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci			txqi = to_txq_info(sta->sta.txq[i]);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci			ieee80211_txq_purge(local, txqi);
1208c2ecf20Sopenharmony_ci		}
1218c2ecf20Sopenharmony_ci	}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1248c2ecf20Sopenharmony_ci		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1258c2ecf20Sopenharmony_ci		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1268c2ecf20Sopenharmony_ci		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
1278c2ecf20Sopenharmony_ci	}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	if (ieee80211_vif_is_mesh(&sdata->vif))
1308c2ecf20Sopenharmony_ci		mesh_sta_cleanup(sta);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	cancel_work_sync(&sta->drv_deliver_wk);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	/*
1358c2ecf20Sopenharmony_ci	 * Destroy aggregation state here. It would be nice to wait for the
1368c2ecf20Sopenharmony_ci	 * driver to finish aggregation stop and then clean up, but for now
1378c2ecf20Sopenharmony_ci	 * drivers have to handle aggregation stop being requested, followed
1388c2ecf20Sopenharmony_ci	 * directly by station destruction.
1398c2ecf20Sopenharmony_ci	 */
1408c2ecf20Sopenharmony_ci	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
1418c2ecf20Sopenharmony_ci		kfree(sta->ampdu_mlme.tid_start_tx[i]);
1428c2ecf20Sopenharmony_ci		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
1438c2ecf20Sopenharmony_ci		if (!tid_tx)
1448c2ecf20Sopenharmony_ci			continue;
1458c2ecf20Sopenharmony_ci		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
1468c2ecf20Sopenharmony_ci		kfree(tid_tx);
1478c2ecf20Sopenharmony_ci	}
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cistatic void cleanup_single_sta(struct sta_info *sta)
1518c2ecf20Sopenharmony_ci{
1528c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata = sta->sdata;
1538c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	__cleanup_single_sta(sta);
1568c2ecf20Sopenharmony_ci	sta_info_free(local, sta);
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistruct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
1608c2ecf20Sopenharmony_ci					 const u8 *addr)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci/* protected by RCU */
1668c2ecf20Sopenharmony_cistruct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
1678c2ecf20Sopenharmony_ci			      const u8 *addr)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
1708c2ecf20Sopenharmony_ci	struct rhlist_head *tmp;
1718c2ecf20Sopenharmony_ci	struct sta_info *sta;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	rcu_read_lock();
1748c2ecf20Sopenharmony_ci	for_each_sta_info(local, addr, sta, tmp) {
1758c2ecf20Sopenharmony_ci		if (sta->sdata == sdata) {
1768c2ecf20Sopenharmony_ci			rcu_read_unlock();
1778c2ecf20Sopenharmony_ci			/* this is safe as the caller must already hold
1788c2ecf20Sopenharmony_ci			 * another rcu read section or the mutex
1798c2ecf20Sopenharmony_ci			 */
1808c2ecf20Sopenharmony_ci			return sta;
1818c2ecf20Sopenharmony_ci		}
1828c2ecf20Sopenharmony_ci	}
1838c2ecf20Sopenharmony_ci	rcu_read_unlock();
1848c2ecf20Sopenharmony_ci	return NULL;
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci/*
1888c2ecf20Sopenharmony_ci * Get sta info either from the specified interface
1898c2ecf20Sopenharmony_ci * or from one of its vlans
1908c2ecf20Sopenharmony_ci */
1918c2ecf20Sopenharmony_cistruct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
1928c2ecf20Sopenharmony_ci				  const u8 *addr)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
1958c2ecf20Sopenharmony_ci	struct rhlist_head *tmp;
1968c2ecf20Sopenharmony_ci	struct sta_info *sta;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	rcu_read_lock();
1998c2ecf20Sopenharmony_ci	for_each_sta_info(local, addr, sta, tmp) {
2008c2ecf20Sopenharmony_ci		if (sta->sdata == sdata ||
2018c2ecf20Sopenharmony_ci		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
2028c2ecf20Sopenharmony_ci			rcu_read_unlock();
2038c2ecf20Sopenharmony_ci			/* this is safe as the caller must already hold
2048c2ecf20Sopenharmony_ci			 * another rcu read section or the mutex
2058c2ecf20Sopenharmony_ci			 */
2068c2ecf20Sopenharmony_ci			return sta;
2078c2ecf20Sopenharmony_ci		}
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci	rcu_read_unlock();
2108c2ecf20Sopenharmony_ci	return NULL;
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistruct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
2148c2ecf20Sopenharmony_ci				       const u8 *sta_addr, const u8 *vif_addr)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	struct rhlist_head *tmp;
2178c2ecf20Sopenharmony_ci	struct sta_info *sta;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	for_each_sta_info(local, sta_addr, sta, tmp) {
2208c2ecf20Sopenharmony_ci		if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
2218c2ecf20Sopenharmony_ci			return sta;
2228c2ecf20Sopenharmony_ci	}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	return NULL;
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_cistruct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2288c2ecf20Sopenharmony_ci				     int idx)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
2318c2ecf20Sopenharmony_ci	struct sta_info *sta;
2328c2ecf20Sopenharmony_ci	int i = 0;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	list_for_each_entry_rcu(sta, &local->sta_list, list,
2358c2ecf20Sopenharmony_ci				lockdep_is_held(&local->sta_mtx)) {
2368c2ecf20Sopenharmony_ci		if (sdata != sta->sdata)
2378c2ecf20Sopenharmony_ci			continue;
2388c2ecf20Sopenharmony_ci		if (i < idx) {
2398c2ecf20Sopenharmony_ci			++i;
2408c2ecf20Sopenharmony_ci			continue;
2418c2ecf20Sopenharmony_ci		}
2428c2ecf20Sopenharmony_ci		return sta;
2438c2ecf20Sopenharmony_ci	}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	return NULL;
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci/**
2498c2ecf20Sopenharmony_ci * sta_info_free - free STA
2508c2ecf20Sopenharmony_ci *
2518c2ecf20Sopenharmony_ci * @local: pointer to the global information
2528c2ecf20Sopenharmony_ci * @sta: STA info to free
2538c2ecf20Sopenharmony_ci *
2548c2ecf20Sopenharmony_ci * This function must undo everything done by sta_info_alloc()
2558c2ecf20Sopenharmony_ci * that may happen before sta_info_insert(). It may only be
2568c2ecf20Sopenharmony_ci * called when sta_info_insert() has not been attempted (and
2578c2ecf20Sopenharmony_ci * if that fails, the station is freed anyway.)
2588c2ecf20Sopenharmony_ci */
2598c2ecf20Sopenharmony_civoid sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
2608c2ecf20Sopenharmony_ci{
2618c2ecf20Sopenharmony_ci	/*
2628c2ecf20Sopenharmony_ci	 * If we had used sta_info_pre_move_state() then we might not
2638c2ecf20Sopenharmony_ci	 * have gone through the state transitions down again, so do
2648c2ecf20Sopenharmony_ci	 * it here now (and warn if it's inserted).
2658c2ecf20Sopenharmony_ci	 *
2668c2ecf20Sopenharmony_ci	 * This will clear state such as fast TX/RX that may have been
2678c2ecf20Sopenharmony_ci	 * allocated during state transitions.
2688c2ecf20Sopenharmony_ci	 */
2698c2ecf20Sopenharmony_ci	while (sta->sta_state > IEEE80211_STA_NONE) {
2708c2ecf20Sopenharmony_ci		int ret;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci		WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci		ret = sta_info_move_state(sta, sta->sta_state - 1);
2758c2ecf20Sopenharmony_ci		if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
2768c2ecf20Sopenharmony_ci			break;
2778c2ecf20Sopenharmony_ci	}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	if (sta->rate_ctrl)
2808c2ecf20Sopenharmony_ci		rate_control_free_sta(sta);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	if (sta->sta.txq[0])
2858c2ecf20Sopenharmony_ci		kfree(to_txq_info(sta->sta.txq[0]));
2868c2ecf20Sopenharmony_ci	kfree(rcu_dereference_raw(sta->sta.rates));
2878c2ecf20Sopenharmony_ci#ifdef CONFIG_MAC80211_MESH
2888c2ecf20Sopenharmony_ci	kfree(sta->mesh);
2898c2ecf20Sopenharmony_ci#endif
2908c2ecf20Sopenharmony_ci	free_percpu(sta->pcpu_rx_stats);
2918c2ecf20Sopenharmony_ci	kfree(sta);
2928c2ecf20Sopenharmony_ci}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci/* Caller must hold local->sta_mtx */
2958c2ecf20Sopenharmony_cistatic int sta_info_hash_add(struct ieee80211_local *local,
2968c2ecf20Sopenharmony_ci			     struct sta_info *sta)
2978c2ecf20Sopenharmony_ci{
2988c2ecf20Sopenharmony_ci	return rhltable_insert(&local->sta_hash, &sta->hash_node,
2998c2ecf20Sopenharmony_ci			       sta_rht_params);
3008c2ecf20Sopenharmony_ci}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_cistatic void sta_deliver_ps_frames(struct work_struct *wk)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	struct sta_info *sta;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	sta = container_of(wk, struct sta_info, drv_deliver_wk);
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	if (sta->dead)
3098c2ecf20Sopenharmony_ci		return;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	local_bh_disable();
3128c2ecf20Sopenharmony_ci	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
3138c2ecf20Sopenharmony_ci		ieee80211_sta_ps_deliver_wakeup(sta);
3148c2ecf20Sopenharmony_ci	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
3158c2ecf20Sopenharmony_ci		ieee80211_sta_ps_deliver_poll_response(sta);
3168c2ecf20Sopenharmony_ci	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
3178c2ecf20Sopenharmony_ci		ieee80211_sta_ps_deliver_uapsd(sta);
3188c2ecf20Sopenharmony_ci	local_bh_enable();
3198c2ecf20Sopenharmony_ci}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_cistatic int sta_prepare_rate_control(struct ieee80211_local *local,
3228c2ecf20Sopenharmony_ci				    struct sta_info *sta, gfp_t gfp)
3238c2ecf20Sopenharmony_ci{
3248c2ecf20Sopenharmony_ci	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
3258c2ecf20Sopenharmony_ci		return 0;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	sta->rate_ctrl = local->rate_ctrl;
3288c2ecf20Sopenharmony_ci	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
3298c2ecf20Sopenharmony_ci						     sta, gfp);
3308c2ecf20Sopenharmony_ci	if (!sta->rate_ctrl_priv)
3318c2ecf20Sopenharmony_ci		return -ENOMEM;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	return 0;
3348c2ecf20Sopenharmony_ci}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_cistruct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
3378c2ecf20Sopenharmony_ci				const u8 *addr, gfp_t gfp)
3388c2ecf20Sopenharmony_ci{
3398c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
3408c2ecf20Sopenharmony_ci	struct ieee80211_hw *hw = &local->hw;
3418c2ecf20Sopenharmony_ci	struct sta_info *sta;
3428c2ecf20Sopenharmony_ci	int i;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
3458c2ecf20Sopenharmony_ci	if (!sta)
3468c2ecf20Sopenharmony_ci		return NULL;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	if (ieee80211_hw_check(hw, USES_RSS)) {
3498c2ecf20Sopenharmony_ci		sta->pcpu_rx_stats =
3508c2ecf20Sopenharmony_ci			alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
3518c2ecf20Sopenharmony_ci		if (!sta->pcpu_rx_stats)
3528c2ecf20Sopenharmony_ci			goto free;
3538c2ecf20Sopenharmony_ci	}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	spin_lock_init(&sta->lock);
3568c2ecf20Sopenharmony_ci	spin_lock_init(&sta->ps_lock);
3578c2ecf20Sopenharmony_ci	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
3588c2ecf20Sopenharmony_ci	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
3598c2ecf20Sopenharmony_ci	mutex_init(&sta->ampdu_mlme.mtx);
3608c2ecf20Sopenharmony_ci#ifdef CONFIG_MAC80211_MESH
3618c2ecf20Sopenharmony_ci	if (ieee80211_vif_is_mesh(&sdata->vif)) {
3628c2ecf20Sopenharmony_ci		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
3638c2ecf20Sopenharmony_ci		if (!sta->mesh)
3648c2ecf20Sopenharmony_ci			goto free;
3658c2ecf20Sopenharmony_ci		sta->mesh->plink_sta = sta;
3668c2ecf20Sopenharmony_ci		spin_lock_init(&sta->mesh->plink_lock);
3678c2ecf20Sopenharmony_ci		if (ieee80211_vif_is_mesh(&sdata->vif) &&
3688c2ecf20Sopenharmony_ci		    !sdata->u.mesh.user_mpm)
3698c2ecf20Sopenharmony_ci			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
3708c2ecf20Sopenharmony_ci				    0);
3718c2ecf20Sopenharmony_ci		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
3728c2ecf20Sopenharmony_ci	}
3738c2ecf20Sopenharmony_ci#endif
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	memcpy(sta->addr, addr, ETH_ALEN);
3768c2ecf20Sopenharmony_ci	memcpy(sta->sta.addr, addr, ETH_ALEN);
3778c2ecf20Sopenharmony_ci	sta->sta.max_rx_aggregation_subframes =
3788c2ecf20Sopenharmony_ci		local->hw.max_rx_aggregation_subframes;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	/* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
3818c2ecf20Sopenharmony_ci	 * The Tx path starts to use a key as soon as the key slot ptk_idx
3828c2ecf20Sopenharmony_ci	 * references to is not NULL. To not use the initial Rx-only key
3838c2ecf20Sopenharmony_ci	 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
3848c2ecf20Sopenharmony_ci	 * which always will refer to a NULL key.
3858c2ecf20Sopenharmony_ci	 */
3868c2ecf20Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
3878c2ecf20Sopenharmony_ci	sta->ptk_idx = INVALID_PTK_KEYIDX;
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	sta->local = local;
3908c2ecf20Sopenharmony_ci	sta->sdata = sdata;
3918c2ecf20Sopenharmony_ci	sta->rx_stats.last_rx = jiffies;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	u64_stats_init(&sta->rx_stats.syncp);
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	ieee80211_init_frag_cache(&sta->frags);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	sta->sta_state = IEEE80211_STA_NONE;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	/* Mark TID as unreserved */
4008c2ecf20Sopenharmony_ci	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	sta->last_connected = ktime_get_seconds();
4038c2ecf20Sopenharmony_ci	ewma_signal_init(&sta->rx_stats_avg.signal);
4048c2ecf20Sopenharmony_ci	ewma_avg_signal_init(&sta->status_stats.avg_ack_signal);
4058c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++)
4068c2ecf20Sopenharmony_ci		ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]);
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	if (local->ops->wake_tx_queue) {
4098c2ecf20Sopenharmony_ci		void *txq_data;
4108c2ecf20Sopenharmony_ci		int size = sizeof(struct txq_info) +
4118c2ecf20Sopenharmony_ci			   ALIGN(hw->txq_data_size, sizeof(void *));
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
4148c2ecf20Sopenharmony_ci		if (!txq_data)
4158c2ecf20Sopenharmony_ci			goto free;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
4188c2ecf20Sopenharmony_ci			struct txq_info *txq = txq_data + i * size;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci			/* might not do anything for the bufferable MMPDU TXQ */
4218c2ecf20Sopenharmony_ci			ieee80211_txq_init(sdata, sta, txq, i);
4228c2ecf20Sopenharmony_ci		}
4238c2ecf20Sopenharmony_ci	}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	if (sta_prepare_rate_control(local, sta, gfp))
4268c2ecf20Sopenharmony_ci		goto free_txq;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
4318c2ecf20Sopenharmony_ci		skb_queue_head_init(&sta->ps_tx_buf[i]);
4328c2ecf20Sopenharmony_ci		skb_queue_head_init(&sta->tx_filtered[i]);
4338c2ecf20Sopenharmony_ci		sta->airtime[i].deficit = sta->airtime_weight;
4348c2ecf20Sopenharmony_ci		atomic_set(&sta->airtime[i].aql_tx_pending, 0);
4358c2ecf20Sopenharmony_ci		sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
4368c2ecf20Sopenharmony_ci		sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
4378c2ecf20Sopenharmony_ci	}
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
4408c2ecf20Sopenharmony_ci		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_NL80211_BANDS; i++) {
4438c2ecf20Sopenharmony_ci		u32 mandatory = 0;
4448c2ecf20Sopenharmony_ci		int r;
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci		if (!hw->wiphy->bands[i])
4478c2ecf20Sopenharmony_ci			continue;
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci		switch (i) {
4508c2ecf20Sopenharmony_ci		case NL80211_BAND_2GHZ:
4518c2ecf20Sopenharmony_ci			/*
4528c2ecf20Sopenharmony_ci			 * We use both here, even if we cannot really know for
4538c2ecf20Sopenharmony_ci			 * sure the station will support both, but the only use
4548c2ecf20Sopenharmony_ci			 * for this is when we don't know anything yet and send
4558c2ecf20Sopenharmony_ci			 * management frames, and then we'll pick the lowest
4568c2ecf20Sopenharmony_ci			 * possible rate anyway.
4578c2ecf20Sopenharmony_ci			 * If we don't include _G here, we cannot find a rate
4588c2ecf20Sopenharmony_ci			 * in P2P, and thus trigger the WARN_ONCE() in rate.c
4598c2ecf20Sopenharmony_ci			 */
4608c2ecf20Sopenharmony_ci			mandatory = IEEE80211_RATE_MANDATORY_B |
4618c2ecf20Sopenharmony_ci				    IEEE80211_RATE_MANDATORY_G;
4628c2ecf20Sopenharmony_ci			break;
4638c2ecf20Sopenharmony_ci		case NL80211_BAND_5GHZ:
4648c2ecf20Sopenharmony_ci			mandatory = IEEE80211_RATE_MANDATORY_A;
4658c2ecf20Sopenharmony_ci			break;
4668c2ecf20Sopenharmony_ci		case NL80211_BAND_60GHZ:
4678c2ecf20Sopenharmony_ci			WARN_ON(1);
4688c2ecf20Sopenharmony_ci			mandatory = 0;
4698c2ecf20Sopenharmony_ci			break;
4708c2ecf20Sopenharmony_ci		}
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci		for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
4738c2ecf20Sopenharmony_ci			struct ieee80211_rate *rate;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci			rate = &hw->wiphy->bands[i]->bitrates[r];
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci			if (!(rate->flags & mandatory))
4788c2ecf20Sopenharmony_ci				continue;
4798c2ecf20Sopenharmony_ci			sta->sta.supp_rates[i] |= BIT(r);
4808c2ecf20Sopenharmony_ci		}
4818c2ecf20Sopenharmony_ci	}
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
4848c2ecf20Sopenharmony_ci	if (sdata->vif.type == NL80211_IFTYPE_AP ||
4858c2ecf20Sopenharmony_ci	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
4868c2ecf20Sopenharmony_ci		struct ieee80211_supported_band *sband;
4878c2ecf20Sopenharmony_ci		u8 smps;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci		sband = ieee80211_get_sband(sdata);
4908c2ecf20Sopenharmony_ci		if (!sband)
4918c2ecf20Sopenharmony_ci			goto free_txq;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci		smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
4948c2ecf20Sopenharmony_ci			IEEE80211_HT_CAP_SM_PS_SHIFT;
4958c2ecf20Sopenharmony_ci		/*
4968c2ecf20Sopenharmony_ci		 * Assume that hostapd advertises our caps in the beacon and
4978c2ecf20Sopenharmony_ci		 * this is the known_smps_mode for a station that just assciated
4988c2ecf20Sopenharmony_ci		 */
4998c2ecf20Sopenharmony_ci		switch (smps) {
5008c2ecf20Sopenharmony_ci		case WLAN_HT_SMPS_CONTROL_DISABLED:
5018c2ecf20Sopenharmony_ci			sta->known_smps_mode = IEEE80211_SMPS_OFF;
5028c2ecf20Sopenharmony_ci			break;
5038c2ecf20Sopenharmony_ci		case WLAN_HT_SMPS_CONTROL_STATIC:
5048c2ecf20Sopenharmony_ci			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
5058c2ecf20Sopenharmony_ci			break;
5068c2ecf20Sopenharmony_ci		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
5078c2ecf20Sopenharmony_ci			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
5088c2ecf20Sopenharmony_ci			break;
5098c2ecf20Sopenharmony_ci		default:
5108c2ecf20Sopenharmony_ci			WARN_ON(1);
5118c2ecf20Sopenharmony_ci		}
5128c2ecf20Sopenharmony_ci	}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
5178c2ecf20Sopenharmony_ci	sta->cparams.target = MS2TIME(20);
5188c2ecf20Sopenharmony_ci	sta->cparams.interval = MS2TIME(100);
5198c2ecf20Sopenharmony_ci	sta->cparams.ecn = true;
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	return sta;
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_cifree_txq:
5268c2ecf20Sopenharmony_ci	if (sta->sta.txq[0])
5278c2ecf20Sopenharmony_ci		kfree(to_txq_info(sta->sta.txq[0]));
5288c2ecf20Sopenharmony_cifree:
5298c2ecf20Sopenharmony_ci	free_percpu(sta->pcpu_rx_stats);
5308c2ecf20Sopenharmony_ci#ifdef CONFIG_MAC80211_MESH
5318c2ecf20Sopenharmony_ci	kfree(sta->mesh);
5328c2ecf20Sopenharmony_ci#endif
5338c2ecf20Sopenharmony_ci	kfree(sta);
5348c2ecf20Sopenharmony_ci	return NULL;
5358c2ecf20Sopenharmony_ci}
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_cistatic int sta_info_insert_check(struct sta_info *sta)
5388c2ecf20Sopenharmony_ci{
5398c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata = sta->sdata;
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	/*
5428c2ecf20Sopenharmony_ci	 * Can't be a WARN_ON because it can be triggered through a race:
5438c2ecf20Sopenharmony_ci	 * something inserts a STA (on one CPU) without holding the RTNL
5448c2ecf20Sopenharmony_ci	 * and another CPU turns off the net device.
5458c2ecf20Sopenharmony_ci	 */
5468c2ecf20Sopenharmony_ci	if (unlikely(!ieee80211_sdata_running(sdata)))
5478c2ecf20Sopenharmony_ci		return -ENETDOWN;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
5508c2ecf20Sopenharmony_ci		    is_multicast_ether_addr(sta->sta.addr)))
5518c2ecf20Sopenharmony_ci		return -EINVAL;
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	/* The RCU read lock is required by rhashtable due to
5548c2ecf20Sopenharmony_ci	 * asynchronous resize/rehash.  We also require the mutex
5558c2ecf20Sopenharmony_ci	 * for correctness.
5568c2ecf20Sopenharmony_ci	 */
5578c2ecf20Sopenharmony_ci	rcu_read_lock();
5588c2ecf20Sopenharmony_ci	lockdep_assert_held(&sdata->local->sta_mtx);
5598c2ecf20Sopenharmony_ci	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
5608c2ecf20Sopenharmony_ci	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
5618c2ecf20Sopenharmony_ci		rcu_read_unlock();
5628c2ecf20Sopenharmony_ci		return -ENOTUNIQ;
5638c2ecf20Sopenharmony_ci	}
5648c2ecf20Sopenharmony_ci	rcu_read_unlock();
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	return 0;
5678c2ecf20Sopenharmony_ci}
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_cistatic int sta_info_insert_drv_state(struct ieee80211_local *local,
5708c2ecf20Sopenharmony_ci				     struct ieee80211_sub_if_data *sdata,
5718c2ecf20Sopenharmony_ci				     struct sta_info *sta)
5728c2ecf20Sopenharmony_ci{
5738c2ecf20Sopenharmony_ci	enum ieee80211_sta_state state;
5748c2ecf20Sopenharmony_ci	int err = 0;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
5778c2ecf20Sopenharmony_ci		err = drv_sta_state(local, sdata, sta, state, state + 1);
5788c2ecf20Sopenharmony_ci		if (err)
5798c2ecf20Sopenharmony_ci			break;
5808c2ecf20Sopenharmony_ci	}
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	if (!err) {
5838c2ecf20Sopenharmony_ci		/*
5848c2ecf20Sopenharmony_ci		 * Drivers using legacy sta_add/sta_remove callbacks only
5858c2ecf20Sopenharmony_ci		 * get uploaded set to true after sta_add is called.
5868c2ecf20Sopenharmony_ci		 */
5878c2ecf20Sopenharmony_ci		if (!local->ops->sta_add)
5888c2ecf20Sopenharmony_ci			sta->uploaded = true;
5898c2ecf20Sopenharmony_ci		return 0;
5908c2ecf20Sopenharmony_ci	}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
5938c2ecf20Sopenharmony_ci		sdata_info(sdata,
5948c2ecf20Sopenharmony_ci			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
5958c2ecf20Sopenharmony_ci			   sta->sta.addr, state + 1, err);
5968c2ecf20Sopenharmony_ci		err = 0;
5978c2ecf20Sopenharmony_ci	}
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	/* unwind on error */
6008c2ecf20Sopenharmony_ci	for (; state > IEEE80211_STA_NOTEXIST; state--)
6018c2ecf20Sopenharmony_ci		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	return err;
6048c2ecf20Sopenharmony_ci}
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_cistatic void
6078c2ecf20Sopenharmony_ciieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
6088c2ecf20Sopenharmony_ci{
6098c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
6108c2ecf20Sopenharmony_ci	bool allow_p2p_go_ps = sdata->vif.p2p;
6118c2ecf20Sopenharmony_ci	struct sta_info *sta;
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	rcu_read_lock();
6148c2ecf20Sopenharmony_ci	list_for_each_entry_rcu(sta, &local->sta_list, list) {
6158c2ecf20Sopenharmony_ci		if (sdata != sta->sdata ||
6168c2ecf20Sopenharmony_ci		    !test_sta_flag(sta, WLAN_STA_ASSOC))
6178c2ecf20Sopenharmony_ci			continue;
6188c2ecf20Sopenharmony_ci		if (!sta->sta.support_p2p_ps) {
6198c2ecf20Sopenharmony_ci			allow_p2p_go_ps = false;
6208c2ecf20Sopenharmony_ci			break;
6218c2ecf20Sopenharmony_ci		}
6228c2ecf20Sopenharmony_ci	}
6238c2ecf20Sopenharmony_ci	rcu_read_unlock();
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
6268c2ecf20Sopenharmony_ci		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
6278c2ecf20Sopenharmony_ci		ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS);
6288c2ecf20Sopenharmony_ci	}
6298c2ecf20Sopenharmony_ci}
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci/*
6328c2ecf20Sopenharmony_ci * should be called with sta_mtx locked
6338c2ecf20Sopenharmony_ci * this function replaces the mutex lock
6348c2ecf20Sopenharmony_ci * with a RCU lock
6358c2ecf20Sopenharmony_ci */
6368c2ecf20Sopenharmony_cistatic int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
6378c2ecf20Sopenharmony_ci{
6388c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sta->local;
6398c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata = sta->sdata;
6408c2ecf20Sopenharmony_ci	struct station_info *sinfo = NULL;
6418c2ecf20Sopenharmony_ci	int err = 0;
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	lockdep_assert_held(&local->sta_mtx);
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	/* check if STA exists already */
6468c2ecf20Sopenharmony_ci	if (sta_info_get_bss(sdata, sta->sta.addr)) {
6478c2ecf20Sopenharmony_ci		err = -EEXIST;
6488c2ecf20Sopenharmony_ci		goto out_cleanup;
6498c2ecf20Sopenharmony_ci	}
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
6528c2ecf20Sopenharmony_ci	if (!sinfo) {
6538c2ecf20Sopenharmony_ci		err = -ENOMEM;
6548c2ecf20Sopenharmony_ci		goto out_cleanup;
6558c2ecf20Sopenharmony_ci	}
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	local->num_sta++;
6588c2ecf20Sopenharmony_ci	local->sta_generation++;
6598c2ecf20Sopenharmony_ci	smp_mb();
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	/* simplify things and don't accept BA sessions yet */
6628c2ecf20Sopenharmony_ci	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	/* make the station visible */
6658c2ecf20Sopenharmony_ci	err = sta_info_hash_add(local, sta);
6668c2ecf20Sopenharmony_ci	if (err)
6678c2ecf20Sopenharmony_ci		goto out_drop_sta;
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	list_add_tail_rcu(&sta->list, &local->sta_list);
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	/* notify driver */
6728c2ecf20Sopenharmony_ci	err = sta_info_insert_drv_state(local, sdata, sta);
6738c2ecf20Sopenharmony_ci	if (err)
6748c2ecf20Sopenharmony_ci		goto out_remove;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	set_sta_flag(sta, WLAN_STA_INSERTED);
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
6798c2ecf20Sopenharmony_ci		ieee80211_recalc_min_chandef(sta->sdata);
6808c2ecf20Sopenharmony_ci		if (!sta->sta.support_p2p_ps)
6818c2ecf20Sopenharmony_ci			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
6828c2ecf20Sopenharmony_ci	}
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	/* accept BA sessions now */
6858c2ecf20Sopenharmony_ci	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci	ieee80211_sta_debugfs_add(sta);
6888c2ecf20Sopenharmony_ci	rate_control_add_sta_debugfs(sta);
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	sinfo->generation = local->sta_generation;
6918c2ecf20Sopenharmony_ci	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
6928c2ecf20Sopenharmony_ci	kfree(sinfo);
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	/* move reference to rcu-protected */
6978c2ecf20Sopenharmony_ci	rcu_read_lock();
6988c2ecf20Sopenharmony_ci	mutex_unlock(&local->sta_mtx);
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	if (ieee80211_vif_is_mesh(&sdata->vif))
7018c2ecf20Sopenharmony_ci		mesh_accept_plinks_update(sdata);
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	ieee80211_check_fast_xmit(sta);
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	return 0;
7068c2ecf20Sopenharmony_ci out_remove:
7078c2ecf20Sopenharmony_ci	sta_info_hash_del(local, sta);
7088c2ecf20Sopenharmony_ci	list_del_rcu(&sta->list);
7098c2ecf20Sopenharmony_ci out_drop_sta:
7108c2ecf20Sopenharmony_ci	local->num_sta--;
7118c2ecf20Sopenharmony_ci	synchronize_net();
7128c2ecf20Sopenharmony_ci out_cleanup:
7138c2ecf20Sopenharmony_ci	cleanup_single_sta(sta);
7148c2ecf20Sopenharmony_ci	mutex_unlock(&local->sta_mtx);
7158c2ecf20Sopenharmony_ci	kfree(sinfo);
7168c2ecf20Sopenharmony_ci	rcu_read_lock();
7178c2ecf20Sopenharmony_ci	return err;
7188c2ecf20Sopenharmony_ci}
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ciint sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
7218c2ecf20Sopenharmony_ci{
7228c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sta->local;
7238c2ecf20Sopenharmony_ci	int err;
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	might_sleep();
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	mutex_lock(&local->sta_mtx);
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	err = sta_info_insert_check(sta);
7308c2ecf20Sopenharmony_ci	if (err) {
7318c2ecf20Sopenharmony_ci		sta_info_free(local, sta);
7328c2ecf20Sopenharmony_ci		mutex_unlock(&local->sta_mtx);
7338c2ecf20Sopenharmony_ci		rcu_read_lock();
7348c2ecf20Sopenharmony_ci		return err;
7358c2ecf20Sopenharmony_ci	}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	return sta_info_insert_finish(sta);
7388c2ecf20Sopenharmony_ci}
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ciint sta_info_insert(struct sta_info *sta)
7418c2ecf20Sopenharmony_ci{
7428c2ecf20Sopenharmony_ci	int err = sta_info_insert_rcu(sta);
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	rcu_read_unlock();
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	return err;
7478c2ecf20Sopenharmony_ci}
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_cistatic inline void __bss_tim_set(u8 *tim, u16 id)
7508c2ecf20Sopenharmony_ci{
7518c2ecf20Sopenharmony_ci	/*
7528c2ecf20Sopenharmony_ci	 * This format has been mandated by the IEEE specifications,
7538c2ecf20Sopenharmony_ci	 * so this line may not be changed to use the __set_bit() format.
7548c2ecf20Sopenharmony_ci	 */
7558c2ecf20Sopenharmony_ci	tim[id / 8] |= (1 << (id % 8));
7568c2ecf20Sopenharmony_ci}
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_cistatic inline void __bss_tim_clear(u8 *tim, u16 id)
7598c2ecf20Sopenharmony_ci{
7608c2ecf20Sopenharmony_ci	/*
7618c2ecf20Sopenharmony_ci	 * This format has been mandated by the IEEE specifications,
7628c2ecf20Sopenharmony_ci	 * so this line may not be changed to use the __clear_bit() format.
7638c2ecf20Sopenharmony_ci	 */
7648c2ecf20Sopenharmony_ci	tim[id / 8] &= ~(1 << (id % 8));
7658c2ecf20Sopenharmony_ci}
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_cistatic inline bool __bss_tim_get(u8 *tim, u16 id)
7688c2ecf20Sopenharmony_ci{
7698c2ecf20Sopenharmony_ci	/*
7708c2ecf20Sopenharmony_ci	 * This format has been mandated by the IEEE specifications,
7718c2ecf20Sopenharmony_ci	 * so this line may not be changed to use the test_bit() format.
7728c2ecf20Sopenharmony_ci	 */
7738c2ecf20Sopenharmony_ci	return tim[id / 8] & (1 << (id % 8));
7748c2ecf20Sopenharmony_ci}
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_cistatic unsigned long ieee80211_tids_for_ac(int ac)
7778c2ecf20Sopenharmony_ci{
7788c2ecf20Sopenharmony_ci	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
7798c2ecf20Sopenharmony_ci	switch (ac) {
7808c2ecf20Sopenharmony_ci	case IEEE80211_AC_VO:
7818c2ecf20Sopenharmony_ci		return BIT(6) | BIT(7);
7828c2ecf20Sopenharmony_ci	case IEEE80211_AC_VI:
7838c2ecf20Sopenharmony_ci		return BIT(4) | BIT(5);
7848c2ecf20Sopenharmony_ci	case IEEE80211_AC_BE:
7858c2ecf20Sopenharmony_ci		return BIT(0) | BIT(3);
7868c2ecf20Sopenharmony_ci	case IEEE80211_AC_BK:
7878c2ecf20Sopenharmony_ci		return BIT(1) | BIT(2);
7888c2ecf20Sopenharmony_ci	default:
7898c2ecf20Sopenharmony_ci		WARN_ON(1);
7908c2ecf20Sopenharmony_ci		return 0;
7918c2ecf20Sopenharmony_ci	}
7928c2ecf20Sopenharmony_ci}
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_cistatic void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
7958c2ecf20Sopenharmony_ci{
7968c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sta->local;
7978c2ecf20Sopenharmony_ci	struct ps_data *ps;
7988c2ecf20Sopenharmony_ci	bool indicate_tim = false;
7998c2ecf20Sopenharmony_ci	u8 ignore_for_tim = sta->sta.uapsd_queues;
8008c2ecf20Sopenharmony_ci	int ac;
8018c2ecf20Sopenharmony_ci	u16 id = sta->sta.aid;
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
8048c2ecf20Sopenharmony_ci	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
8058c2ecf20Sopenharmony_ci		if (WARN_ON_ONCE(!sta->sdata->bss))
8068c2ecf20Sopenharmony_ci			return;
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci		ps = &sta->sdata->bss->ps;
8098c2ecf20Sopenharmony_ci#ifdef CONFIG_MAC80211_MESH
8108c2ecf20Sopenharmony_ci	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
8118c2ecf20Sopenharmony_ci		ps = &sta->sdata->u.mesh.ps;
8128c2ecf20Sopenharmony_ci#endif
8138c2ecf20Sopenharmony_ci	} else {
8148c2ecf20Sopenharmony_ci		return;
8158c2ecf20Sopenharmony_ci	}
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	/* No need to do anything if the driver does all */
8188c2ecf20Sopenharmony_ci	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
8198c2ecf20Sopenharmony_ci		return;
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	if (sta->dead)
8228c2ecf20Sopenharmony_ci		goto done;
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	/*
8258c2ecf20Sopenharmony_ci	 * If all ACs are delivery-enabled then we should build
8268c2ecf20Sopenharmony_ci	 * the TIM bit for all ACs anyway; if only some are then
8278c2ecf20Sopenharmony_ci	 * we ignore those and build the TIM bit using only the
8288c2ecf20Sopenharmony_ci	 * non-enabled ones.
8298c2ecf20Sopenharmony_ci	 */
8308c2ecf20Sopenharmony_ci	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
8318c2ecf20Sopenharmony_ci		ignore_for_tim = 0;
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	if (ignore_pending)
8348c2ecf20Sopenharmony_ci		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
8378c2ecf20Sopenharmony_ci		unsigned long tids;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
8408c2ecf20Sopenharmony_ci			continue;
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
8438c2ecf20Sopenharmony_ci				!skb_queue_empty(&sta->ps_tx_buf[ac]);
8448c2ecf20Sopenharmony_ci		if (indicate_tim)
8458c2ecf20Sopenharmony_ci			break;
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci		tids = ieee80211_tids_for_ac(ac);
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci		indicate_tim |=
8508c2ecf20Sopenharmony_ci			sta->driver_buffered_tids & tids;
8518c2ecf20Sopenharmony_ci		indicate_tim |=
8528c2ecf20Sopenharmony_ci			sta->txq_buffered_tids & tids;
8538c2ecf20Sopenharmony_ci	}
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci done:
8568c2ecf20Sopenharmony_ci	spin_lock_bh(&local->tim_lock);
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci	if (indicate_tim == __bss_tim_get(ps->tim, id))
8598c2ecf20Sopenharmony_ci		goto out_unlock;
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	if (indicate_tim)
8628c2ecf20Sopenharmony_ci		__bss_tim_set(ps->tim, id);
8638c2ecf20Sopenharmony_ci	else
8648c2ecf20Sopenharmony_ci		__bss_tim_clear(ps->tim, id);
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
8678c2ecf20Sopenharmony_ci		local->tim_in_locked_section = true;
8688c2ecf20Sopenharmony_ci		drv_set_tim(local, &sta->sta, indicate_tim);
8698c2ecf20Sopenharmony_ci		local->tim_in_locked_section = false;
8708c2ecf20Sopenharmony_ci	}
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ciout_unlock:
8738c2ecf20Sopenharmony_ci	spin_unlock_bh(&local->tim_lock);
8748c2ecf20Sopenharmony_ci}
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_civoid sta_info_recalc_tim(struct sta_info *sta)
8778c2ecf20Sopenharmony_ci{
8788c2ecf20Sopenharmony_ci	__sta_info_recalc_tim(sta, false);
8798c2ecf20Sopenharmony_ci}
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_cistatic bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
8828c2ecf20Sopenharmony_ci{
8838c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *info;
8848c2ecf20Sopenharmony_ci	int timeout;
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	if (!skb)
8878c2ecf20Sopenharmony_ci		return false;
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	info = IEEE80211_SKB_CB(skb);
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
8928c2ecf20Sopenharmony_ci	timeout = (sta->listen_interval *
8938c2ecf20Sopenharmony_ci		   sta->sdata->vif.bss_conf.beacon_int *
8948c2ecf20Sopenharmony_ci		   32 / 15625) * HZ;
8958c2ecf20Sopenharmony_ci	if (timeout < STA_TX_BUFFER_EXPIRE)
8968c2ecf20Sopenharmony_ci		timeout = STA_TX_BUFFER_EXPIRE;
8978c2ecf20Sopenharmony_ci	return time_after(jiffies, info->control.jiffies + timeout);
8988c2ecf20Sopenharmony_ci}
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_cistatic bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
9028c2ecf20Sopenharmony_ci						struct sta_info *sta, int ac)
9038c2ecf20Sopenharmony_ci{
9048c2ecf20Sopenharmony_ci	unsigned long flags;
9058c2ecf20Sopenharmony_ci	struct sk_buff *skb;
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	/*
9088c2ecf20Sopenharmony_ci	 * First check for frames that should expire on the filtered
9098c2ecf20Sopenharmony_ci	 * queue. Frames here were rejected by the driver and are on
9108c2ecf20Sopenharmony_ci	 * a separate queue to avoid reordering with normal PS-buffered
9118c2ecf20Sopenharmony_ci	 * frames. They also aren't accounted for right now in the
9128c2ecf20Sopenharmony_ci	 * total_ps_buffered counter.
9138c2ecf20Sopenharmony_ci	 */
9148c2ecf20Sopenharmony_ci	for (;;) {
9158c2ecf20Sopenharmony_ci		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
9168c2ecf20Sopenharmony_ci		skb = skb_peek(&sta->tx_filtered[ac]);
9178c2ecf20Sopenharmony_ci		if (sta_info_buffer_expired(sta, skb))
9188c2ecf20Sopenharmony_ci			skb = __skb_dequeue(&sta->tx_filtered[ac]);
9198c2ecf20Sopenharmony_ci		else
9208c2ecf20Sopenharmony_ci			skb = NULL;
9218c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci		/*
9248c2ecf20Sopenharmony_ci		 * Frames are queued in order, so if this one
9258c2ecf20Sopenharmony_ci		 * hasn't expired yet we can stop testing. If
9268c2ecf20Sopenharmony_ci		 * we actually reached the end of the queue we
9278c2ecf20Sopenharmony_ci		 * also need to stop, of course.
9288c2ecf20Sopenharmony_ci		 */
9298c2ecf20Sopenharmony_ci		if (!skb)
9308c2ecf20Sopenharmony_ci			break;
9318c2ecf20Sopenharmony_ci		ieee80211_free_txskb(&local->hw, skb);
9328c2ecf20Sopenharmony_ci	}
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	/*
9358c2ecf20Sopenharmony_ci	 * Now also check the normal PS-buffered queue, this will
9368c2ecf20Sopenharmony_ci	 * only find something if the filtered queue was emptied
9378c2ecf20Sopenharmony_ci	 * since the filtered frames are all before the normal PS
9388c2ecf20Sopenharmony_ci	 * buffered frames.
9398c2ecf20Sopenharmony_ci	 */
9408c2ecf20Sopenharmony_ci	for (;;) {
9418c2ecf20Sopenharmony_ci		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
9428c2ecf20Sopenharmony_ci		skb = skb_peek(&sta->ps_tx_buf[ac]);
9438c2ecf20Sopenharmony_ci		if (sta_info_buffer_expired(sta, skb))
9448c2ecf20Sopenharmony_ci			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
9458c2ecf20Sopenharmony_ci		else
9468c2ecf20Sopenharmony_ci			skb = NULL;
9478c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci		/*
9508c2ecf20Sopenharmony_ci		 * frames are queued in order, so if this one
9518c2ecf20Sopenharmony_ci		 * hasn't expired yet (or we reached the end of
9528c2ecf20Sopenharmony_ci		 * the queue) we can stop testing
9538c2ecf20Sopenharmony_ci		 */
9548c2ecf20Sopenharmony_ci		if (!skb)
9558c2ecf20Sopenharmony_ci			break;
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci		local->total_ps_buffered--;
9588c2ecf20Sopenharmony_ci		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
9598c2ecf20Sopenharmony_ci		       sta->sta.addr);
9608c2ecf20Sopenharmony_ci		ieee80211_free_txskb(&local->hw, skb);
9618c2ecf20Sopenharmony_ci	}
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci	/*
9648c2ecf20Sopenharmony_ci	 * Finally, recalculate the TIM bit for this station -- it might
9658c2ecf20Sopenharmony_ci	 * now be clear because the station was too slow to retrieve its
9668c2ecf20Sopenharmony_ci	 * frames.
9678c2ecf20Sopenharmony_ci	 */
9688c2ecf20Sopenharmony_ci	sta_info_recalc_tim(sta);
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	/*
9718c2ecf20Sopenharmony_ci	 * Return whether there are any frames still buffered, this is
9728c2ecf20Sopenharmony_ci	 * used to check whether the cleanup timer still needs to run,
9738c2ecf20Sopenharmony_ci	 * if there are no frames we don't need to rearm the timer.
9748c2ecf20Sopenharmony_ci	 */
9758c2ecf20Sopenharmony_ci	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
9768c2ecf20Sopenharmony_ci		 skb_queue_empty(&sta->tx_filtered[ac]));
9778c2ecf20Sopenharmony_ci}
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_cistatic bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
9808c2ecf20Sopenharmony_ci					     struct sta_info *sta)
9818c2ecf20Sopenharmony_ci{
9828c2ecf20Sopenharmony_ci	bool have_buffered = false;
9838c2ecf20Sopenharmony_ci	int ac;
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	/* This is only necessary for stations on BSS/MBSS interfaces */
9868c2ecf20Sopenharmony_ci	if (!sta->sdata->bss &&
9878c2ecf20Sopenharmony_ci	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
9888c2ecf20Sopenharmony_ci		return false;
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
9918c2ecf20Sopenharmony_ci		have_buffered |=
9928c2ecf20Sopenharmony_ci			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci	return have_buffered;
9958c2ecf20Sopenharmony_ci}
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_cistatic int __must_check __sta_info_destroy_part1(struct sta_info *sta)
9988c2ecf20Sopenharmony_ci{
9998c2ecf20Sopenharmony_ci	struct ieee80211_local *local;
10008c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata;
10018c2ecf20Sopenharmony_ci	int ret;
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	might_sleep();
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci	if (!sta)
10068c2ecf20Sopenharmony_ci		return -ENOENT;
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	local = sta->local;
10098c2ecf20Sopenharmony_ci	sdata = sta->sdata;
10108c2ecf20Sopenharmony_ci
10118c2ecf20Sopenharmony_ci	lockdep_assert_held(&local->sta_mtx);
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ci	/*
10148c2ecf20Sopenharmony_ci	 * Before removing the station from the driver and
10158c2ecf20Sopenharmony_ci	 * rate control, it might still start new aggregation
10168c2ecf20Sopenharmony_ci	 * sessions -- block that to make sure the tear-down
10178c2ecf20Sopenharmony_ci	 * will be sufficient.
10188c2ecf20Sopenharmony_ci	 */
10198c2ecf20Sopenharmony_ci	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
10208c2ecf20Sopenharmony_ci	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	/*
10238c2ecf20Sopenharmony_ci	 * Before removing the station from the driver there might be pending
10248c2ecf20Sopenharmony_ci	 * rx frames on RSS queues sent prior to the disassociation - wait for
10258c2ecf20Sopenharmony_ci	 * all such frames to be processed.
10268c2ecf20Sopenharmony_ci	 */
10278c2ecf20Sopenharmony_ci	drv_sync_rx_queues(local, sta);
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	ret = sta_info_hash_del(local, sta);
10308c2ecf20Sopenharmony_ci	if (WARN_ON(ret))
10318c2ecf20Sopenharmony_ci		return ret;
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci	/*
10348c2ecf20Sopenharmony_ci	 * for TDLS peers, make sure to return to the base channel before
10358c2ecf20Sopenharmony_ci	 * removal.
10368c2ecf20Sopenharmony_ci	 */
10378c2ecf20Sopenharmony_ci	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
10388c2ecf20Sopenharmony_ci		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
10398c2ecf20Sopenharmony_ci		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
10408c2ecf20Sopenharmony_ci	}
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci	list_del_rcu(&sta->list);
10438c2ecf20Sopenharmony_ci	sta->removed = true;
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci	if (sta->uploaded)
10468c2ecf20Sopenharmony_ci		drv_sta_pre_rcu_remove(local, sta->sdata, sta);
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_ci	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
10498c2ecf20Sopenharmony_ci	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
10508c2ecf20Sopenharmony_ci		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci	return 0;
10538c2ecf20Sopenharmony_ci}
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_cistatic void __sta_info_destroy_part2(struct sta_info *sta)
10568c2ecf20Sopenharmony_ci{
10578c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sta->local;
10588c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata = sta->sdata;
10598c2ecf20Sopenharmony_ci	struct station_info *sinfo;
10608c2ecf20Sopenharmony_ci	int ret;
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	/*
10638c2ecf20Sopenharmony_ci	 * NOTE: This assumes at least synchronize_net() was done
10648c2ecf20Sopenharmony_ci	 *	 after _part1 and before _part2!
10658c2ecf20Sopenharmony_ci	 */
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_ci	might_sleep();
10688c2ecf20Sopenharmony_ci	lockdep_assert_held(&local->sta_mtx);
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_ci	if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
10718c2ecf20Sopenharmony_ci		ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
10728c2ecf20Sopenharmony_ci		WARN_ON_ONCE(ret);
10738c2ecf20Sopenharmony_ci	}
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci	/* now keys can no longer be reached */
10768c2ecf20Sopenharmony_ci	ieee80211_free_sta_keys(local, sta);
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_ci	/* disable TIM bit - last chance to tell driver */
10798c2ecf20Sopenharmony_ci	__sta_info_recalc_tim(sta, true);
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ci	sta->dead = true;
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_ci	local->num_sta--;
10848c2ecf20Sopenharmony_ci	local->sta_generation++;
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci	while (sta->sta_state > IEEE80211_STA_NONE) {
10878c2ecf20Sopenharmony_ci		ret = sta_info_move_state(sta, sta->sta_state - 1);
10888c2ecf20Sopenharmony_ci		if (ret) {
10898c2ecf20Sopenharmony_ci			WARN_ON_ONCE(1);
10908c2ecf20Sopenharmony_ci			break;
10918c2ecf20Sopenharmony_ci		}
10928c2ecf20Sopenharmony_ci	}
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_ci	if (sta->uploaded) {
10958c2ecf20Sopenharmony_ci		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
10968c2ecf20Sopenharmony_ci				    IEEE80211_STA_NOTEXIST);
10978c2ecf20Sopenharmony_ci		WARN_ON_ONCE(ret != 0);
10988c2ecf20Sopenharmony_ci	}
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
11018c2ecf20Sopenharmony_ci
11028c2ecf20Sopenharmony_ci	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
11038c2ecf20Sopenharmony_ci	if (sinfo)
11048c2ecf20Sopenharmony_ci		sta_set_sinfo(sta, sinfo, true);
11058c2ecf20Sopenharmony_ci	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
11068c2ecf20Sopenharmony_ci	kfree(sinfo);
11078c2ecf20Sopenharmony_ci
11088c2ecf20Sopenharmony_ci	ieee80211_sta_debugfs_remove(sta);
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci	ieee80211_destroy_frag_cache(&sta->frags);
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_ci	cleanup_single_sta(sta);
11138c2ecf20Sopenharmony_ci}
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ciint __must_check __sta_info_destroy(struct sta_info *sta)
11168c2ecf20Sopenharmony_ci{
11178c2ecf20Sopenharmony_ci	int err = __sta_info_destroy_part1(sta);
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ci	if (err)
11208c2ecf20Sopenharmony_ci		return err;
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_ci	synchronize_net();
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_ci	__sta_info_destroy_part2(sta);
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_ci	return 0;
11278c2ecf20Sopenharmony_ci}
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ciint sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
11308c2ecf20Sopenharmony_ci{
11318c2ecf20Sopenharmony_ci	struct sta_info *sta;
11328c2ecf20Sopenharmony_ci	int ret;
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_ci	mutex_lock(&sdata->local->sta_mtx);
11358c2ecf20Sopenharmony_ci	sta = sta_info_get(sdata, addr);
11368c2ecf20Sopenharmony_ci	ret = __sta_info_destroy(sta);
11378c2ecf20Sopenharmony_ci	mutex_unlock(&sdata->local->sta_mtx);
11388c2ecf20Sopenharmony_ci
11398c2ecf20Sopenharmony_ci	return ret;
11408c2ecf20Sopenharmony_ci}
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ciint sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
11438c2ecf20Sopenharmony_ci			      const u8 *addr)
11448c2ecf20Sopenharmony_ci{
11458c2ecf20Sopenharmony_ci	struct sta_info *sta;
11468c2ecf20Sopenharmony_ci	int ret;
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci	mutex_lock(&sdata->local->sta_mtx);
11498c2ecf20Sopenharmony_ci	sta = sta_info_get_bss(sdata, addr);
11508c2ecf20Sopenharmony_ci	ret = __sta_info_destroy(sta);
11518c2ecf20Sopenharmony_ci	mutex_unlock(&sdata->local->sta_mtx);
11528c2ecf20Sopenharmony_ci
11538c2ecf20Sopenharmony_ci	return ret;
11548c2ecf20Sopenharmony_ci}
11558c2ecf20Sopenharmony_ci
11568c2ecf20Sopenharmony_cistatic void sta_info_cleanup(struct timer_list *t)
11578c2ecf20Sopenharmony_ci{
11588c2ecf20Sopenharmony_ci	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
11598c2ecf20Sopenharmony_ci	struct sta_info *sta;
11608c2ecf20Sopenharmony_ci	bool timer_needed = false;
11618c2ecf20Sopenharmony_ci
11628c2ecf20Sopenharmony_ci	rcu_read_lock();
11638c2ecf20Sopenharmony_ci	list_for_each_entry_rcu(sta, &local->sta_list, list)
11648c2ecf20Sopenharmony_ci		if (sta_info_cleanup_expire_buffered(local, sta))
11658c2ecf20Sopenharmony_ci			timer_needed = true;
11668c2ecf20Sopenharmony_ci	rcu_read_unlock();
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ci	if (local->quiescing)
11698c2ecf20Sopenharmony_ci		return;
11708c2ecf20Sopenharmony_ci
11718c2ecf20Sopenharmony_ci	if (!timer_needed)
11728c2ecf20Sopenharmony_ci		return;
11738c2ecf20Sopenharmony_ci
11748c2ecf20Sopenharmony_ci	mod_timer(&local->sta_cleanup,
11758c2ecf20Sopenharmony_ci		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
11768c2ecf20Sopenharmony_ci}
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_ciint sta_info_init(struct ieee80211_local *local)
11798c2ecf20Sopenharmony_ci{
11808c2ecf20Sopenharmony_ci	int err;
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci	err = rhltable_init(&local->sta_hash, &sta_rht_params);
11838c2ecf20Sopenharmony_ci	if (err)
11848c2ecf20Sopenharmony_ci		return err;
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_ci	spin_lock_init(&local->tim_lock);
11878c2ecf20Sopenharmony_ci	mutex_init(&local->sta_mtx);
11888c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&local->sta_list);
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_ci	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
11918c2ecf20Sopenharmony_ci	return 0;
11928c2ecf20Sopenharmony_ci}
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_civoid sta_info_stop(struct ieee80211_local *local)
11958c2ecf20Sopenharmony_ci{
11968c2ecf20Sopenharmony_ci	del_timer_sync(&local->sta_cleanup);
11978c2ecf20Sopenharmony_ci	rhltable_destroy(&local->sta_hash);
11988c2ecf20Sopenharmony_ci}
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_ci
12018c2ecf20Sopenharmony_ciint __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
12028c2ecf20Sopenharmony_ci{
12038c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
12048c2ecf20Sopenharmony_ci	struct sta_info *sta, *tmp;
12058c2ecf20Sopenharmony_ci	LIST_HEAD(free_list);
12068c2ecf20Sopenharmony_ci	int ret = 0;
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_ci	might_sleep();
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_ci	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
12118c2ecf20Sopenharmony_ci	WARN_ON(vlans && !sdata->bss);
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci	mutex_lock(&local->sta_mtx);
12148c2ecf20Sopenharmony_ci	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
12158c2ecf20Sopenharmony_ci		if (sdata == sta->sdata ||
12168c2ecf20Sopenharmony_ci		    (vlans && sdata->bss == sta->sdata->bss)) {
12178c2ecf20Sopenharmony_ci			if (!WARN_ON(__sta_info_destroy_part1(sta)))
12188c2ecf20Sopenharmony_ci				list_add(&sta->free_list, &free_list);
12198c2ecf20Sopenharmony_ci			ret++;
12208c2ecf20Sopenharmony_ci		}
12218c2ecf20Sopenharmony_ci	}
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci	if (!list_empty(&free_list)) {
12248c2ecf20Sopenharmony_ci		synchronize_net();
12258c2ecf20Sopenharmony_ci		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
12268c2ecf20Sopenharmony_ci			__sta_info_destroy_part2(sta);
12278c2ecf20Sopenharmony_ci	}
12288c2ecf20Sopenharmony_ci	mutex_unlock(&local->sta_mtx);
12298c2ecf20Sopenharmony_ci
12308c2ecf20Sopenharmony_ci	return ret;
12318c2ecf20Sopenharmony_ci}
12328c2ecf20Sopenharmony_ci
12338c2ecf20Sopenharmony_civoid ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
12348c2ecf20Sopenharmony_ci			  unsigned long exp_time)
12358c2ecf20Sopenharmony_ci{
12368c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
12378c2ecf20Sopenharmony_ci	struct sta_info *sta, *tmp;
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci	mutex_lock(&local->sta_mtx);
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
12428c2ecf20Sopenharmony_ci		unsigned long last_active = ieee80211_sta_last_active(sta);
12438c2ecf20Sopenharmony_ci
12448c2ecf20Sopenharmony_ci		if (sdata != sta->sdata)
12458c2ecf20Sopenharmony_ci			continue;
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci		if (time_is_before_jiffies(last_active + exp_time)) {
12488c2ecf20Sopenharmony_ci			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
12498c2ecf20Sopenharmony_ci				sta->sta.addr);
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_ci			if (ieee80211_vif_is_mesh(&sdata->vif) &&
12528c2ecf20Sopenharmony_ci			    test_sta_flag(sta, WLAN_STA_PS_STA))
12538c2ecf20Sopenharmony_ci				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_ci			WARN_ON(__sta_info_destroy(sta));
12568c2ecf20Sopenharmony_ci		}
12578c2ecf20Sopenharmony_ci	}
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci	mutex_unlock(&local->sta_mtx);
12608c2ecf20Sopenharmony_ci}
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_cistruct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
12638c2ecf20Sopenharmony_ci						   const u8 *addr,
12648c2ecf20Sopenharmony_ci						   const u8 *localaddr)
12658c2ecf20Sopenharmony_ci{
12668c2ecf20Sopenharmony_ci	struct ieee80211_local *local = hw_to_local(hw);
12678c2ecf20Sopenharmony_ci	struct rhlist_head *tmp;
12688c2ecf20Sopenharmony_ci	struct sta_info *sta;
12698c2ecf20Sopenharmony_ci
12708c2ecf20Sopenharmony_ci	/*
12718c2ecf20Sopenharmony_ci	 * Just return a random station if localaddr is NULL
12728c2ecf20Sopenharmony_ci	 * ... first in list.
12738c2ecf20Sopenharmony_ci	 */
12748c2ecf20Sopenharmony_ci	for_each_sta_info(local, addr, sta, tmp) {
12758c2ecf20Sopenharmony_ci		if (localaddr &&
12768c2ecf20Sopenharmony_ci		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
12778c2ecf20Sopenharmony_ci			continue;
12788c2ecf20Sopenharmony_ci		if (!sta->uploaded)
12798c2ecf20Sopenharmony_ci			return NULL;
12808c2ecf20Sopenharmony_ci		return &sta->sta;
12818c2ecf20Sopenharmony_ci	}
12828c2ecf20Sopenharmony_ci
12838c2ecf20Sopenharmony_ci	return NULL;
12848c2ecf20Sopenharmony_ci}
12858c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
12868c2ecf20Sopenharmony_ci
12878c2ecf20Sopenharmony_cistruct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
12888c2ecf20Sopenharmony_ci					 const u8 *addr)
12898c2ecf20Sopenharmony_ci{
12908c2ecf20Sopenharmony_ci	struct sta_info *sta;
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	if (!vif)
12938c2ecf20Sopenharmony_ci		return NULL;
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
12968c2ecf20Sopenharmony_ci	if (!sta)
12978c2ecf20Sopenharmony_ci		return NULL;
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_ci	if (!sta->uploaded)
13008c2ecf20Sopenharmony_ci		return NULL;
13018c2ecf20Sopenharmony_ci
13028c2ecf20Sopenharmony_ci	return &sta->sta;
13038c2ecf20Sopenharmony_ci}
13048c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ieee80211_find_sta);
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_ci/* powersave support code */
13078c2ecf20Sopenharmony_civoid ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
13088c2ecf20Sopenharmony_ci{
13098c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata = sta->sdata;
13108c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
13118c2ecf20Sopenharmony_ci	struct sk_buff_head pending;
13128c2ecf20Sopenharmony_ci	int filtered = 0, buffered = 0, ac, i;
13138c2ecf20Sopenharmony_ci	unsigned long flags;
13148c2ecf20Sopenharmony_ci	struct ps_data *ps;
13158c2ecf20Sopenharmony_ci
13168c2ecf20Sopenharmony_ci	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
13178c2ecf20Sopenharmony_ci		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
13188c2ecf20Sopenharmony_ci				     u.ap);
13198c2ecf20Sopenharmony_ci
13208c2ecf20Sopenharmony_ci	if (sdata->vif.type == NL80211_IFTYPE_AP)
13218c2ecf20Sopenharmony_ci		ps = &sdata->bss->ps;
13228c2ecf20Sopenharmony_ci	else if (ieee80211_vif_is_mesh(&sdata->vif))
13238c2ecf20Sopenharmony_ci		ps = &sdata->u.mesh.ps;
13248c2ecf20Sopenharmony_ci	else
13258c2ecf20Sopenharmony_ci		return;
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_ci	clear_sta_flag(sta, WLAN_STA_SP);
13288c2ecf20Sopenharmony_ci
13298c2ecf20Sopenharmony_ci	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
13308c2ecf20Sopenharmony_ci	sta->driver_buffered_tids = 0;
13318c2ecf20Sopenharmony_ci	sta->txq_buffered_tids = 0;
13328c2ecf20Sopenharmony_ci
13338c2ecf20Sopenharmony_ci	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
13348c2ecf20Sopenharmony_ci		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
13358c2ecf20Sopenharmony_ci
13368c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
13378c2ecf20Sopenharmony_ci		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
13388c2ecf20Sopenharmony_ci			continue;
13398c2ecf20Sopenharmony_ci
13408c2ecf20Sopenharmony_ci		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
13418c2ecf20Sopenharmony_ci	}
13428c2ecf20Sopenharmony_ci
13438c2ecf20Sopenharmony_ci	skb_queue_head_init(&pending);
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_ci	/* sync with ieee80211_tx_h_unicast_ps_buf */
13468c2ecf20Sopenharmony_ci	spin_lock_bh(&sta->ps_lock);
13478c2ecf20Sopenharmony_ci	/* Send all buffered frames to the station */
13488c2ecf20Sopenharmony_ci	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
13498c2ecf20Sopenharmony_ci		int count = skb_queue_len(&pending), tmp;
13508c2ecf20Sopenharmony_ci
13518c2ecf20Sopenharmony_ci		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
13528c2ecf20Sopenharmony_ci		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
13538c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
13548c2ecf20Sopenharmony_ci		tmp = skb_queue_len(&pending);
13558c2ecf20Sopenharmony_ci		filtered += tmp - count;
13568c2ecf20Sopenharmony_ci		count = tmp;
13578c2ecf20Sopenharmony_ci
13588c2ecf20Sopenharmony_ci		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
13598c2ecf20Sopenharmony_ci		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
13608c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
13618c2ecf20Sopenharmony_ci		tmp = skb_queue_len(&pending);
13628c2ecf20Sopenharmony_ci		buffered += tmp - count;
13638c2ecf20Sopenharmony_ci	}
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci	ieee80211_add_pending_skbs(local, &pending);
13668c2ecf20Sopenharmony_ci
13678c2ecf20Sopenharmony_ci	/* now we're no longer in the deliver code */
13688c2ecf20Sopenharmony_ci	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
13698c2ecf20Sopenharmony_ci
13708c2ecf20Sopenharmony_ci	/* The station might have polled and then woken up before we responded,
13718c2ecf20Sopenharmony_ci	 * so clear these flags now to avoid them sticking around.
13728c2ecf20Sopenharmony_ci	 */
13738c2ecf20Sopenharmony_ci	clear_sta_flag(sta, WLAN_STA_PSPOLL);
13748c2ecf20Sopenharmony_ci	clear_sta_flag(sta, WLAN_STA_UAPSD);
13758c2ecf20Sopenharmony_ci	spin_unlock_bh(&sta->ps_lock);
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ci	atomic_dec(&ps->num_sta_ps);
13788c2ecf20Sopenharmony_ci
13798c2ecf20Sopenharmony_ci	local->total_ps_buffered -= buffered;
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ci	sta_info_recalc_tim(sta);
13828c2ecf20Sopenharmony_ci
13838c2ecf20Sopenharmony_ci	ps_dbg(sdata,
13848c2ecf20Sopenharmony_ci	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
13858c2ecf20Sopenharmony_ci	       sta->sta.addr, sta->sta.aid, filtered, buffered);
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_ci	ieee80211_check_fast_xmit(sta);
13888c2ecf20Sopenharmony_ci}
13898c2ecf20Sopenharmony_ci
13908c2ecf20Sopenharmony_cistatic void ieee80211_send_null_response(struct sta_info *sta, int tid,
13918c2ecf20Sopenharmony_ci					 enum ieee80211_frame_release_type reason,
13928c2ecf20Sopenharmony_ci					 bool call_driver, bool more_data)
13938c2ecf20Sopenharmony_ci{
13948c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata = sta->sdata;
13958c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
13968c2ecf20Sopenharmony_ci	struct ieee80211_qos_hdr *nullfunc;
13978c2ecf20Sopenharmony_ci	struct sk_buff *skb;
13988c2ecf20Sopenharmony_ci	int size = sizeof(*nullfunc);
13998c2ecf20Sopenharmony_ci	__le16 fc;
14008c2ecf20Sopenharmony_ci	bool qos = sta->sta.wme;
14018c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *info;
14028c2ecf20Sopenharmony_ci	struct ieee80211_chanctx_conf *chanctx_conf;
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_ci	if (qos) {
14058c2ecf20Sopenharmony_ci		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
14068c2ecf20Sopenharmony_ci				 IEEE80211_STYPE_QOS_NULLFUNC |
14078c2ecf20Sopenharmony_ci				 IEEE80211_FCTL_FROMDS);
14088c2ecf20Sopenharmony_ci	} else {
14098c2ecf20Sopenharmony_ci		size -= 2;
14108c2ecf20Sopenharmony_ci		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
14118c2ecf20Sopenharmony_ci				 IEEE80211_STYPE_NULLFUNC |
14128c2ecf20Sopenharmony_ci				 IEEE80211_FCTL_FROMDS);
14138c2ecf20Sopenharmony_ci	}
14148c2ecf20Sopenharmony_ci
14158c2ecf20Sopenharmony_ci	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
14168c2ecf20Sopenharmony_ci	if (!skb)
14178c2ecf20Sopenharmony_ci		return;
14188c2ecf20Sopenharmony_ci
14198c2ecf20Sopenharmony_ci	skb_reserve(skb, local->hw.extra_tx_headroom);
14208c2ecf20Sopenharmony_ci
14218c2ecf20Sopenharmony_ci	nullfunc = skb_put(skb, size);
14228c2ecf20Sopenharmony_ci	nullfunc->frame_control = fc;
14238c2ecf20Sopenharmony_ci	nullfunc->duration_id = 0;
14248c2ecf20Sopenharmony_ci	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
14258c2ecf20Sopenharmony_ci	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
14268c2ecf20Sopenharmony_ci	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
14278c2ecf20Sopenharmony_ci	nullfunc->seq_ctrl = 0;
14288c2ecf20Sopenharmony_ci
14298c2ecf20Sopenharmony_ci	skb->priority = tid;
14308c2ecf20Sopenharmony_ci	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
14318c2ecf20Sopenharmony_ci	if (qos) {
14328c2ecf20Sopenharmony_ci		nullfunc->qos_ctrl = cpu_to_le16(tid);
14338c2ecf20Sopenharmony_ci
14348c2ecf20Sopenharmony_ci		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
14358c2ecf20Sopenharmony_ci			nullfunc->qos_ctrl |=
14368c2ecf20Sopenharmony_ci				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
14378c2ecf20Sopenharmony_ci			if (more_data)
14388c2ecf20Sopenharmony_ci				nullfunc->frame_control |=
14398c2ecf20Sopenharmony_ci					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
14408c2ecf20Sopenharmony_ci		}
14418c2ecf20Sopenharmony_ci	}
14428c2ecf20Sopenharmony_ci
14438c2ecf20Sopenharmony_ci	info = IEEE80211_SKB_CB(skb);
14448c2ecf20Sopenharmony_ci
14458c2ecf20Sopenharmony_ci	/*
14468c2ecf20Sopenharmony_ci	 * Tell TX path to send this frame even though the
14478c2ecf20Sopenharmony_ci	 * STA may still remain is PS mode after this frame
14488c2ecf20Sopenharmony_ci	 * exchange. Also set EOSP to indicate this packet
14498c2ecf20Sopenharmony_ci	 * ends the poll/service period.
14508c2ecf20Sopenharmony_ci	 */
14518c2ecf20Sopenharmony_ci	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
14528c2ecf20Sopenharmony_ci		       IEEE80211_TX_STATUS_EOSP |
14538c2ecf20Sopenharmony_ci		       IEEE80211_TX_CTL_REQ_TX_STATUS;
14548c2ecf20Sopenharmony_ci
14558c2ecf20Sopenharmony_ci	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
14568c2ecf20Sopenharmony_ci
14578c2ecf20Sopenharmony_ci	if (call_driver)
14588c2ecf20Sopenharmony_ci		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
14598c2ecf20Sopenharmony_ci					  reason, false);
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_ci	skb->dev = sdata->dev;
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci	rcu_read_lock();
14648c2ecf20Sopenharmony_ci	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
14658c2ecf20Sopenharmony_ci	if (WARN_ON(!chanctx_conf)) {
14668c2ecf20Sopenharmony_ci		rcu_read_unlock();
14678c2ecf20Sopenharmony_ci		kfree_skb(skb);
14688c2ecf20Sopenharmony_ci		return;
14698c2ecf20Sopenharmony_ci	}
14708c2ecf20Sopenharmony_ci
14718c2ecf20Sopenharmony_ci	info->band = chanctx_conf->def.chan->band;
14728c2ecf20Sopenharmony_ci	ieee80211_xmit(sdata, sta, skb);
14738c2ecf20Sopenharmony_ci	rcu_read_unlock();
14748c2ecf20Sopenharmony_ci}
14758c2ecf20Sopenharmony_ci
14768c2ecf20Sopenharmony_cistatic int find_highest_prio_tid(unsigned long tids)
14778c2ecf20Sopenharmony_ci{
14788c2ecf20Sopenharmony_ci	/* lower 3 TIDs aren't ordered perfectly */
14798c2ecf20Sopenharmony_ci	if (tids & 0xF8)
14808c2ecf20Sopenharmony_ci		return fls(tids) - 1;
14818c2ecf20Sopenharmony_ci	/* TID 0 is BE just like TID 3 */
14828c2ecf20Sopenharmony_ci	if (tids & BIT(0))
14838c2ecf20Sopenharmony_ci		return 0;
14848c2ecf20Sopenharmony_ci	return fls(tids) - 1;
14858c2ecf20Sopenharmony_ci}
14868c2ecf20Sopenharmony_ci
14878c2ecf20Sopenharmony_ci/* Indicates if the MORE_DATA bit should be set in the last
14888c2ecf20Sopenharmony_ci * frame obtained by ieee80211_sta_ps_get_frames.
14898c2ecf20Sopenharmony_ci * Note that driver_release_tids is relevant only if
14908c2ecf20Sopenharmony_ci * reason = IEEE80211_FRAME_RELEASE_PSPOLL
14918c2ecf20Sopenharmony_ci */
14928c2ecf20Sopenharmony_cistatic bool
14938c2ecf20Sopenharmony_ciieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
14948c2ecf20Sopenharmony_ci			   enum ieee80211_frame_release_type reason,
14958c2ecf20Sopenharmony_ci			   unsigned long driver_release_tids)
14968c2ecf20Sopenharmony_ci{
14978c2ecf20Sopenharmony_ci	int ac;
14988c2ecf20Sopenharmony_ci
14998c2ecf20Sopenharmony_ci	/* If the driver has data on more than one TID then
15008c2ecf20Sopenharmony_ci	 * certainly there's more data if we release just a
15018c2ecf20Sopenharmony_ci	 * single frame now (from a single TID). This will
15028c2ecf20Sopenharmony_ci	 * only happen for PS-Poll.
15038c2ecf20Sopenharmony_ci	 */
15048c2ecf20Sopenharmony_ci	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
15058c2ecf20Sopenharmony_ci	    hweight16(driver_release_tids) > 1)
15068c2ecf20Sopenharmony_ci		return true;
15078c2ecf20Sopenharmony_ci
15088c2ecf20Sopenharmony_ci	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
15098c2ecf20Sopenharmony_ci		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
15108c2ecf20Sopenharmony_ci			continue;
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
15138c2ecf20Sopenharmony_ci		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
15148c2ecf20Sopenharmony_ci			return true;
15158c2ecf20Sopenharmony_ci	}
15168c2ecf20Sopenharmony_ci
15178c2ecf20Sopenharmony_ci	return false;
15188c2ecf20Sopenharmony_ci}
15198c2ecf20Sopenharmony_ci
15208c2ecf20Sopenharmony_cistatic void
15218c2ecf20Sopenharmony_ciieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
15228c2ecf20Sopenharmony_ci			    enum ieee80211_frame_release_type reason,
15238c2ecf20Sopenharmony_ci			    struct sk_buff_head *frames,
15248c2ecf20Sopenharmony_ci			    unsigned long *driver_release_tids)
15258c2ecf20Sopenharmony_ci{
15268c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata = sta->sdata;
15278c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
15288c2ecf20Sopenharmony_ci	int ac;
15298c2ecf20Sopenharmony_ci
15308c2ecf20Sopenharmony_ci	/* Get response frame(s) and more data bit for the last one. */
15318c2ecf20Sopenharmony_ci	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
15328c2ecf20Sopenharmony_ci		unsigned long tids;
15338c2ecf20Sopenharmony_ci
15348c2ecf20Sopenharmony_ci		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
15358c2ecf20Sopenharmony_ci			continue;
15368c2ecf20Sopenharmony_ci
15378c2ecf20Sopenharmony_ci		tids = ieee80211_tids_for_ac(ac);
15388c2ecf20Sopenharmony_ci
15398c2ecf20Sopenharmony_ci		/* if we already have frames from software, then we can't also
15408c2ecf20Sopenharmony_ci		 * release from hardware queues
15418c2ecf20Sopenharmony_ci		 */
15428c2ecf20Sopenharmony_ci		if (skb_queue_empty(frames)) {
15438c2ecf20Sopenharmony_ci			*driver_release_tids |=
15448c2ecf20Sopenharmony_ci				sta->driver_buffered_tids & tids;
15458c2ecf20Sopenharmony_ci			*driver_release_tids |= sta->txq_buffered_tids & tids;
15468c2ecf20Sopenharmony_ci		}
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_ci		if (!*driver_release_tids) {
15498c2ecf20Sopenharmony_ci			struct sk_buff *skb;
15508c2ecf20Sopenharmony_ci
15518c2ecf20Sopenharmony_ci			while (n_frames > 0) {
15528c2ecf20Sopenharmony_ci				skb = skb_dequeue(&sta->tx_filtered[ac]);
15538c2ecf20Sopenharmony_ci				if (!skb) {
15548c2ecf20Sopenharmony_ci					skb = skb_dequeue(
15558c2ecf20Sopenharmony_ci						&sta->ps_tx_buf[ac]);
15568c2ecf20Sopenharmony_ci					if (skb)
15578c2ecf20Sopenharmony_ci						local->total_ps_buffered--;
15588c2ecf20Sopenharmony_ci				}
15598c2ecf20Sopenharmony_ci				if (!skb)
15608c2ecf20Sopenharmony_ci					break;
15618c2ecf20Sopenharmony_ci				n_frames--;
15628c2ecf20Sopenharmony_ci				__skb_queue_tail(frames, skb);
15638c2ecf20Sopenharmony_ci			}
15648c2ecf20Sopenharmony_ci		}
15658c2ecf20Sopenharmony_ci
15668c2ecf20Sopenharmony_ci		/* If we have more frames buffered on this AC, then abort the
15678c2ecf20Sopenharmony_ci		 * loop since we can't send more data from other ACs before
15688c2ecf20Sopenharmony_ci		 * the buffered frames from this.
15698c2ecf20Sopenharmony_ci		 */
15708c2ecf20Sopenharmony_ci		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
15718c2ecf20Sopenharmony_ci		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
15728c2ecf20Sopenharmony_ci			break;
15738c2ecf20Sopenharmony_ci	}
15748c2ecf20Sopenharmony_ci}
15758c2ecf20Sopenharmony_ci
15768c2ecf20Sopenharmony_cistatic void
15778c2ecf20Sopenharmony_ciieee80211_sta_ps_deliver_response(struct sta_info *sta,
15788c2ecf20Sopenharmony_ci				  int n_frames, u8 ignored_acs,
15798c2ecf20Sopenharmony_ci				  enum ieee80211_frame_release_type reason)
15808c2ecf20Sopenharmony_ci{
15818c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata = sta->sdata;
15828c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
15838c2ecf20Sopenharmony_ci	unsigned long driver_release_tids = 0;
15848c2ecf20Sopenharmony_ci	struct sk_buff_head frames;
15858c2ecf20Sopenharmony_ci	bool more_data;
15868c2ecf20Sopenharmony_ci
15878c2ecf20Sopenharmony_ci	/* Service or PS-Poll period starts */
15888c2ecf20Sopenharmony_ci	set_sta_flag(sta, WLAN_STA_SP);
15898c2ecf20Sopenharmony_ci
15908c2ecf20Sopenharmony_ci	__skb_queue_head_init(&frames);
15918c2ecf20Sopenharmony_ci
15928c2ecf20Sopenharmony_ci	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
15938c2ecf20Sopenharmony_ci				    &frames, &driver_release_tids);
15948c2ecf20Sopenharmony_ci
15958c2ecf20Sopenharmony_ci	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
15968c2ecf20Sopenharmony_ci
15978c2ecf20Sopenharmony_ci	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
15988c2ecf20Sopenharmony_ci		driver_release_tids =
15998c2ecf20Sopenharmony_ci			BIT(find_highest_prio_tid(driver_release_tids));
16008c2ecf20Sopenharmony_ci
16018c2ecf20Sopenharmony_ci	if (skb_queue_empty(&frames) && !driver_release_tids) {
16028c2ecf20Sopenharmony_ci		int tid, ac;
16038c2ecf20Sopenharmony_ci
16048c2ecf20Sopenharmony_ci		/*
16058c2ecf20Sopenharmony_ci		 * For PS-Poll, this can only happen due to a race condition
16068c2ecf20Sopenharmony_ci		 * when we set the TIM bit and the station notices it, but
16078c2ecf20Sopenharmony_ci		 * before it can poll for the frame we expire it.
16088c2ecf20Sopenharmony_ci		 *
16098c2ecf20Sopenharmony_ci		 * For uAPSD, this is said in the standard (11.2.1.5 h):
16108c2ecf20Sopenharmony_ci		 *	At each unscheduled SP for a non-AP STA, the AP shall
16118c2ecf20Sopenharmony_ci		 *	attempt to transmit at least one MSDU or MMPDU, but no
16128c2ecf20Sopenharmony_ci		 *	more than the value specified in the Max SP Length field
16138c2ecf20Sopenharmony_ci		 *	in the QoS Capability element from delivery-enabled ACs,
16148c2ecf20Sopenharmony_ci		 *	that are destined for the non-AP STA.
16158c2ecf20Sopenharmony_ci		 *
16168c2ecf20Sopenharmony_ci		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
16178c2ecf20Sopenharmony_ci		 */
16188c2ecf20Sopenharmony_ci
16198c2ecf20Sopenharmony_ci		/* This will evaluate to 1, 3, 5 or 7. */
16208c2ecf20Sopenharmony_ci		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
16218c2ecf20Sopenharmony_ci			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
16228c2ecf20Sopenharmony_ci				break;
16238c2ecf20Sopenharmony_ci		tid = 7 - 2 * ac;
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_ci		ieee80211_send_null_response(sta, tid, reason, true, false);
16268c2ecf20Sopenharmony_ci	} else if (!driver_release_tids) {
16278c2ecf20Sopenharmony_ci		struct sk_buff_head pending;
16288c2ecf20Sopenharmony_ci		struct sk_buff *skb;
16298c2ecf20Sopenharmony_ci		int num = 0;
16308c2ecf20Sopenharmony_ci		u16 tids = 0;
16318c2ecf20Sopenharmony_ci		bool need_null = false;
16328c2ecf20Sopenharmony_ci
16338c2ecf20Sopenharmony_ci		skb_queue_head_init(&pending);
16348c2ecf20Sopenharmony_ci
16358c2ecf20Sopenharmony_ci		while ((skb = __skb_dequeue(&frames))) {
16368c2ecf20Sopenharmony_ci			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
16378c2ecf20Sopenharmony_ci			struct ieee80211_hdr *hdr = (void *) skb->data;
16388c2ecf20Sopenharmony_ci			u8 *qoshdr = NULL;
16398c2ecf20Sopenharmony_ci
16408c2ecf20Sopenharmony_ci			num++;
16418c2ecf20Sopenharmony_ci
16428c2ecf20Sopenharmony_ci			/*
16438c2ecf20Sopenharmony_ci			 * Tell TX path to send this frame even though the
16448c2ecf20Sopenharmony_ci			 * STA may still remain is PS mode after this frame
16458c2ecf20Sopenharmony_ci			 * exchange.
16468c2ecf20Sopenharmony_ci			 */
16478c2ecf20Sopenharmony_ci			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
16488c2ecf20Sopenharmony_ci			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
16498c2ecf20Sopenharmony_ci
16508c2ecf20Sopenharmony_ci			/*
16518c2ecf20Sopenharmony_ci			 * Use MoreData flag to indicate whether there are
16528c2ecf20Sopenharmony_ci			 * more buffered frames for this STA
16538c2ecf20Sopenharmony_ci			 */
16548c2ecf20Sopenharmony_ci			if (more_data || !skb_queue_empty(&frames))
16558c2ecf20Sopenharmony_ci				hdr->frame_control |=
16568c2ecf20Sopenharmony_ci					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
16578c2ecf20Sopenharmony_ci			else
16588c2ecf20Sopenharmony_ci				hdr->frame_control &=
16598c2ecf20Sopenharmony_ci					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
16608c2ecf20Sopenharmony_ci
16618c2ecf20Sopenharmony_ci			if (ieee80211_is_data_qos(hdr->frame_control) ||
16628c2ecf20Sopenharmony_ci			    ieee80211_is_qos_nullfunc(hdr->frame_control))
16638c2ecf20Sopenharmony_ci				qoshdr = ieee80211_get_qos_ctl(hdr);
16648c2ecf20Sopenharmony_ci
16658c2ecf20Sopenharmony_ci			tids |= BIT(skb->priority);
16668c2ecf20Sopenharmony_ci
16678c2ecf20Sopenharmony_ci			__skb_queue_tail(&pending, skb);
16688c2ecf20Sopenharmony_ci
16698c2ecf20Sopenharmony_ci			/* end service period after last frame or add one */
16708c2ecf20Sopenharmony_ci			if (!skb_queue_empty(&frames))
16718c2ecf20Sopenharmony_ci				continue;
16728c2ecf20Sopenharmony_ci
16738c2ecf20Sopenharmony_ci			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
16748c2ecf20Sopenharmony_ci				/* for PS-Poll, there's only one frame */
16758c2ecf20Sopenharmony_ci				info->flags |= IEEE80211_TX_STATUS_EOSP |
16768c2ecf20Sopenharmony_ci					       IEEE80211_TX_CTL_REQ_TX_STATUS;
16778c2ecf20Sopenharmony_ci				break;
16788c2ecf20Sopenharmony_ci			}
16798c2ecf20Sopenharmony_ci
16808c2ecf20Sopenharmony_ci			/* For uAPSD, things are a bit more complicated. If the
16818c2ecf20Sopenharmony_ci			 * last frame has a QoS header (i.e. is a QoS-data or
16828c2ecf20Sopenharmony_ci			 * QoS-nulldata frame) then just set the EOSP bit there
16838c2ecf20Sopenharmony_ci			 * and be done.
16848c2ecf20Sopenharmony_ci			 * If the frame doesn't have a QoS header (which means
16858c2ecf20Sopenharmony_ci			 * it should be a bufferable MMPDU) then we can't set
16868c2ecf20Sopenharmony_ci			 * the EOSP bit in the QoS header; add a QoS-nulldata
16878c2ecf20Sopenharmony_ci			 * frame to the list to send it after the MMPDU.
16888c2ecf20Sopenharmony_ci			 *
16898c2ecf20Sopenharmony_ci			 * Note that this code is only in the mac80211-release
16908c2ecf20Sopenharmony_ci			 * code path, we assume that the driver will not buffer
16918c2ecf20Sopenharmony_ci			 * anything but QoS-data frames, or if it does, will
16928c2ecf20Sopenharmony_ci			 * create the QoS-nulldata frame by itself if needed.
16938c2ecf20Sopenharmony_ci			 *
16948c2ecf20Sopenharmony_ci			 * Cf. 802.11-2012 10.2.1.10 (c).
16958c2ecf20Sopenharmony_ci			 */
16968c2ecf20Sopenharmony_ci			if (qoshdr) {
16978c2ecf20Sopenharmony_ci				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
16988c2ecf20Sopenharmony_ci
16998c2ecf20Sopenharmony_ci				info->flags |= IEEE80211_TX_STATUS_EOSP |
17008c2ecf20Sopenharmony_ci					       IEEE80211_TX_CTL_REQ_TX_STATUS;
17018c2ecf20Sopenharmony_ci			} else {
17028c2ecf20Sopenharmony_ci				/* The standard isn't completely clear on this
17038c2ecf20Sopenharmony_ci				 * as it says the more-data bit should be set
17048c2ecf20Sopenharmony_ci				 * if there are more BUs. The QoS-Null frame
17058c2ecf20Sopenharmony_ci				 * we're about to send isn't buffered yet, we
17068c2ecf20Sopenharmony_ci				 * only create it below, but let's pretend it
17078c2ecf20Sopenharmony_ci				 * was buffered just in case some clients only
17088c2ecf20Sopenharmony_ci				 * expect more-data=0 when eosp=1.
17098c2ecf20Sopenharmony_ci				 */
17108c2ecf20Sopenharmony_ci				hdr->frame_control |=
17118c2ecf20Sopenharmony_ci					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
17128c2ecf20Sopenharmony_ci				need_null = true;
17138c2ecf20Sopenharmony_ci				num++;
17148c2ecf20Sopenharmony_ci			}
17158c2ecf20Sopenharmony_ci			break;
17168c2ecf20Sopenharmony_ci		}
17178c2ecf20Sopenharmony_ci
17188c2ecf20Sopenharmony_ci		drv_allow_buffered_frames(local, sta, tids, num,
17198c2ecf20Sopenharmony_ci					  reason, more_data);
17208c2ecf20Sopenharmony_ci
17218c2ecf20Sopenharmony_ci		ieee80211_add_pending_skbs(local, &pending);
17228c2ecf20Sopenharmony_ci
17238c2ecf20Sopenharmony_ci		if (need_null)
17248c2ecf20Sopenharmony_ci			ieee80211_send_null_response(
17258c2ecf20Sopenharmony_ci				sta, find_highest_prio_tid(tids),
17268c2ecf20Sopenharmony_ci				reason, false, false);
17278c2ecf20Sopenharmony_ci
17288c2ecf20Sopenharmony_ci		sta_info_recalc_tim(sta);
17298c2ecf20Sopenharmony_ci	} else {
17308c2ecf20Sopenharmony_ci		int tid;
17318c2ecf20Sopenharmony_ci
17328c2ecf20Sopenharmony_ci		/*
17338c2ecf20Sopenharmony_ci		 * We need to release a frame that is buffered somewhere in the
17348c2ecf20Sopenharmony_ci		 * driver ... it'll have to handle that.
17358c2ecf20Sopenharmony_ci		 * Note that the driver also has to check the number of frames
17368c2ecf20Sopenharmony_ci		 * on the TIDs we're releasing from - if there are more than
17378c2ecf20Sopenharmony_ci		 * n_frames it has to set the more-data bit (if we didn't ask
17388c2ecf20Sopenharmony_ci		 * it to set it anyway due to other buffered frames); if there
17398c2ecf20Sopenharmony_ci		 * are fewer than n_frames it has to make sure to adjust that
17408c2ecf20Sopenharmony_ci		 * to allow the service period to end properly.
17418c2ecf20Sopenharmony_ci		 */
17428c2ecf20Sopenharmony_ci		drv_release_buffered_frames(local, sta, driver_release_tids,
17438c2ecf20Sopenharmony_ci					    n_frames, reason, more_data);
17448c2ecf20Sopenharmony_ci
17458c2ecf20Sopenharmony_ci		/*
17468c2ecf20Sopenharmony_ci		 * Note that we don't recalculate the TIM bit here as it would
17478c2ecf20Sopenharmony_ci		 * most likely have no effect at all unless the driver told us
17488c2ecf20Sopenharmony_ci		 * that the TID(s) became empty before returning here from the
17498c2ecf20Sopenharmony_ci		 * release function.
17508c2ecf20Sopenharmony_ci		 * Either way, however, when the driver tells us that the TID(s)
17518c2ecf20Sopenharmony_ci		 * became empty or we find that a txq became empty, we'll do the
17528c2ecf20Sopenharmony_ci		 * TIM recalculation.
17538c2ecf20Sopenharmony_ci		 */
17548c2ecf20Sopenharmony_ci
17558c2ecf20Sopenharmony_ci		if (!sta->sta.txq[0])
17568c2ecf20Sopenharmony_ci			return;
17578c2ecf20Sopenharmony_ci
17588c2ecf20Sopenharmony_ci		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
17598c2ecf20Sopenharmony_ci			if (!sta->sta.txq[tid] ||
17608c2ecf20Sopenharmony_ci			    !(driver_release_tids & BIT(tid)) ||
17618c2ecf20Sopenharmony_ci			    txq_has_queue(sta->sta.txq[tid]))
17628c2ecf20Sopenharmony_ci				continue;
17638c2ecf20Sopenharmony_ci
17648c2ecf20Sopenharmony_ci			sta_info_recalc_tim(sta);
17658c2ecf20Sopenharmony_ci			break;
17668c2ecf20Sopenharmony_ci		}
17678c2ecf20Sopenharmony_ci	}
17688c2ecf20Sopenharmony_ci}
17698c2ecf20Sopenharmony_ci
17708c2ecf20Sopenharmony_civoid ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
17718c2ecf20Sopenharmony_ci{
17728c2ecf20Sopenharmony_ci	u8 ignore_for_response = sta->sta.uapsd_queues;
17738c2ecf20Sopenharmony_ci
17748c2ecf20Sopenharmony_ci	/*
17758c2ecf20Sopenharmony_ci	 * If all ACs are delivery-enabled then we should reply
17768c2ecf20Sopenharmony_ci	 * from any of them, if only some are enabled we reply
17778c2ecf20Sopenharmony_ci	 * only from the non-enabled ones.
17788c2ecf20Sopenharmony_ci	 */
17798c2ecf20Sopenharmony_ci	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
17808c2ecf20Sopenharmony_ci		ignore_for_response = 0;
17818c2ecf20Sopenharmony_ci
17828c2ecf20Sopenharmony_ci	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
17838c2ecf20Sopenharmony_ci					  IEEE80211_FRAME_RELEASE_PSPOLL);
17848c2ecf20Sopenharmony_ci}
17858c2ecf20Sopenharmony_ci
17868c2ecf20Sopenharmony_civoid ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
17878c2ecf20Sopenharmony_ci{
17888c2ecf20Sopenharmony_ci	int n_frames = sta->sta.max_sp;
17898c2ecf20Sopenharmony_ci	u8 delivery_enabled = sta->sta.uapsd_queues;
17908c2ecf20Sopenharmony_ci
17918c2ecf20Sopenharmony_ci	/*
17928c2ecf20Sopenharmony_ci	 * If we ever grow support for TSPEC this might happen if
17938c2ecf20Sopenharmony_ci	 * the TSPEC update from hostapd comes in between a trigger
17948c2ecf20Sopenharmony_ci	 * frame setting WLAN_STA_UAPSD in the RX path and this
17958c2ecf20Sopenharmony_ci	 * actually getting called.
17968c2ecf20Sopenharmony_ci	 */
17978c2ecf20Sopenharmony_ci	if (!delivery_enabled)
17988c2ecf20Sopenharmony_ci		return;
17998c2ecf20Sopenharmony_ci
18008c2ecf20Sopenharmony_ci	switch (sta->sta.max_sp) {
18018c2ecf20Sopenharmony_ci	case 1:
18028c2ecf20Sopenharmony_ci		n_frames = 2;
18038c2ecf20Sopenharmony_ci		break;
18048c2ecf20Sopenharmony_ci	case 2:
18058c2ecf20Sopenharmony_ci		n_frames = 4;
18068c2ecf20Sopenharmony_ci		break;
18078c2ecf20Sopenharmony_ci	case 3:
18088c2ecf20Sopenharmony_ci		n_frames = 6;
18098c2ecf20Sopenharmony_ci		break;
18108c2ecf20Sopenharmony_ci	case 0:
18118c2ecf20Sopenharmony_ci		/* XXX: what is a good value? */
18128c2ecf20Sopenharmony_ci		n_frames = 128;
18138c2ecf20Sopenharmony_ci		break;
18148c2ecf20Sopenharmony_ci	}
18158c2ecf20Sopenharmony_ci
18168c2ecf20Sopenharmony_ci	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
18178c2ecf20Sopenharmony_ci					  IEEE80211_FRAME_RELEASE_UAPSD);
18188c2ecf20Sopenharmony_ci}
18198c2ecf20Sopenharmony_ci
18208c2ecf20Sopenharmony_civoid ieee80211_sta_block_awake(struct ieee80211_hw *hw,
18218c2ecf20Sopenharmony_ci			       struct ieee80211_sta *pubsta, bool block)
18228c2ecf20Sopenharmony_ci{
18238c2ecf20Sopenharmony_ci	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
18248c2ecf20Sopenharmony_ci
18258c2ecf20Sopenharmony_ci	trace_api_sta_block_awake(sta->local, pubsta, block);
18268c2ecf20Sopenharmony_ci
18278c2ecf20Sopenharmony_ci	if (block) {
18288c2ecf20Sopenharmony_ci		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
18298c2ecf20Sopenharmony_ci		ieee80211_clear_fast_xmit(sta);
18308c2ecf20Sopenharmony_ci		return;
18318c2ecf20Sopenharmony_ci	}
18328c2ecf20Sopenharmony_ci
18338c2ecf20Sopenharmony_ci	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
18348c2ecf20Sopenharmony_ci		return;
18358c2ecf20Sopenharmony_ci
18368c2ecf20Sopenharmony_ci	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
18378c2ecf20Sopenharmony_ci		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
18388c2ecf20Sopenharmony_ci		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
18398c2ecf20Sopenharmony_ci		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
18408c2ecf20Sopenharmony_ci	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
18418c2ecf20Sopenharmony_ci		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
18428c2ecf20Sopenharmony_ci		/* must be asleep in this case */
18438c2ecf20Sopenharmony_ci		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
18448c2ecf20Sopenharmony_ci		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
18458c2ecf20Sopenharmony_ci	} else {
18468c2ecf20Sopenharmony_ci		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
18478c2ecf20Sopenharmony_ci		ieee80211_check_fast_xmit(sta);
18488c2ecf20Sopenharmony_ci	}
18498c2ecf20Sopenharmony_ci}
18508c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ieee80211_sta_block_awake);
18518c2ecf20Sopenharmony_ci
18528c2ecf20Sopenharmony_civoid ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
18538c2ecf20Sopenharmony_ci{
18548c2ecf20Sopenharmony_ci	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
18558c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sta->local;
18568c2ecf20Sopenharmony_ci
18578c2ecf20Sopenharmony_ci	trace_api_eosp(local, pubsta);
18588c2ecf20Sopenharmony_ci
18598c2ecf20Sopenharmony_ci	clear_sta_flag(sta, WLAN_STA_SP);
18608c2ecf20Sopenharmony_ci}
18618c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ieee80211_sta_eosp);
18628c2ecf20Sopenharmony_ci
18638c2ecf20Sopenharmony_civoid ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
18648c2ecf20Sopenharmony_ci{
18658c2ecf20Sopenharmony_ci	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
18668c2ecf20Sopenharmony_ci	enum ieee80211_frame_release_type reason;
18678c2ecf20Sopenharmony_ci	bool more_data;
18688c2ecf20Sopenharmony_ci
18698c2ecf20Sopenharmony_ci	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
18708c2ecf20Sopenharmony_ci
18718c2ecf20Sopenharmony_ci	reason = IEEE80211_FRAME_RELEASE_UAPSD;
18728c2ecf20Sopenharmony_ci	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
18738c2ecf20Sopenharmony_ci					       reason, 0);
18748c2ecf20Sopenharmony_ci
18758c2ecf20Sopenharmony_ci	ieee80211_send_null_response(sta, tid, reason, false, more_data);
18768c2ecf20Sopenharmony_ci}
18778c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
18788c2ecf20Sopenharmony_ci
18798c2ecf20Sopenharmony_civoid ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
18808c2ecf20Sopenharmony_ci				u8 tid, bool buffered)
18818c2ecf20Sopenharmony_ci{
18828c2ecf20Sopenharmony_ci	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
18838c2ecf20Sopenharmony_ci
18848c2ecf20Sopenharmony_ci	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
18858c2ecf20Sopenharmony_ci		return;
18868c2ecf20Sopenharmony_ci
18878c2ecf20Sopenharmony_ci	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
18888c2ecf20Sopenharmony_ci
18898c2ecf20Sopenharmony_ci	if (buffered)
18908c2ecf20Sopenharmony_ci		set_bit(tid, &sta->driver_buffered_tids);
18918c2ecf20Sopenharmony_ci	else
18928c2ecf20Sopenharmony_ci		clear_bit(tid, &sta->driver_buffered_tids);
18938c2ecf20Sopenharmony_ci
18948c2ecf20Sopenharmony_ci	sta_info_recalc_tim(sta);
18958c2ecf20Sopenharmony_ci}
18968c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ieee80211_sta_set_buffered);
18978c2ecf20Sopenharmony_ci
18988c2ecf20Sopenharmony_civoid ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
18998c2ecf20Sopenharmony_ci				    u32 tx_airtime, u32 rx_airtime)
19008c2ecf20Sopenharmony_ci{
19018c2ecf20Sopenharmony_ci	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
19028c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sta->sdata->local;
19038c2ecf20Sopenharmony_ci	u8 ac = ieee80211_ac_from_tid(tid);
19048c2ecf20Sopenharmony_ci	u32 airtime = 0;
19058c2ecf20Sopenharmony_ci
19068c2ecf20Sopenharmony_ci	if (sta->local->airtime_flags & AIRTIME_USE_TX)
19078c2ecf20Sopenharmony_ci		airtime += tx_airtime;
19088c2ecf20Sopenharmony_ci	if (sta->local->airtime_flags & AIRTIME_USE_RX)
19098c2ecf20Sopenharmony_ci		airtime += rx_airtime;
19108c2ecf20Sopenharmony_ci
19118c2ecf20Sopenharmony_ci	spin_lock_bh(&local->active_txq_lock[ac]);
19128c2ecf20Sopenharmony_ci	sta->airtime[ac].tx_airtime += tx_airtime;
19138c2ecf20Sopenharmony_ci	sta->airtime[ac].rx_airtime += rx_airtime;
19148c2ecf20Sopenharmony_ci	sta->airtime[ac].deficit -= airtime;
19158c2ecf20Sopenharmony_ci	spin_unlock_bh(&local->active_txq_lock[ac]);
19168c2ecf20Sopenharmony_ci}
19178c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ieee80211_sta_register_airtime);
19188c2ecf20Sopenharmony_ci
19198c2ecf20Sopenharmony_civoid ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
19208c2ecf20Sopenharmony_ci					  struct sta_info *sta, u8 ac,
19218c2ecf20Sopenharmony_ci					  u16 tx_airtime, bool tx_completed)
19228c2ecf20Sopenharmony_ci{
19238c2ecf20Sopenharmony_ci	int tx_pending;
19248c2ecf20Sopenharmony_ci
19258c2ecf20Sopenharmony_ci	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
19268c2ecf20Sopenharmony_ci		return;
19278c2ecf20Sopenharmony_ci
19288c2ecf20Sopenharmony_ci	if (!tx_completed) {
19298c2ecf20Sopenharmony_ci		if (sta)
19308c2ecf20Sopenharmony_ci			atomic_add(tx_airtime,
19318c2ecf20Sopenharmony_ci				   &sta->airtime[ac].aql_tx_pending);
19328c2ecf20Sopenharmony_ci
19338c2ecf20Sopenharmony_ci		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
19348c2ecf20Sopenharmony_ci		return;
19358c2ecf20Sopenharmony_ci	}
19368c2ecf20Sopenharmony_ci
19378c2ecf20Sopenharmony_ci	if (sta) {
19388c2ecf20Sopenharmony_ci		tx_pending = atomic_sub_return(tx_airtime,
19398c2ecf20Sopenharmony_ci					       &sta->airtime[ac].aql_tx_pending);
19408c2ecf20Sopenharmony_ci		if (tx_pending < 0)
19418c2ecf20Sopenharmony_ci			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
19428c2ecf20Sopenharmony_ci				       tx_pending, 0);
19438c2ecf20Sopenharmony_ci	}
19448c2ecf20Sopenharmony_ci
19458c2ecf20Sopenharmony_ci	tx_pending = atomic_sub_return(tx_airtime,
19468c2ecf20Sopenharmony_ci				       &local->aql_total_pending_airtime);
19478c2ecf20Sopenharmony_ci	if (WARN_ONCE(tx_pending < 0,
19488c2ecf20Sopenharmony_ci		      "Device %s AC %d pending airtime underflow: %u, %u",
19498c2ecf20Sopenharmony_ci		      wiphy_name(local->hw.wiphy), ac, tx_pending,
19508c2ecf20Sopenharmony_ci		      tx_airtime))
19518c2ecf20Sopenharmony_ci		atomic_cmpxchg(&local->aql_total_pending_airtime,
19528c2ecf20Sopenharmony_ci			       tx_pending, 0);
19538c2ecf20Sopenharmony_ci}
19548c2ecf20Sopenharmony_ci
19558c2ecf20Sopenharmony_ciint sta_info_move_state(struct sta_info *sta,
19568c2ecf20Sopenharmony_ci			enum ieee80211_sta_state new_state)
19578c2ecf20Sopenharmony_ci{
19588c2ecf20Sopenharmony_ci	might_sleep();
19598c2ecf20Sopenharmony_ci
19608c2ecf20Sopenharmony_ci	if (sta->sta_state == new_state)
19618c2ecf20Sopenharmony_ci		return 0;
19628c2ecf20Sopenharmony_ci
19638c2ecf20Sopenharmony_ci	/* check allowed transitions first */
19648c2ecf20Sopenharmony_ci
19658c2ecf20Sopenharmony_ci	switch (new_state) {
19668c2ecf20Sopenharmony_ci	case IEEE80211_STA_NONE:
19678c2ecf20Sopenharmony_ci		if (sta->sta_state != IEEE80211_STA_AUTH)
19688c2ecf20Sopenharmony_ci			return -EINVAL;
19698c2ecf20Sopenharmony_ci		break;
19708c2ecf20Sopenharmony_ci	case IEEE80211_STA_AUTH:
19718c2ecf20Sopenharmony_ci		if (sta->sta_state != IEEE80211_STA_NONE &&
19728c2ecf20Sopenharmony_ci		    sta->sta_state != IEEE80211_STA_ASSOC)
19738c2ecf20Sopenharmony_ci			return -EINVAL;
19748c2ecf20Sopenharmony_ci		break;
19758c2ecf20Sopenharmony_ci	case IEEE80211_STA_ASSOC:
19768c2ecf20Sopenharmony_ci		if (sta->sta_state != IEEE80211_STA_AUTH &&
19778c2ecf20Sopenharmony_ci		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
19788c2ecf20Sopenharmony_ci			return -EINVAL;
19798c2ecf20Sopenharmony_ci		break;
19808c2ecf20Sopenharmony_ci	case IEEE80211_STA_AUTHORIZED:
19818c2ecf20Sopenharmony_ci		if (sta->sta_state != IEEE80211_STA_ASSOC)
19828c2ecf20Sopenharmony_ci			return -EINVAL;
19838c2ecf20Sopenharmony_ci		break;
19848c2ecf20Sopenharmony_ci	default:
19858c2ecf20Sopenharmony_ci		WARN(1, "invalid state %d", new_state);
19868c2ecf20Sopenharmony_ci		return -EINVAL;
19878c2ecf20Sopenharmony_ci	}
19888c2ecf20Sopenharmony_ci
19898c2ecf20Sopenharmony_ci	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
19908c2ecf20Sopenharmony_ci		sta->sta.addr, new_state);
19918c2ecf20Sopenharmony_ci
19928c2ecf20Sopenharmony_ci	/*
19938c2ecf20Sopenharmony_ci	 * notify the driver before the actual changes so it can
19948c2ecf20Sopenharmony_ci	 * fail the transition
19958c2ecf20Sopenharmony_ci	 */
19968c2ecf20Sopenharmony_ci	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
19978c2ecf20Sopenharmony_ci		int err = drv_sta_state(sta->local, sta->sdata, sta,
19988c2ecf20Sopenharmony_ci					sta->sta_state, new_state);
19998c2ecf20Sopenharmony_ci		if (err)
20008c2ecf20Sopenharmony_ci			return err;
20018c2ecf20Sopenharmony_ci	}
20028c2ecf20Sopenharmony_ci
20038c2ecf20Sopenharmony_ci	/* reflect the change in all state variables */
20048c2ecf20Sopenharmony_ci
20058c2ecf20Sopenharmony_ci	switch (new_state) {
20068c2ecf20Sopenharmony_ci	case IEEE80211_STA_NONE:
20078c2ecf20Sopenharmony_ci		if (sta->sta_state == IEEE80211_STA_AUTH)
20088c2ecf20Sopenharmony_ci			clear_bit(WLAN_STA_AUTH, &sta->_flags);
20098c2ecf20Sopenharmony_ci		break;
20108c2ecf20Sopenharmony_ci	case IEEE80211_STA_AUTH:
20118c2ecf20Sopenharmony_ci		if (sta->sta_state == IEEE80211_STA_NONE) {
20128c2ecf20Sopenharmony_ci			set_bit(WLAN_STA_AUTH, &sta->_flags);
20138c2ecf20Sopenharmony_ci		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
20148c2ecf20Sopenharmony_ci			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
20158c2ecf20Sopenharmony_ci			ieee80211_recalc_min_chandef(sta->sdata);
20168c2ecf20Sopenharmony_ci			if (!sta->sta.support_p2p_ps)
20178c2ecf20Sopenharmony_ci				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
20188c2ecf20Sopenharmony_ci		}
20198c2ecf20Sopenharmony_ci		break;
20208c2ecf20Sopenharmony_ci	case IEEE80211_STA_ASSOC:
20218c2ecf20Sopenharmony_ci		if (sta->sta_state == IEEE80211_STA_AUTH) {
20228c2ecf20Sopenharmony_ci			set_bit(WLAN_STA_ASSOC, &sta->_flags);
20238c2ecf20Sopenharmony_ci			sta->assoc_at = ktime_get_boottime_ns();
20248c2ecf20Sopenharmony_ci			ieee80211_recalc_min_chandef(sta->sdata);
20258c2ecf20Sopenharmony_ci			if (!sta->sta.support_p2p_ps)
20268c2ecf20Sopenharmony_ci				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
20278c2ecf20Sopenharmony_ci		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
20288c2ecf20Sopenharmony_ci			ieee80211_vif_dec_num_mcast(sta->sdata);
20298c2ecf20Sopenharmony_ci			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
20308c2ecf20Sopenharmony_ci			ieee80211_clear_fast_xmit(sta);
20318c2ecf20Sopenharmony_ci			ieee80211_clear_fast_rx(sta);
20328c2ecf20Sopenharmony_ci		}
20338c2ecf20Sopenharmony_ci		break;
20348c2ecf20Sopenharmony_ci	case IEEE80211_STA_AUTHORIZED:
20358c2ecf20Sopenharmony_ci		if (sta->sta_state == IEEE80211_STA_ASSOC) {
20368c2ecf20Sopenharmony_ci			ieee80211_vif_inc_num_mcast(sta->sdata);
20378c2ecf20Sopenharmony_ci			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
20388c2ecf20Sopenharmony_ci			ieee80211_check_fast_xmit(sta);
20398c2ecf20Sopenharmony_ci			ieee80211_check_fast_rx(sta);
20408c2ecf20Sopenharmony_ci		}
20418c2ecf20Sopenharmony_ci		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
20428c2ecf20Sopenharmony_ci		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
20438c2ecf20Sopenharmony_ci			cfg80211_send_layer2_update(sta->sdata->dev,
20448c2ecf20Sopenharmony_ci						    sta->sta.addr);
20458c2ecf20Sopenharmony_ci		break;
20468c2ecf20Sopenharmony_ci	default:
20478c2ecf20Sopenharmony_ci		break;
20488c2ecf20Sopenharmony_ci	}
20498c2ecf20Sopenharmony_ci
20508c2ecf20Sopenharmony_ci	sta->sta_state = new_state;
20518c2ecf20Sopenharmony_ci
20528c2ecf20Sopenharmony_ci	return 0;
20538c2ecf20Sopenharmony_ci}
20548c2ecf20Sopenharmony_ci
20558c2ecf20Sopenharmony_ciu8 sta_info_tx_streams(struct sta_info *sta)
20568c2ecf20Sopenharmony_ci{
20578c2ecf20Sopenharmony_ci	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
20588c2ecf20Sopenharmony_ci	u8 rx_streams;
20598c2ecf20Sopenharmony_ci
20608c2ecf20Sopenharmony_ci	if (!sta->sta.ht_cap.ht_supported)
20618c2ecf20Sopenharmony_ci		return 1;
20628c2ecf20Sopenharmony_ci
20638c2ecf20Sopenharmony_ci	if (sta->sta.vht_cap.vht_supported) {
20648c2ecf20Sopenharmony_ci		int i;
20658c2ecf20Sopenharmony_ci		u16 tx_mcs_map =
20668c2ecf20Sopenharmony_ci			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
20678c2ecf20Sopenharmony_ci
20688c2ecf20Sopenharmony_ci		for (i = 7; i >= 0; i--)
20698c2ecf20Sopenharmony_ci			if ((tx_mcs_map & (0x3 << (i * 2))) !=
20708c2ecf20Sopenharmony_ci			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
20718c2ecf20Sopenharmony_ci				return i + 1;
20728c2ecf20Sopenharmony_ci	}
20738c2ecf20Sopenharmony_ci
20748c2ecf20Sopenharmony_ci	if (ht_cap->mcs.rx_mask[3])
20758c2ecf20Sopenharmony_ci		rx_streams = 4;
20768c2ecf20Sopenharmony_ci	else if (ht_cap->mcs.rx_mask[2])
20778c2ecf20Sopenharmony_ci		rx_streams = 3;
20788c2ecf20Sopenharmony_ci	else if (ht_cap->mcs.rx_mask[1])
20798c2ecf20Sopenharmony_ci		rx_streams = 2;
20808c2ecf20Sopenharmony_ci	else
20818c2ecf20Sopenharmony_ci		rx_streams = 1;
20828c2ecf20Sopenharmony_ci
20838c2ecf20Sopenharmony_ci	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
20848c2ecf20Sopenharmony_ci		return rx_streams;
20858c2ecf20Sopenharmony_ci
20868c2ecf20Sopenharmony_ci	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
20878c2ecf20Sopenharmony_ci			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
20888c2ecf20Sopenharmony_ci}
20898c2ecf20Sopenharmony_ci
20908c2ecf20Sopenharmony_cistatic struct ieee80211_sta_rx_stats *
20918c2ecf20Sopenharmony_cista_get_last_rx_stats(struct sta_info *sta)
20928c2ecf20Sopenharmony_ci{
20938c2ecf20Sopenharmony_ci	struct ieee80211_sta_rx_stats *stats = &sta->rx_stats;
20948c2ecf20Sopenharmony_ci	int cpu;
20958c2ecf20Sopenharmony_ci
20968c2ecf20Sopenharmony_ci	if (!sta->pcpu_rx_stats)
20978c2ecf20Sopenharmony_ci		return stats;
20988c2ecf20Sopenharmony_ci
20998c2ecf20Sopenharmony_ci	for_each_possible_cpu(cpu) {
21008c2ecf20Sopenharmony_ci		struct ieee80211_sta_rx_stats *cpustats;
21018c2ecf20Sopenharmony_ci
21028c2ecf20Sopenharmony_ci		cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
21038c2ecf20Sopenharmony_ci
21048c2ecf20Sopenharmony_ci		if (time_after(cpustats->last_rx, stats->last_rx))
21058c2ecf20Sopenharmony_ci			stats = cpustats;
21068c2ecf20Sopenharmony_ci	}
21078c2ecf20Sopenharmony_ci
21088c2ecf20Sopenharmony_ci	return stats;
21098c2ecf20Sopenharmony_ci}
21108c2ecf20Sopenharmony_ci
21118c2ecf20Sopenharmony_cistatic void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
21128c2ecf20Sopenharmony_ci				  struct rate_info *rinfo)
21138c2ecf20Sopenharmony_ci{
21148c2ecf20Sopenharmony_ci	rinfo->bw = STA_STATS_GET(BW, rate);
21158c2ecf20Sopenharmony_ci
21168c2ecf20Sopenharmony_ci	switch (STA_STATS_GET(TYPE, rate)) {
21178c2ecf20Sopenharmony_ci	case STA_STATS_RATE_TYPE_VHT:
21188c2ecf20Sopenharmony_ci		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
21198c2ecf20Sopenharmony_ci		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
21208c2ecf20Sopenharmony_ci		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
21218c2ecf20Sopenharmony_ci		if (STA_STATS_GET(SGI, rate))
21228c2ecf20Sopenharmony_ci			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
21238c2ecf20Sopenharmony_ci		break;
21248c2ecf20Sopenharmony_ci	case STA_STATS_RATE_TYPE_HT:
21258c2ecf20Sopenharmony_ci		rinfo->flags = RATE_INFO_FLAGS_MCS;
21268c2ecf20Sopenharmony_ci		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
21278c2ecf20Sopenharmony_ci		if (STA_STATS_GET(SGI, rate))
21288c2ecf20Sopenharmony_ci			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
21298c2ecf20Sopenharmony_ci		break;
21308c2ecf20Sopenharmony_ci	case STA_STATS_RATE_TYPE_LEGACY: {
21318c2ecf20Sopenharmony_ci		struct ieee80211_supported_band *sband;
21328c2ecf20Sopenharmony_ci		u16 brate;
21338c2ecf20Sopenharmony_ci		unsigned int shift;
21348c2ecf20Sopenharmony_ci		int band = STA_STATS_GET(LEGACY_BAND, rate);
21358c2ecf20Sopenharmony_ci		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
21368c2ecf20Sopenharmony_ci
21378c2ecf20Sopenharmony_ci		sband = local->hw.wiphy->bands[band];
21388c2ecf20Sopenharmony_ci
21398c2ecf20Sopenharmony_ci		if (WARN_ON_ONCE(!sband->bitrates))
21408c2ecf20Sopenharmony_ci			break;
21418c2ecf20Sopenharmony_ci
21428c2ecf20Sopenharmony_ci		brate = sband->bitrates[rate_idx].bitrate;
21438c2ecf20Sopenharmony_ci		if (rinfo->bw == RATE_INFO_BW_5)
21448c2ecf20Sopenharmony_ci			shift = 2;
21458c2ecf20Sopenharmony_ci		else if (rinfo->bw == RATE_INFO_BW_10)
21468c2ecf20Sopenharmony_ci			shift = 1;
21478c2ecf20Sopenharmony_ci		else
21488c2ecf20Sopenharmony_ci			shift = 0;
21498c2ecf20Sopenharmony_ci		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
21508c2ecf20Sopenharmony_ci		break;
21518c2ecf20Sopenharmony_ci		}
21528c2ecf20Sopenharmony_ci	case STA_STATS_RATE_TYPE_HE:
21538c2ecf20Sopenharmony_ci		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
21548c2ecf20Sopenharmony_ci		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
21558c2ecf20Sopenharmony_ci		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
21568c2ecf20Sopenharmony_ci		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
21578c2ecf20Sopenharmony_ci		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
21588c2ecf20Sopenharmony_ci		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
21598c2ecf20Sopenharmony_ci		break;
21608c2ecf20Sopenharmony_ci	}
21618c2ecf20Sopenharmony_ci}
21628c2ecf20Sopenharmony_ci
21638c2ecf20Sopenharmony_cistatic int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
21648c2ecf20Sopenharmony_ci{
21658c2ecf20Sopenharmony_ci	u32 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
21668c2ecf20Sopenharmony_ci
21678c2ecf20Sopenharmony_ci	if (rate == STA_STATS_RATE_INVALID)
21688c2ecf20Sopenharmony_ci		return -EINVAL;
21698c2ecf20Sopenharmony_ci
21708c2ecf20Sopenharmony_ci	sta_stats_decode_rate(sta->local, rate, rinfo);
21718c2ecf20Sopenharmony_ci	return 0;
21728c2ecf20Sopenharmony_ci}
21738c2ecf20Sopenharmony_ci
21748c2ecf20Sopenharmony_cistatic inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
21758c2ecf20Sopenharmony_ci					int tid)
21768c2ecf20Sopenharmony_ci{
21778c2ecf20Sopenharmony_ci	unsigned int start;
21788c2ecf20Sopenharmony_ci	u64 value;
21798c2ecf20Sopenharmony_ci
21808c2ecf20Sopenharmony_ci	do {
21818c2ecf20Sopenharmony_ci		start = u64_stats_fetch_begin_irq(&rxstats->syncp);
21828c2ecf20Sopenharmony_ci		value = rxstats->msdu[tid];
21838c2ecf20Sopenharmony_ci	} while (u64_stats_fetch_retry_irq(&rxstats->syncp, start));
21848c2ecf20Sopenharmony_ci
21858c2ecf20Sopenharmony_ci	return value;
21868c2ecf20Sopenharmony_ci}
21878c2ecf20Sopenharmony_ci
21888c2ecf20Sopenharmony_cistatic void sta_set_tidstats(struct sta_info *sta,
21898c2ecf20Sopenharmony_ci			     struct cfg80211_tid_stats *tidstats,
21908c2ecf20Sopenharmony_ci			     int tid)
21918c2ecf20Sopenharmony_ci{
21928c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sta->local;
21938c2ecf20Sopenharmony_ci	int cpu;
21948c2ecf20Sopenharmony_ci
21958c2ecf20Sopenharmony_ci	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
21968c2ecf20Sopenharmony_ci		tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->rx_stats, tid);
21978c2ecf20Sopenharmony_ci
21988c2ecf20Sopenharmony_ci		if (sta->pcpu_rx_stats) {
21998c2ecf20Sopenharmony_ci			for_each_possible_cpu(cpu) {
22008c2ecf20Sopenharmony_ci				struct ieee80211_sta_rx_stats *cpurxs;
22018c2ecf20Sopenharmony_ci
22028c2ecf20Sopenharmony_ci				cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
22038c2ecf20Sopenharmony_ci				tidstats->rx_msdu +=
22048c2ecf20Sopenharmony_ci					sta_get_tidstats_msdu(cpurxs, tid);
22058c2ecf20Sopenharmony_ci			}
22068c2ecf20Sopenharmony_ci		}
22078c2ecf20Sopenharmony_ci
22088c2ecf20Sopenharmony_ci		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
22098c2ecf20Sopenharmony_ci	}
22108c2ecf20Sopenharmony_ci
22118c2ecf20Sopenharmony_ci	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
22128c2ecf20Sopenharmony_ci		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
22138c2ecf20Sopenharmony_ci		tidstats->tx_msdu = sta->tx_stats.msdu[tid];
22148c2ecf20Sopenharmony_ci	}
22158c2ecf20Sopenharmony_ci
22168c2ecf20Sopenharmony_ci	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
22178c2ecf20Sopenharmony_ci	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
22188c2ecf20Sopenharmony_ci		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
22198c2ecf20Sopenharmony_ci		tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid];
22208c2ecf20Sopenharmony_ci	}
22218c2ecf20Sopenharmony_ci
22228c2ecf20Sopenharmony_ci	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
22238c2ecf20Sopenharmony_ci	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
22248c2ecf20Sopenharmony_ci		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
22258c2ecf20Sopenharmony_ci		tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid];
22268c2ecf20Sopenharmony_ci	}
22278c2ecf20Sopenharmony_ci
22288c2ecf20Sopenharmony_ci	if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
22298c2ecf20Sopenharmony_ci		spin_lock_bh(&local->fq.lock);
22308c2ecf20Sopenharmony_ci		rcu_read_lock();
22318c2ecf20Sopenharmony_ci
22328c2ecf20Sopenharmony_ci		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
22338c2ecf20Sopenharmony_ci		ieee80211_fill_txq_stats(&tidstats->txq_stats,
22348c2ecf20Sopenharmony_ci					 to_txq_info(sta->sta.txq[tid]));
22358c2ecf20Sopenharmony_ci
22368c2ecf20Sopenharmony_ci		rcu_read_unlock();
22378c2ecf20Sopenharmony_ci		spin_unlock_bh(&local->fq.lock);
22388c2ecf20Sopenharmony_ci	}
22398c2ecf20Sopenharmony_ci}
22408c2ecf20Sopenharmony_ci
22418c2ecf20Sopenharmony_cistatic inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
22428c2ecf20Sopenharmony_ci{
22438c2ecf20Sopenharmony_ci	unsigned int start;
22448c2ecf20Sopenharmony_ci	u64 value;
22458c2ecf20Sopenharmony_ci
22468c2ecf20Sopenharmony_ci	do {
22478c2ecf20Sopenharmony_ci		start = u64_stats_fetch_begin_irq(&rxstats->syncp);
22488c2ecf20Sopenharmony_ci		value = rxstats->bytes;
22498c2ecf20Sopenharmony_ci	} while (u64_stats_fetch_retry_irq(&rxstats->syncp, start));
22508c2ecf20Sopenharmony_ci
22518c2ecf20Sopenharmony_ci	return value;
22528c2ecf20Sopenharmony_ci}
22538c2ecf20Sopenharmony_ci
22548c2ecf20Sopenharmony_civoid sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
22558c2ecf20Sopenharmony_ci		   bool tidstats)
22568c2ecf20Sopenharmony_ci{
22578c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata = sta->sdata;
22588c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
22598c2ecf20Sopenharmony_ci	u32 thr = 0;
22608c2ecf20Sopenharmony_ci	int i, ac, cpu;
22618c2ecf20Sopenharmony_ci	struct ieee80211_sta_rx_stats *last_rxstats;
22628c2ecf20Sopenharmony_ci
22638c2ecf20Sopenharmony_ci	last_rxstats = sta_get_last_rx_stats(sta);
22648c2ecf20Sopenharmony_ci
22658c2ecf20Sopenharmony_ci	sinfo->generation = sdata->local->sta_generation;
22668c2ecf20Sopenharmony_ci
22678c2ecf20Sopenharmony_ci	/* do before driver, so beacon filtering drivers have a
22688c2ecf20Sopenharmony_ci	 * chance to e.g. just add the number of filtered beacons
22698c2ecf20Sopenharmony_ci	 * (or just modify the value entirely, of course)
22708c2ecf20Sopenharmony_ci	 */
22718c2ecf20Sopenharmony_ci	if (sdata->vif.type == NL80211_IFTYPE_STATION)
22728c2ecf20Sopenharmony_ci		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
22738c2ecf20Sopenharmony_ci
22748c2ecf20Sopenharmony_ci	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
22758c2ecf20Sopenharmony_ci	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
22768c2ecf20Sopenharmony_ci			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
22778c2ecf20Sopenharmony_ci			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
22788c2ecf20Sopenharmony_ci			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
22798c2ecf20Sopenharmony_ci			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
22808c2ecf20Sopenharmony_ci			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
22818c2ecf20Sopenharmony_ci
22828c2ecf20Sopenharmony_ci	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
22838c2ecf20Sopenharmony_ci		sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
22848c2ecf20Sopenharmony_ci		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
22858c2ecf20Sopenharmony_ci	}
22868c2ecf20Sopenharmony_ci
22878c2ecf20Sopenharmony_ci	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
22888c2ecf20Sopenharmony_ci	sinfo->assoc_at = sta->assoc_at;
22898c2ecf20Sopenharmony_ci	sinfo->inactive_time =
22908c2ecf20Sopenharmony_ci		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
22918c2ecf20Sopenharmony_ci
22928c2ecf20Sopenharmony_ci	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
22938c2ecf20Sopenharmony_ci			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
22948c2ecf20Sopenharmony_ci		sinfo->tx_bytes = 0;
22958c2ecf20Sopenharmony_ci		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
22968c2ecf20Sopenharmony_ci			sinfo->tx_bytes += sta->tx_stats.bytes[ac];
22978c2ecf20Sopenharmony_ci		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
22988c2ecf20Sopenharmony_ci	}
22998c2ecf20Sopenharmony_ci
23008c2ecf20Sopenharmony_ci	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
23018c2ecf20Sopenharmony_ci		sinfo->tx_packets = 0;
23028c2ecf20Sopenharmony_ci		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
23038c2ecf20Sopenharmony_ci			sinfo->tx_packets += sta->tx_stats.packets[ac];
23048c2ecf20Sopenharmony_ci		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
23058c2ecf20Sopenharmony_ci	}
23068c2ecf20Sopenharmony_ci
23078c2ecf20Sopenharmony_ci	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
23088c2ecf20Sopenharmony_ci			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
23098c2ecf20Sopenharmony_ci		sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats);
23108c2ecf20Sopenharmony_ci
23118c2ecf20Sopenharmony_ci		if (sta->pcpu_rx_stats) {
23128c2ecf20Sopenharmony_ci			for_each_possible_cpu(cpu) {
23138c2ecf20Sopenharmony_ci				struct ieee80211_sta_rx_stats *cpurxs;
23148c2ecf20Sopenharmony_ci
23158c2ecf20Sopenharmony_ci				cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
23168c2ecf20Sopenharmony_ci				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
23178c2ecf20Sopenharmony_ci			}
23188c2ecf20Sopenharmony_ci		}
23198c2ecf20Sopenharmony_ci
23208c2ecf20Sopenharmony_ci		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
23218c2ecf20Sopenharmony_ci	}
23228c2ecf20Sopenharmony_ci
23238c2ecf20Sopenharmony_ci	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
23248c2ecf20Sopenharmony_ci		sinfo->rx_packets = sta->rx_stats.packets;
23258c2ecf20Sopenharmony_ci		if (sta->pcpu_rx_stats) {
23268c2ecf20Sopenharmony_ci			for_each_possible_cpu(cpu) {
23278c2ecf20Sopenharmony_ci				struct ieee80211_sta_rx_stats *cpurxs;
23288c2ecf20Sopenharmony_ci
23298c2ecf20Sopenharmony_ci				cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
23308c2ecf20Sopenharmony_ci				sinfo->rx_packets += cpurxs->packets;
23318c2ecf20Sopenharmony_ci			}
23328c2ecf20Sopenharmony_ci		}
23338c2ecf20Sopenharmony_ci		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
23348c2ecf20Sopenharmony_ci	}
23358c2ecf20Sopenharmony_ci
23368c2ecf20Sopenharmony_ci	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
23378c2ecf20Sopenharmony_ci		sinfo->tx_retries = sta->status_stats.retry_count;
23388c2ecf20Sopenharmony_ci		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
23398c2ecf20Sopenharmony_ci	}
23408c2ecf20Sopenharmony_ci
23418c2ecf20Sopenharmony_ci	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
23428c2ecf20Sopenharmony_ci		sinfo->tx_failed = sta->status_stats.retry_failed;
23438c2ecf20Sopenharmony_ci		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
23448c2ecf20Sopenharmony_ci	}
23458c2ecf20Sopenharmony_ci
23468c2ecf20Sopenharmony_ci	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
23478c2ecf20Sopenharmony_ci		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
23488c2ecf20Sopenharmony_ci			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
23498c2ecf20Sopenharmony_ci		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
23508c2ecf20Sopenharmony_ci	}
23518c2ecf20Sopenharmony_ci
23528c2ecf20Sopenharmony_ci	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
23538c2ecf20Sopenharmony_ci		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
23548c2ecf20Sopenharmony_ci			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
23558c2ecf20Sopenharmony_ci		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
23568c2ecf20Sopenharmony_ci	}
23578c2ecf20Sopenharmony_ci
23588c2ecf20Sopenharmony_ci	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
23598c2ecf20Sopenharmony_ci		sinfo->airtime_weight = sta->airtime_weight;
23608c2ecf20Sopenharmony_ci		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
23618c2ecf20Sopenharmony_ci	}
23628c2ecf20Sopenharmony_ci
23638c2ecf20Sopenharmony_ci	sinfo->rx_dropped_misc = sta->rx_stats.dropped;
23648c2ecf20Sopenharmony_ci	if (sta->pcpu_rx_stats) {
23658c2ecf20Sopenharmony_ci		for_each_possible_cpu(cpu) {
23668c2ecf20Sopenharmony_ci			struct ieee80211_sta_rx_stats *cpurxs;
23678c2ecf20Sopenharmony_ci
23688c2ecf20Sopenharmony_ci			cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu);
23698c2ecf20Sopenharmony_ci			sinfo->rx_dropped_misc += cpurxs->dropped;
23708c2ecf20Sopenharmony_ci		}
23718c2ecf20Sopenharmony_ci	}
23728c2ecf20Sopenharmony_ci
23738c2ecf20Sopenharmony_ci	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
23748c2ecf20Sopenharmony_ci	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
23758c2ecf20Sopenharmony_ci		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
23768c2ecf20Sopenharmony_ci				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
23778c2ecf20Sopenharmony_ci		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
23788c2ecf20Sopenharmony_ci	}
23798c2ecf20Sopenharmony_ci
23808c2ecf20Sopenharmony_ci	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
23818c2ecf20Sopenharmony_ci	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
23828c2ecf20Sopenharmony_ci		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
23838c2ecf20Sopenharmony_ci			sinfo->signal = (s8)last_rxstats->last_signal;
23848c2ecf20Sopenharmony_ci			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
23858c2ecf20Sopenharmony_ci		}
23868c2ecf20Sopenharmony_ci
23878c2ecf20Sopenharmony_ci		if (!sta->pcpu_rx_stats &&
23888c2ecf20Sopenharmony_ci		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
23898c2ecf20Sopenharmony_ci			sinfo->signal_avg =
23908c2ecf20Sopenharmony_ci				-ewma_signal_read(&sta->rx_stats_avg.signal);
23918c2ecf20Sopenharmony_ci			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
23928c2ecf20Sopenharmony_ci		}
23938c2ecf20Sopenharmony_ci	}
23948c2ecf20Sopenharmony_ci
23958c2ecf20Sopenharmony_ci	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
23968c2ecf20Sopenharmony_ci	 * the sta->rx_stats struct, so the check here is fine with and without
23978c2ecf20Sopenharmony_ci	 * pcpu statistics
23988c2ecf20Sopenharmony_ci	 */
23998c2ecf20Sopenharmony_ci	if (last_rxstats->chains &&
24008c2ecf20Sopenharmony_ci	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
24018c2ecf20Sopenharmony_ci			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
24028c2ecf20Sopenharmony_ci		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
24038c2ecf20Sopenharmony_ci		if (!sta->pcpu_rx_stats)
24048c2ecf20Sopenharmony_ci			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
24058c2ecf20Sopenharmony_ci
24068c2ecf20Sopenharmony_ci		sinfo->chains = last_rxstats->chains;
24078c2ecf20Sopenharmony_ci
24088c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
24098c2ecf20Sopenharmony_ci			sinfo->chain_signal[i] =
24108c2ecf20Sopenharmony_ci				last_rxstats->chain_signal_last[i];
24118c2ecf20Sopenharmony_ci			sinfo->chain_signal_avg[i] =
24128c2ecf20Sopenharmony_ci				-ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]);
24138c2ecf20Sopenharmony_ci		}
24148c2ecf20Sopenharmony_ci	}
24158c2ecf20Sopenharmony_ci
24168c2ecf20Sopenharmony_ci	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
24178c2ecf20Sopenharmony_ci		sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
24188c2ecf20Sopenharmony_ci				     &sinfo->txrate);
24198c2ecf20Sopenharmony_ci		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
24208c2ecf20Sopenharmony_ci	}
24218c2ecf20Sopenharmony_ci
24228c2ecf20Sopenharmony_ci	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
24238c2ecf20Sopenharmony_ci		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
24248c2ecf20Sopenharmony_ci			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
24258c2ecf20Sopenharmony_ci	}
24268c2ecf20Sopenharmony_ci
24278c2ecf20Sopenharmony_ci	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
24288c2ecf20Sopenharmony_ci		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
24298c2ecf20Sopenharmony_ci			sta_set_tidstats(sta, &sinfo->pertid[i], i);
24308c2ecf20Sopenharmony_ci	}
24318c2ecf20Sopenharmony_ci
24328c2ecf20Sopenharmony_ci	if (ieee80211_vif_is_mesh(&sdata->vif)) {
24338c2ecf20Sopenharmony_ci#ifdef CONFIG_MAC80211_MESH
24348c2ecf20Sopenharmony_ci		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
24358c2ecf20Sopenharmony_ci				 BIT_ULL(NL80211_STA_INFO_PLID) |
24368c2ecf20Sopenharmony_ci				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
24378c2ecf20Sopenharmony_ci				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
24388c2ecf20Sopenharmony_ci				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
24398c2ecf20Sopenharmony_ci				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
24408c2ecf20Sopenharmony_ci				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
24418c2ecf20Sopenharmony_ci				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
24428c2ecf20Sopenharmony_ci
24438c2ecf20Sopenharmony_ci		sinfo->llid = sta->mesh->llid;
24448c2ecf20Sopenharmony_ci		sinfo->plid = sta->mesh->plid;
24458c2ecf20Sopenharmony_ci		sinfo->plink_state = sta->mesh->plink_state;
24468c2ecf20Sopenharmony_ci		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
24478c2ecf20Sopenharmony_ci			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
24488c2ecf20Sopenharmony_ci			sinfo->t_offset = sta->mesh->t_offset;
24498c2ecf20Sopenharmony_ci		}
24508c2ecf20Sopenharmony_ci		sinfo->local_pm = sta->mesh->local_pm;
24518c2ecf20Sopenharmony_ci		sinfo->peer_pm = sta->mesh->peer_pm;
24528c2ecf20Sopenharmony_ci		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
24538c2ecf20Sopenharmony_ci		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
24548c2ecf20Sopenharmony_ci		sinfo->connected_to_as = sta->mesh->connected_to_as;
24558c2ecf20Sopenharmony_ci#endif
24568c2ecf20Sopenharmony_ci	}
24578c2ecf20Sopenharmony_ci
24588c2ecf20Sopenharmony_ci	sinfo->bss_param.flags = 0;
24598c2ecf20Sopenharmony_ci	if (sdata->vif.bss_conf.use_cts_prot)
24608c2ecf20Sopenharmony_ci		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
24618c2ecf20Sopenharmony_ci	if (sdata->vif.bss_conf.use_short_preamble)
24628c2ecf20Sopenharmony_ci		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
24638c2ecf20Sopenharmony_ci	if (sdata->vif.bss_conf.use_short_slot)
24648c2ecf20Sopenharmony_ci		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
24658c2ecf20Sopenharmony_ci	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
24668c2ecf20Sopenharmony_ci	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
24678c2ecf20Sopenharmony_ci
24688c2ecf20Sopenharmony_ci	sinfo->sta_flags.set = 0;
24698c2ecf20Sopenharmony_ci	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
24708c2ecf20Sopenharmony_ci				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
24718c2ecf20Sopenharmony_ci				BIT(NL80211_STA_FLAG_WME) |
24728c2ecf20Sopenharmony_ci				BIT(NL80211_STA_FLAG_MFP) |
24738c2ecf20Sopenharmony_ci				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
24748c2ecf20Sopenharmony_ci				BIT(NL80211_STA_FLAG_ASSOCIATED) |
24758c2ecf20Sopenharmony_ci				BIT(NL80211_STA_FLAG_TDLS_PEER);
24768c2ecf20Sopenharmony_ci	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
24778c2ecf20Sopenharmony_ci		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
24788c2ecf20Sopenharmony_ci	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
24798c2ecf20Sopenharmony_ci		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
24808c2ecf20Sopenharmony_ci	if (sta->sta.wme)
24818c2ecf20Sopenharmony_ci		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
24828c2ecf20Sopenharmony_ci	if (test_sta_flag(sta, WLAN_STA_MFP))
24838c2ecf20Sopenharmony_ci		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
24848c2ecf20Sopenharmony_ci	if (test_sta_flag(sta, WLAN_STA_AUTH))
24858c2ecf20Sopenharmony_ci		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
24868c2ecf20Sopenharmony_ci	if (test_sta_flag(sta, WLAN_STA_ASSOC))
24878c2ecf20Sopenharmony_ci		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
24888c2ecf20Sopenharmony_ci	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
24898c2ecf20Sopenharmony_ci		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
24908c2ecf20Sopenharmony_ci
24918c2ecf20Sopenharmony_ci	thr = sta_get_expected_throughput(sta);
24928c2ecf20Sopenharmony_ci
24938c2ecf20Sopenharmony_ci	if (thr != 0) {
24948c2ecf20Sopenharmony_ci		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
24958c2ecf20Sopenharmony_ci		sinfo->expected_throughput = thr;
24968c2ecf20Sopenharmony_ci	}
24978c2ecf20Sopenharmony_ci
24988c2ecf20Sopenharmony_ci	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
24998c2ecf20Sopenharmony_ci	    sta->status_stats.ack_signal_filled) {
25008c2ecf20Sopenharmony_ci		sinfo->ack_signal = sta->status_stats.last_ack_signal;
25018c2ecf20Sopenharmony_ci		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
25028c2ecf20Sopenharmony_ci	}
25038c2ecf20Sopenharmony_ci
25048c2ecf20Sopenharmony_ci	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
25058c2ecf20Sopenharmony_ci	    sta->status_stats.ack_signal_filled) {
25068c2ecf20Sopenharmony_ci		sinfo->avg_ack_signal =
25078c2ecf20Sopenharmony_ci			-(s8)ewma_avg_signal_read(
25088c2ecf20Sopenharmony_ci				&sta->status_stats.avg_ack_signal);
25098c2ecf20Sopenharmony_ci		sinfo->filled |=
25108c2ecf20Sopenharmony_ci			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
25118c2ecf20Sopenharmony_ci	}
25128c2ecf20Sopenharmony_ci
25138c2ecf20Sopenharmony_ci	if (ieee80211_vif_is_mesh(&sdata->vif)) {
25148c2ecf20Sopenharmony_ci		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
25158c2ecf20Sopenharmony_ci		sinfo->airtime_link_metric =
25168c2ecf20Sopenharmony_ci			airtime_link_metric_get(local, sta);
25178c2ecf20Sopenharmony_ci	}
25188c2ecf20Sopenharmony_ci}
25198c2ecf20Sopenharmony_ci
25208c2ecf20Sopenharmony_ciu32 sta_get_expected_throughput(struct sta_info *sta)
25218c2ecf20Sopenharmony_ci{
25228c2ecf20Sopenharmony_ci	struct ieee80211_sub_if_data *sdata = sta->sdata;
25238c2ecf20Sopenharmony_ci	struct ieee80211_local *local = sdata->local;
25248c2ecf20Sopenharmony_ci	struct rate_control_ref *ref = NULL;
25258c2ecf20Sopenharmony_ci	u32 thr = 0;
25268c2ecf20Sopenharmony_ci
25278c2ecf20Sopenharmony_ci	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
25288c2ecf20Sopenharmony_ci		ref = local->rate_ctrl;
25298c2ecf20Sopenharmony_ci
25308c2ecf20Sopenharmony_ci	/* check if the driver has a SW RC implementation */
25318c2ecf20Sopenharmony_ci	if (ref && ref->ops->get_expected_throughput)
25328c2ecf20Sopenharmony_ci		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
25338c2ecf20Sopenharmony_ci	else
25348c2ecf20Sopenharmony_ci		thr = drv_get_expected_throughput(local, sta);
25358c2ecf20Sopenharmony_ci
25368c2ecf20Sopenharmony_ci	return thr;
25378c2ecf20Sopenharmony_ci}
25388c2ecf20Sopenharmony_ci
25398c2ecf20Sopenharmony_ciunsigned long ieee80211_sta_last_active(struct sta_info *sta)
25408c2ecf20Sopenharmony_ci{
25418c2ecf20Sopenharmony_ci	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
25428c2ecf20Sopenharmony_ci
25438c2ecf20Sopenharmony_ci	if (!sta->status_stats.last_ack ||
25448c2ecf20Sopenharmony_ci	    time_after(stats->last_rx, sta->status_stats.last_ack))
25458c2ecf20Sopenharmony_ci		return stats->last_rx;
25468c2ecf20Sopenharmony_ci	return sta->status_stats.last_ack;
25478c2ecf20Sopenharmony_ci}
25488c2ecf20Sopenharmony_ci
25498c2ecf20Sopenharmony_cistatic void sta_update_codel_params(struct sta_info *sta, u32 thr)
25508c2ecf20Sopenharmony_ci{
25518c2ecf20Sopenharmony_ci	if (!sta->sdata->local->ops->wake_tx_queue)
25528c2ecf20Sopenharmony_ci		return;
25538c2ecf20Sopenharmony_ci
25548c2ecf20Sopenharmony_ci	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
25558c2ecf20Sopenharmony_ci		sta->cparams.target = MS2TIME(50);
25568c2ecf20Sopenharmony_ci		sta->cparams.interval = MS2TIME(300);
25578c2ecf20Sopenharmony_ci		sta->cparams.ecn = false;
25588c2ecf20Sopenharmony_ci	} else {
25598c2ecf20Sopenharmony_ci		sta->cparams.target = MS2TIME(20);
25608c2ecf20Sopenharmony_ci		sta->cparams.interval = MS2TIME(100);
25618c2ecf20Sopenharmony_ci		sta->cparams.ecn = true;
25628c2ecf20Sopenharmony_ci	}
25638c2ecf20Sopenharmony_ci}
25648c2ecf20Sopenharmony_ci
25658c2ecf20Sopenharmony_civoid ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
25668c2ecf20Sopenharmony_ci					   u32 thr)
25678c2ecf20Sopenharmony_ci{
25688c2ecf20Sopenharmony_ci	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
25698c2ecf20Sopenharmony_ci
25708c2ecf20Sopenharmony_ci	sta_update_codel_params(sta, thr);
25718c2ecf20Sopenharmony_ci}
2572