xref: /openbmc/linux/net/mac80211/sta_info.c (revision ba6ddab94fc63813ad582c55dd95ed596420d101)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f0706e82SJiri Benc /*
3f0706e82SJiri Benc  * Copyright 2002-2005, Instant802 Networks, Inc.
4f0706e82SJiri Benc  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
5d98ad83eSJohannes Berg  * Copyright 2013-2014  Intel Mobile Communications GmbH
6dcba665bSJohannes Berg  * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
73a11ce08SJohannes Berg  * Copyright (C) 2018-2021 Intel Corporation
8f0706e82SJiri Benc  */
9f0706e82SJiri Benc 
10f0706e82SJiri Benc #include <linux/module.h>
11f0706e82SJiri Benc #include <linux/init.h>
12888d04dfSFelix Fietkau #include <linux/etherdevice.h>
13f0706e82SJiri Benc #include <linux/netdevice.h>
14f0706e82SJiri Benc #include <linux/types.h>
15f0706e82SJiri Benc #include <linux/slab.h>
16f0706e82SJiri Benc #include <linux/skbuff.h>
17f0706e82SJiri Benc #include <linux/if_arp.h>
180d174406SJohannes Berg #include <linux/timer.h>
19d0709a65SJohannes Berg #include <linux/rtnetlink.h>
20f0706e82SJiri Benc 
21484a54c2SToke Høiland-Jørgensen #include <net/codel.h>
22f0706e82SJiri Benc #include <net/mac80211.h>
23f0706e82SJiri Benc #include "ieee80211_i.h"
2424487981SJohannes Berg #include "driver-ops.h"
252c8dccc7SJohannes Berg #include "rate.h"
26f0706e82SJiri Benc #include "sta_info.h"
27e9f207f0SJiri Benc #include "debugfs_sta.h"
28ee385855SLuis Carlos Cobo #include "mesh.h"
29ce662b44SJohannes Berg #include "wme.h"
30f0706e82SJiri Benc 
31d0709a65SJohannes Berg /**
32d0709a65SJohannes Berg  * DOC: STA information lifetime rules
33d0709a65SJohannes Berg  *
34d0709a65SJohannes Berg  * STA info structures (&struct sta_info) are managed in a hash table
35d0709a65SJohannes Berg  * for faster lookup and a list for iteration. They are managed using
36d0709a65SJohannes Berg  * RCU, i.e. access to the list and hash table is protected by RCU.
37d0709a65SJohannes Berg  *
3834e89507SJohannes Berg  * Upon allocating a STA info structure with sta_info_alloc(), the caller
3934e89507SJohannes Berg  * owns that structure. It must then insert it into the hash table using
4034e89507SJohannes Berg  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
4134e89507SJohannes Berg  * case (which acquires an rcu read section but must not be called from
4234e89507SJohannes Berg  * within one) will the pointer still be valid after the call. Note that
4334e89507SJohannes Berg  * the caller may not do much with the STA info before inserting it, in
4434e89507SJohannes Berg  * particular, it may not start any mesh peer link management or add
4534e89507SJohannes Berg  * encryption keys.
4693e5deb1SJohannes Berg  *
4793e5deb1SJohannes Berg  * When the insertion fails (sta_info_insert()) returns non-zero), the
4893e5deb1SJohannes Berg  * structure will have been freed by sta_info_insert()!
49d0709a65SJohannes Berg  *
5034e89507SJohannes Berg  * Station entries are added by mac80211 when you establish a link with a
517e189a12SLuis R. Rodriguez  * peer. This means different things for the different type of interfaces
527e189a12SLuis R. Rodriguez  * we support. For a regular station this mean we add the AP sta when we
5325985edcSLucas De Marchi  * receive an association response from the AP. For IBSS this occurs when
5434e89507SJohannes Berg  * get to know about a peer on the same IBSS. For WDS we add the sta for
5525985edcSLucas De Marchi  * the peer immediately upon device open. When using AP mode we add stations
5634e89507SJohannes Berg  * for each respective station upon request from userspace through nl80211.
577e189a12SLuis R. Rodriguez  *
5834e89507SJohannes Berg  * In order to remove a STA info structure, various sta_info_destroy_*()
5934e89507SJohannes Berg  * calls are available.
60d0709a65SJohannes Berg  *
6134e89507SJohannes Berg  * There is no concept of ownership on a STA entry, each structure is
6234e89507SJohannes Berg  * owned by the global hash table/list until it is removed. All users of
6334e89507SJohannes Berg  * the structure need to be RCU protected so that the structure won't be
6434e89507SJohannes Berg  * freed before they are done using it.
65d0709a65SJohannes Berg  */
66f0706e82SJiri Benc 
67cb71f1d1SJohannes Berg struct sta_link_alloc {
68cb71f1d1SJohannes Berg 	struct link_sta_info info;
69cb71f1d1SJohannes Berg 	struct ieee80211_link_sta sta;
70c71420dbSJohannes Berg 	struct rcu_head rcu_head;
71cb71f1d1SJohannes Berg };
72cb71f1d1SJohannes Berg 
737bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = {
747bedd0cfSJohannes Berg 	.nelem_hint = 3, /* start small */
75caf22d31SJohannes Berg 	.automatic_shrinking = true,
767bedd0cfSJohannes Berg 	.head_offset = offsetof(struct sta_info, hash_node),
77ac100ce5SJohannes Berg 	.key_offset = offsetof(struct sta_info, addr),
787bedd0cfSJohannes Berg 	.key_len = ETH_ALEN,
79ebd82b39SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
807bedd0cfSJohannes Berg };
817bedd0cfSJohannes Berg 
82*ba6ddab9SJohannes Berg static const struct rhashtable_params link_sta_rht_params = {
83*ba6ddab9SJohannes Berg 	.nelem_hint = 3, /* start small */
84*ba6ddab9SJohannes Berg 	.automatic_shrinking = true,
85*ba6ddab9SJohannes Berg 	.head_offset = offsetof(struct link_sta_info, link_hash_node),
86*ba6ddab9SJohannes Berg 	.key_offset = offsetof(struct link_sta_info, addr),
87*ba6ddab9SJohannes Berg 	.key_len = ETH_ALEN,
88*ba6ddab9SJohannes Berg 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
89*ba6ddab9SJohannes Berg };
90*ba6ddab9SJohannes Berg 
914d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
92be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local,
93f0706e82SJiri Benc 			     struct sta_info *sta)
94f0706e82SJiri Benc {
9583e7e4ceSHerbert Xu 	return rhltable_remove(&local->sta_hash, &sta->hash_node,
967bedd0cfSJohannes Berg 			       sta_rht_params);
97f0706e82SJiri Benc }
98f0706e82SJiri Benc 
99*ba6ddab9SJohannes Berg static int link_sta_info_hash_del(struct ieee80211_local *local,
100*ba6ddab9SJohannes Berg 				  struct link_sta_info *link_sta)
101*ba6ddab9SJohannes Berg {
102*ba6ddab9SJohannes Berg 	return rhltable_remove(&local->link_sta_hash,
103*ba6ddab9SJohannes Berg 			       &link_sta->link_hash_node,
104*ba6ddab9SJohannes Berg 			       link_sta_rht_params);
105*ba6ddab9SJohannes Berg }
106*ba6ddab9SJohannes Berg 
1075108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta)
108b22cfcfcSEliad Peller {
109b22cfcfcSEliad Peller 	int ac, i;
110b22cfcfcSEliad Peller 	struct tid_ampdu_tx *tid_tx;
111b22cfcfcSEliad Peller 	struct ieee80211_sub_if_data *sdata = sta->sdata;
112b22cfcfcSEliad Peller 	struct ieee80211_local *local = sdata->local;
113d012a605SMarco Porsch 	struct ps_data *ps;
114b22cfcfcSEliad Peller 
115e3685e03SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
1165ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
1175ac2e350SJohannes Berg 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
118d012a605SMarco Porsch 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
119d012a605SMarco Porsch 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
120d012a605SMarco Porsch 			ps = &sdata->bss->ps;
1213f52b7e3SMarco Porsch 		else if (ieee80211_vif_is_mesh(&sdata->vif))
1223f52b7e3SMarco Porsch 			ps = &sdata->u.mesh.ps;
123d012a605SMarco Porsch 		else
124d012a605SMarco Porsch 			return;
125b22cfcfcSEliad Peller 
126b22cfcfcSEliad Peller 		clear_sta_flag(sta, WLAN_STA_PS_STA);
127e3685e03SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1285ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
129b22cfcfcSEliad Peller 
130d012a605SMarco Porsch 		atomic_dec(&ps->num_sta_ps);
131b22cfcfcSEliad Peller 	}
132b22cfcfcSEliad Peller 
133ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0]) {
134ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
135adf8ed01SJohannes Berg 			struct txq_info *txqi;
136adf8ed01SJohannes Berg 
137adf8ed01SJohannes Berg 			if (!sta->sta.txq[i])
138adf8ed01SJohannes Berg 				continue;
139adf8ed01SJohannes Berg 
140adf8ed01SJohannes Berg 			txqi = to_txq_info(sta->sta.txq[i]);
141ba8c3d6fSFelix Fietkau 
142fa962b92SMichal Kazior 			ieee80211_txq_purge(local, txqi);
143ba8c3d6fSFelix Fietkau 		}
144ba8c3d6fSFelix Fietkau 	}
145ba8c3d6fSFelix Fietkau 
146b22cfcfcSEliad Peller 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
147b22cfcfcSEliad Peller 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
1481f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
1491f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
150b22cfcfcSEliad Peller 	}
151b22cfcfcSEliad Peller 
15245b5028eSThomas Pedersen 	if (ieee80211_vif_is_mesh(&sdata->vif))
15345b5028eSThomas Pedersen 		mesh_sta_cleanup(sta);
154b22cfcfcSEliad Peller 
1555ac2e350SJohannes Berg 	cancel_work_sync(&sta->drv_deliver_wk);
156b22cfcfcSEliad Peller 
157b22cfcfcSEliad Peller 	/*
158b22cfcfcSEliad Peller 	 * Destroy aggregation state here. It would be nice to wait for the
159b22cfcfcSEliad Peller 	 * driver to finish aggregation stop and then clean up, but for now
160b22cfcfcSEliad Peller 	 * drivers have to handle aggregation stop being requested, followed
161b22cfcfcSEliad Peller 	 * directly by station destruction.
162b22cfcfcSEliad Peller 	 */
1635a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
164661eb381SJohannes Berg 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
165b22cfcfcSEliad Peller 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
166b22cfcfcSEliad Peller 		if (!tid_tx)
167b22cfcfcSEliad Peller 			continue;
1681f98ab7fSFelix Fietkau 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
169b22cfcfcSEliad Peller 		kfree(tid_tx);
170b22cfcfcSEliad Peller 	}
1715108ca82SJohannes Berg }
172b22cfcfcSEliad Peller 
1735108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta)
1745108ca82SJohannes Berg {
1755108ca82SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1765108ca82SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1775108ca82SJohannes Berg 
1785108ca82SJohannes Berg 	__cleanup_single_sta(sta);
179b22cfcfcSEliad Peller 	sta_info_free(local, sta);
180b22cfcfcSEliad Peller }
181b22cfcfcSEliad Peller 
18283e7e4ceSHerbert Xu struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
18383e7e4ceSHerbert Xu 					 const u8 *addr)
18483e7e4ceSHerbert Xu {
18583e7e4ceSHerbert Xu 	return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
18683e7e4ceSHerbert Xu }
18783e7e4ceSHerbert Xu 
188d0709a65SJohannes Berg /* protected by RCU */
189abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
190abe60632SJohannes Berg 			      const u8 *addr)
19143ba7e95SJohannes Berg {
192abe60632SJohannes Berg 	struct ieee80211_local *local = sdata->local;
19383e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
19460f4b626SJohannes Berg 	struct sta_info *sta;
19543ba7e95SJohannes Berg 
19660f4b626SJohannes Berg 	rcu_read_lock();
19783e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
19860f4b626SJohannes Berg 		if (sta->sdata == sdata) {
19960f4b626SJohannes Berg 			rcu_read_unlock();
20060f4b626SJohannes Berg 			/* this is safe as the caller must already hold
20160f4b626SJohannes Berg 			 * another rcu read section or the mutex
20260f4b626SJohannes Berg 			 */
20360f4b626SJohannes Berg 			return sta;
20460f4b626SJohannes Berg 		}
20560f4b626SJohannes Berg 	}
20660f4b626SJohannes Berg 	rcu_read_unlock();
20760f4b626SJohannes Berg 	return NULL;
20843ba7e95SJohannes Berg }
20943ba7e95SJohannes Berg 
2100e5ded5aSFelix Fietkau /*
2110e5ded5aSFelix Fietkau  * Get sta info either from the specified interface
2120e5ded5aSFelix Fietkau  * or from one of its vlans
2130e5ded5aSFelix Fietkau  */
2140e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
2150e5ded5aSFelix Fietkau 				  const u8 *addr)
2160e5ded5aSFelix Fietkau {
2170e5ded5aSFelix Fietkau 	struct ieee80211_local *local = sdata->local;
21883e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
2190e5ded5aSFelix Fietkau 	struct sta_info *sta;
2200e5ded5aSFelix Fietkau 
2217bedd0cfSJohannes Berg 	rcu_read_lock();
22283e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
2237bedd0cfSJohannes Berg 		if (sta->sdata == sdata ||
2247bedd0cfSJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
2257bedd0cfSJohannes Berg 			rcu_read_unlock();
2267bedd0cfSJohannes Berg 			/* this is safe as the caller must already hold
2277bedd0cfSJohannes Berg 			 * another rcu read section or the mutex
2287bedd0cfSJohannes Berg 			 */
2290e5ded5aSFelix Fietkau 			return sta;
2300e5ded5aSFelix Fietkau 		}
2317bedd0cfSJohannes Berg 	}
2327bedd0cfSJohannes Berg 	rcu_read_unlock();
2337bedd0cfSJohannes Berg 	return NULL;
2347bedd0cfSJohannes Berg }
2350e5ded5aSFelix Fietkau 
236*ba6ddab9SJohannes Berg struct rhlist_head *link_sta_info_hash_lookup(struct ieee80211_local *local,
237*ba6ddab9SJohannes Berg 					      const u8 *addr)
238*ba6ddab9SJohannes Berg {
239*ba6ddab9SJohannes Berg 	return rhltable_lookup(&local->link_sta_hash, addr,
240*ba6ddab9SJohannes Berg 			       link_sta_rht_params);
241*ba6ddab9SJohannes Berg }
242*ba6ddab9SJohannes Berg 
243*ba6ddab9SJohannes Berg struct link_sta_info *
244*ba6ddab9SJohannes Berg link_sta_info_get_bss(struct ieee80211_sub_if_data *sdata, const u8 *addr)
245*ba6ddab9SJohannes Berg {
246*ba6ddab9SJohannes Berg 	struct ieee80211_local *local = sdata->local;
247*ba6ddab9SJohannes Berg 	struct rhlist_head *tmp;
248*ba6ddab9SJohannes Berg 	struct link_sta_info *link_sta;
249*ba6ddab9SJohannes Berg 
250*ba6ddab9SJohannes Berg 	rcu_read_lock();
251*ba6ddab9SJohannes Berg 	for_each_link_sta_info(local, addr, link_sta, tmp) {
252*ba6ddab9SJohannes Berg 		struct sta_info *sta = link_sta->sta;
253*ba6ddab9SJohannes Berg 
254*ba6ddab9SJohannes Berg 		if (sta->sdata == sdata ||
255*ba6ddab9SJohannes Berg 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
256*ba6ddab9SJohannes Berg 			rcu_read_unlock();
257*ba6ddab9SJohannes Berg 			/* this is safe as the caller must already hold
258*ba6ddab9SJohannes Berg 			 * another rcu read section or the mutex
259*ba6ddab9SJohannes Berg 			 */
260*ba6ddab9SJohannes Berg 			return link_sta;
261*ba6ddab9SJohannes Berg 		}
262*ba6ddab9SJohannes Berg 	}
263*ba6ddab9SJohannes Berg 	rcu_read_unlock();
264*ba6ddab9SJohannes Berg 	return NULL;
265*ba6ddab9SJohannes Berg }
266*ba6ddab9SJohannes Berg 
2675072f73cSToke Høiland-Jørgensen struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
2685072f73cSToke Høiland-Jørgensen 				       const u8 *sta_addr, const u8 *vif_addr)
2695072f73cSToke Høiland-Jørgensen {
2705072f73cSToke Høiland-Jørgensen 	struct rhlist_head *tmp;
2715072f73cSToke Høiland-Jørgensen 	struct sta_info *sta;
2725072f73cSToke Høiland-Jørgensen 
2735072f73cSToke Høiland-Jørgensen 	for_each_sta_info(local, sta_addr, sta, tmp) {
2745072f73cSToke Høiland-Jørgensen 		if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
2755072f73cSToke Høiland-Jørgensen 			return sta;
2765072f73cSToke Høiland-Jørgensen 	}
2775072f73cSToke Høiland-Jørgensen 
2785072f73cSToke Høiland-Jørgensen 	return NULL;
2795072f73cSToke Høiland-Jørgensen }
2805072f73cSToke Høiland-Jørgensen 
2813b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
2823b53fde8SJohannes Berg 				     int idx)
283ee385855SLuis Carlos Cobo {
2843b53fde8SJohannes Berg 	struct ieee80211_local *local = sdata->local;
285ee385855SLuis Carlos Cobo 	struct sta_info *sta;
286ee385855SLuis Carlos Cobo 	int i = 0;
287ee385855SLuis Carlos Cobo 
2888ca47eb9SMadhuparna Bhowmik 	list_for_each_entry_rcu(sta, &local->sta_list, list,
2898ca47eb9SMadhuparna Bhowmik 				lockdep_is_held(&local->sta_mtx)) {
2903b53fde8SJohannes Berg 		if (sdata != sta->sdata)
2912a8ca29aSLuis Carlos Cobo 			continue;
292ee385855SLuis Carlos Cobo 		if (i < idx) {
293ee385855SLuis Carlos Cobo 			++i;
294ee385855SLuis Carlos Cobo 			continue;
295ee385855SLuis Carlos Cobo 		}
2962a8ca29aSLuis Carlos Cobo 		return sta;
297ee385855SLuis Carlos Cobo 	}
298ee385855SLuis Carlos Cobo 
299ee385855SLuis Carlos Cobo 	return NULL;
300ee385855SLuis Carlos Cobo }
301f0706e82SJiri Benc 
302cb71f1d1SJohannes Berg static void sta_info_free_link(struct link_sta_info *link_sta)
303246b39e4SJohannes Berg {
304cb71f1d1SJohannes Berg 	free_percpu(link_sta->pcpu_rx_stats);
305cb71f1d1SJohannes Berg }
306246b39e4SJohannes Berg 
307*ba6ddab9SJohannes Berg static void sta_remove_link(struct sta_info *sta, unsigned int link_id,
308*ba6ddab9SJohannes Berg 			    bool unhash)
309cb71f1d1SJohannes Berg {
310cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc = NULL;
311c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
312cb71f1d1SJohannes Berg 
313c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
314c71420dbSJohannes Berg 					     lockdep_is_held(&sta->local->sta_mtx));
315c71420dbSJohannes Berg 
316c71420dbSJohannes Berg 	if (WARN_ON(!link_sta))
317cb71f1d1SJohannes Berg 		return;
318246b39e4SJohannes Berg 
319*ba6ddab9SJohannes Berg 	if (unhash)
320*ba6ddab9SJohannes Berg 		link_sta_info_hash_del(sta->local, link_sta);
321*ba6ddab9SJohannes Berg 
322c71420dbSJohannes Berg 	if (link_sta != &sta->deflink)
323c71420dbSJohannes Berg 		alloc = container_of(link_sta, typeof(*alloc), info);
324cb71f1d1SJohannes Berg 
325cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
326c71420dbSJohannes Berg 	RCU_INIT_POINTER(sta->link[link_id], NULL);
327c71420dbSJohannes Berg 	RCU_INIT_POINTER(sta->sta.link[link_id], NULL);
328cb71f1d1SJohannes Berg 	if (alloc) {
329cb71f1d1SJohannes Berg 		sta_info_free_link(&alloc->info);
330c71420dbSJohannes Berg 		kfree_rcu(alloc, rcu_head);
331246b39e4SJohannes Berg 	}
332246b39e4SJohannes Berg }
333246b39e4SJohannes Berg 
33493e5deb1SJohannes Berg /**
335d9a7ddb0SJohannes Berg  * sta_info_free - free STA
33693e5deb1SJohannes Berg  *
3376ef307bcSRandy Dunlap  * @local: pointer to the global information
33893e5deb1SJohannes Berg  * @sta: STA info to free
33993e5deb1SJohannes Berg  *
34093e5deb1SJohannes Berg  * This function must undo everything done by sta_info_alloc()
341d9a7ddb0SJohannes Berg  * that may happen before sta_info_insert(). It may only be
342d9a7ddb0SJohannes Berg  * called when sta_info_insert() has not been attempted (and
343d9a7ddb0SJohannes Berg  * if that fails, the station is freed anyway.)
34493e5deb1SJohannes Berg  */
345d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
34693e5deb1SJohannes Berg {
347cb71f1d1SJohannes Berg 	int i;
348cb71f1d1SJohannes Berg 
349cb71f1d1SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
350cb71f1d1SJohannes Berg 		if (!(sta->sta.valid_links & BIT(i)))
351cb71f1d1SJohannes Berg 			continue;
352cb71f1d1SJohannes Berg 
353*ba6ddab9SJohannes Berg 		sta_remove_link(sta, i, true);
354cb71f1d1SJohannes Berg 	}
355cb71f1d1SJohannes Berg 
356dcd479e1SJohannes Berg 	/*
357dcd479e1SJohannes Berg 	 * If we had used sta_info_pre_move_state() then we might not
358dcd479e1SJohannes Berg 	 * have gone through the state transitions down again, so do
359dcd479e1SJohannes Berg 	 * it here now (and warn if it's inserted).
360dcd479e1SJohannes Berg 	 *
361dcd479e1SJohannes Berg 	 * This will clear state such as fast TX/RX that may have been
362dcd479e1SJohannes Berg 	 * allocated during state transitions.
363dcd479e1SJohannes Berg 	 */
364dcd479e1SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
365dcd479e1SJohannes Berg 		int ret;
366dcd479e1SJohannes Berg 
367dcd479e1SJohannes Berg 		WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
368dcd479e1SJohannes Berg 
369dcd479e1SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
370dcd479e1SJohannes Berg 		if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
371dcd479e1SJohannes Berg 			break;
372dcd479e1SJohannes Berg 	}
373dcd479e1SJohannes Berg 
374889cbb91SJohannes Berg 	if (sta->rate_ctrl)
3754b7679a5SJohannes Berg 		rate_control_free_sta(sta);
37693e5deb1SJohannes Berg 
377bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
37893e5deb1SJohannes Berg 
379ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
380ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
38153d04525SFelix Fietkau 	kfree(rcu_dereference_raw(sta->sta.rates));
382433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
383433f5bc1SJohannes Berg 	kfree(sta->mesh);
384433f5bc1SJohannes Berg #endif
385246b39e4SJohannes Berg 
386cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
38793e5deb1SJohannes Berg 	kfree(sta);
38893e5deb1SJohannes Berg }
38993e5deb1SJohannes Berg 
3904d33960bSJohannes Berg /* Caller must hold local->sta_mtx */
39162b14b24SJohannes Berg static int sta_info_hash_add(struct ieee80211_local *local,
392d0709a65SJohannes Berg 			     struct sta_info *sta)
393f0706e82SJiri Benc {
39483e7e4ceSHerbert Xu 	return rhltable_insert(&local->sta_hash, &sta->hash_node,
3957bedd0cfSJohannes Berg 			       sta_rht_params);
396f0706e82SJiri Benc }
397f0706e82SJiri Benc 
3985ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk)
399af818581SJohannes Berg {
400af818581SJohannes Berg 	struct sta_info *sta;
401af818581SJohannes Berg 
4025ac2e350SJohannes Berg 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
403af818581SJohannes Berg 
404af818581SJohannes Berg 	if (sta->dead)
405af818581SJohannes Berg 		return;
406af818581SJohannes Berg 
40754420473SHelmut Schaa 	local_bh_disable();
4085ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
409af818581SJohannes Berg 		ieee80211_sta_ps_deliver_wakeup(sta);
4105ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
411af818581SJohannes Berg 		ieee80211_sta_ps_deliver_poll_response(sta);
4125ac2e350SJohannes Berg 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
41347086fc5SJohannes Berg 		ieee80211_sta_ps_deliver_uapsd(sta);
414ce662b44SJohannes Berg 	local_bh_enable();
415af818581SJohannes Berg }
416af818581SJohannes Berg 
417af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local,
418af65cd96SJohannes Berg 				    struct sta_info *sta, gfp_t gfp)
419af65cd96SJohannes Berg {
42030686bf7SJohannes Berg 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
421af65cd96SJohannes Berg 		return 0;
422af65cd96SJohannes Berg 
423889cbb91SJohannes Berg 	sta->rate_ctrl = local->rate_ctrl;
424af65cd96SJohannes Berg 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
42535c347acSJohannes Berg 						     sta, gfp);
426889cbb91SJohannes Berg 	if (!sta->rate_ctrl_priv)
427af65cd96SJohannes Berg 		return -ENOMEM;
428af65cd96SJohannes Berg 
429af65cd96SJohannes Berg 	return 0;
430af65cd96SJohannes Berg }
431af65cd96SJohannes Berg 
432cb71f1d1SJohannes Berg static int sta_info_alloc_link(struct ieee80211_local *local,
433246b39e4SJohannes Berg 			       struct link_sta_info *link_info,
434246b39e4SJohannes Berg 			       gfp_t gfp)
435246b39e4SJohannes Berg {
436246b39e4SJohannes Berg 	struct ieee80211_hw *hw = &local->hw;
437246b39e4SJohannes Berg 	int i;
438246b39e4SJohannes Berg 
439246b39e4SJohannes Berg 	if (ieee80211_hw_check(hw, USES_RSS)) {
440246b39e4SJohannes Berg 		link_info->pcpu_rx_stats =
441246b39e4SJohannes Berg 			alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
442246b39e4SJohannes Berg 		if (!link_info->pcpu_rx_stats)
443246b39e4SJohannes Berg 			return -ENOMEM;
444246b39e4SJohannes Berg 	}
445246b39e4SJohannes Berg 
446246b39e4SJohannes Berg 	link_info->rx_stats.last_rx = jiffies;
447246b39e4SJohannes Berg 	u64_stats_init(&link_info->rx_stats.syncp);
448246b39e4SJohannes Berg 
449246b39e4SJohannes Berg 	ewma_signal_init(&link_info->rx_stats_avg.signal);
450246b39e4SJohannes Berg 	ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
451246b39e4SJohannes Berg 	for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
452246b39e4SJohannes Berg 		ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
453246b39e4SJohannes Berg 
454246b39e4SJohannes Berg 	return 0;
455246b39e4SJohannes Berg }
456246b39e4SJohannes Berg 
457cb71f1d1SJohannes Berg static void sta_info_add_link(struct sta_info *sta,
458cb71f1d1SJohannes Berg 			      unsigned int link_id,
459cb71f1d1SJohannes Berg 			      struct link_sta_info *link_info,
460cb71f1d1SJohannes Berg 			      struct ieee80211_link_sta *link_sta)
461cb71f1d1SJohannes Berg {
462cb71f1d1SJohannes Berg 	link_info->sta = sta;
463cb71f1d1SJohannes Berg 	link_info->link_id = link_id;
464c71420dbSJohannes Berg 	link_info->pub = link_sta;
465c71420dbSJohannes Berg 	rcu_assign_pointer(sta->link[link_id], link_info);
466c71420dbSJohannes Berg 	rcu_assign_pointer(sta->sta.link[link_id], link_sta);
467cb71f1d1SJohannes Berg }
468cb71f1d1SJohannes Berg 
46973651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
470cb71f1d1SJohannes Berg 				const u8 *addr, int link_id, gfp_t gfp)
471f0706e82SJiri Benc {
472d0709a65SJohannes Berg 	struct ieee80211_local *local = sdata->local;
473ba8c3d6fSFelix Fietkau 	struct ieee80211_hw *hw = &local->hw;
474f0706e82SJiri Benc 	struct sta_info *sta;
47516c5f15cSRon Rindjunsky 	int i;
476f0706e82SJiri Benc 
477ba8c3d6fSFelix Fietkau 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
478f0706e82SJiri Benc 	if (!sta)
47973651ee6SJohannes Berg 		return NULL;
480f0706e82SJiri Benc 
481246b39e4SJohannes Berg 	sta->local = local;
482246b39e4SJohannes Berg 	sta->sdata = sdata;
483246b39e4SJohannes Berg 
484cb71f1d1SJohannes Berg 	if (sta_info_alloc_link(local, &sta->deflink, gfp))
485246b39e4SJohannes Berg 		return NULL;
486c9c5962bSJohannes Berg 
487cb71f1d1SJohannes Berg 	if (link_id >= 0) {
488cb71f1d1SJohannes Berg 		sta_info_add_link(sta, link_id, &sta->deflink,
489cb71f1d1SJohannes Berg 				  &sta->sta.deflink);
490cb71f1d1SJohannes Berg 		sta->sta.valid_links = BIT(link_id);
491cb71f1d1SJohannes Berg 	} else {
492cb71f1d1SJohannes Berg 		sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
493cb71f1d1SJohannes Berg 	}
494cb71f1d1SJohannes Berg 
49507346f81SJohannes Berg 	spin_lock_init(&sta->lock);
4961d147bfaSEmmanuel Grumbach 	spin_lock_init(&sta->ps_lock);
4975ac2e350SJohannes Berg 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
49867c282c0SJohannes Berg 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
499a93e3644SJohannes Berg 	mutex_init(&sta->ampdu_mlme.mtx);
50087f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH
501433f5bc1SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
502433f5bc1SJohannes Berg 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
503433f5bc1SJohannes Berg 		if (!sta->mesh)
504433f5bc1SJohannes Berg 			goto free;
5054c02d62fSKees Cook 		sta->mesh->plink_sta = sta;
506433f5bc1SJohannes Berg 		spin_lock_init(&sta->mesh->plink_lock);
50745d33746SBaligh Gasmi 		if (!sdata->u.mesh.user_mpm)
5084c02d62fSKees Cook 			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
5094c02d62fSKees Cook 				    0);
510433f5bc1SJohannes Berg 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
511433f5bc1SJohannes Berg 	}
51287f59c70SThomas Pedersen #endif
51307346f81SJohannes Berg 
514ac100ce5SJohannes Berg 	memcpy(sta->addr, addr, ETH_ALEN);
51517741cdcSJohannes Berg 	memcpy(sta->sta.addr, addr, ETH_ALEN);
516480dd46bSMaxim Altshul 	sta->sta.max_rx_aggregation_subframes =
517480dd46bSMaxim Altshul 		local->hw.max_rx_aggregation_subframes;
518480dd46bSMaxim Altshul 
519046d2e7cSSriram R 	/* TODO link specific alloc and assignments for MLO Link STA */
520046d2e7cSSriram R 
52196fc6efbSAlexander Wetzel 	/* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
52296fc6efbSAlexander Wetzel 	 * The Tx path starts to use a key as soon as the key slot ptk_idx
52396fc6efbSAlexander Wetzel 	 * references to is not NULL. To not use the initial Rx-only key
52496fc6efbSAlexander Wetzel 	 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
52596fc6efbSAlexander Wetzel 	 * which always will refer to a NULL key.
52696fc6efbSAlexander Wetzel 	 */
52796fc6efbSAlexander Wetzel 	BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
52896fc6efbSAlexander Wetzel 	sta->ptk_idx = INVALID_PTK_KEYIDX;
52996fc6efbSAlexander Wetzel 
5300f9c5a61SJohannes Berg 
5313a11ce08SJohannes Berg 	ieee80211_init_frag_cache(&sta->frags);
5323a11ce08SJohannes Berg 
53371ec375cSJohannes Berg 	sta->sta_state = IEEE80211_STA_NONE;
53471ec375cSJohannes Berg 
535b6da911bSLiad Kaufman 	/* Mark TID as unreserved */
536b6da911bSLiad Kaufman 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
537b6da911bSLiad Kaufman 
53884b00607SArnd Bergmann 	sta->last_connected = ktime_get_seconds();
539541a45a1SBruno Randolf 
540ba8c3d6fSFelix Fietkau 	if (local->ops->wake_tx_queue) {
541ba8c3d6fSFelix Fietkau 		void *txq_data;
542ba8c3d6fSFelix Fietkau 		int size = sizeof(struct txq_info) +
543ba8c3d6fSFelix Fietkau 			   ALIGN(hw->txq_data_size, sizeof(void *));
544ba8c3d6fSFelix Fietkau 
545ba8c3d6fSFelix Fietkau 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
546ba8c3d6fSFelix Fietkau 		if (!txq_data)
547ba8c3d6fSFelix Fietkau 			goto free;
548ba8c3d6fSFelix Fietkau 
549ba8c3d6fSFelix Fietkau 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
550ba8c3d6fSFelix Fietkau 			struct txq_info *txq = txq_data + i * size;
551ba8c3d6fSFelix Fietkau 
552adf8ed01SJohannes Berg 			/* might not do anything for the bufferable MMPDU TXQ */
553fa962b92SMichal Kazior 			ieee80211_txq_init(sdata, sta, txq, i);
554abfbc3afSJohannes Berg 		}
555ba8c3d6fSFelix Fietkau 	}
556ba8c3d6fSFelix Fietkau 
557ba8c3d6fSFelix Fietkau 	if (sta_prepare_rate_control(local, sta, gfp))
558ba8c3d6fSFelix Fietkau 		goto free_txq;
559f0706e82SJiri Benc 
560b4809e94SToke Høiland-Jørgensen 
561948d887dSJohannes Berg 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
562948d887dSJohannes Berg 		skb_queue_head_init(&sta->ps_tx_buf[i]);
563948d887dSJohannes Berg 		skb_queue_head_init(&sta->tx_filtered[i]);
5642433647bSToke Høiland-Jørgensen 		init_airtime_info(&sta->airtime[i], &local->airtime[i]);
565948d887dSJohannes Berg 	}
56673651ee6SJohannes Berg 
5675a306f58SJohannes Berg 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
5684be929beSAlexey Dobriyan 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
569cccaec98SSenthil Balasubramanian 
570bd718fc1SJohannes Berg 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
571bd718fc1SJohannes Berg 		u32 mandatory = 0;
572bd718fc1SJohannes Berg 		int r;
573bd718fc1SJohannes Berg 
574bd718fc1SJohannes Berg 		if (!hw->wiphy->bands[i])
575bd718fc1SJohannes Berg 			continue;
576bd718fc1SJohannes Berg 
577bd718fc1SJohannes Berg 		switch (i) {
578bd718fc1SJohannes Berg 		case NL80211_BAND_2GHZ:
57963fa0426SSrinivasan Raju 		case NL80211_BAND_LC:
580bd718fc1SJohannes Berg 			/*
581bd718fc1SJohannes Berg 			 * We use both here, even if we cannot really know for
582bd718fc1SJohannes Berg 			 * sure the station will support both, but the only use
583bd718fc1SJohannes Berg 			 * for this is when we don't know anything yet and send
584bd718fc1SJohannes Berg 			 * management frames, and then we'll pick the lowest
585bd718fc1SJohannes Berg 			 * possible rate anyway.
586bd718fc1SJohannes Berg 			 * If we don't include _G here, we cannot find a rate
587bd718fc1SJohannes Berg 			 * in P2P, and thus trigger the WARN_ONCE() in rate.c
588bd718fc1SJohannes Berg 			 */
589bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_B |
590bd718fc1SJohannes Berg 				    IEEE80211_RATE_MANDATORY_G;
591bd718fc1SJohannes Berg 			break;
592bd718fc1SJohannes Berg 		case NL80211_BAND_5GHZ:
593bd718fc1SJohannes Berg 			mandatory = IEEE80211_RATE_MANDATORY_A;
594bd718fc1SJohannes Berg 			break;
595bd718fc1SJohannes Berg 		case NL80211_BAND_60GHZ:
596bd718fc1SJohannes Berg 			WARN_ON(1);
597bd718fc1SJohannes Berg 			mandatory = 0;
598bd718fc1SJohannes Berg 			break;
599bd718fc1SJohannes Berg 		}
600bd718fc1SJohannes Berg 
601bd718fc1SJohannes Berg 		for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
602bd718fc1SJohannes Berg 			struct ieee80211_rate *rate;
603bd718fc1SJohannes Berg 
604bd718fc1SJohannes Berg 			rate = &hw->wiphy->bands[i]->bitrates[r];
605bd718fc1SJohannes Berg 
606bd718fc1SJohannes Berg 			if (!(rate->flags & mandatory))
607bd718fc1SJohannes Berg 				continue;
608046d2e7cSSriram R 			sta->sta.deflink.supp_rates[i] |= BIT(r);
609bd718fc1SJohannes Berg 		}
610bd718fc1SJohannes Berg 	}
611bd718fc1SJohannes Berg 
612af0ed69bSJohannes Berg 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
613687da132SEmmanuel Grumbach 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
614687da132SEmmanuel Grumbach 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
61521a8e9ddSMohammed Shafi Shajakhan 		struct ieee80211_supported_band *sband;
61621a8e9ddSMohammed Shafi Shajakhan 		u8 smps;
61721a8e9ddSMohammed Shafi Shajakhan 
61821a8e9ddSMohammed Shafi Shajakhan 		sband = ieee80211_get_sband(sdata);
61921a8e9ddSMohammed Shafi Shajakhan 		if (!sband)
62021a8e9ddSMohammed Shafi Shajakhan 			goto free_txq;
62121a8e9ddSMohammed Shafi Shajakhan 
62221a8e9ddSMohammed Shafi Shajakhan 		smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
623687da132SEmmanuel Grumbach 			IEEE80211_HT_CAP_SM_PS_SHIFT;
624687da132SEmmanuel Grumbach 		/*
625687da132SEmmanuel Grumbach 		 * Assume that hostapd advertises our caps in the beacon and
626687da132SEmmanuel Grumbach 		 * this is the known_smps_mode for a station that just assciated
627687da132SEmmanuel Grumbach 		 */
628687da132SEmmanuel Grumbach 		switch (smps) {
629687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DISABLED:
630687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
631687da132SEmmanuel Grumbach 			break;
632687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_STATIC:
633687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
634687da132SEmmanuel Grumbach 			break;
635687da132SEmmanuel Grumbach 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
636687da132SEmmanuel Grumbach 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
637687da132SEmmanuel Grumbach 			break;
638687da132SEmmanuel Grumbach 		default:
639687da132SEmmanuel Grumbach 			WARN_ON(1);
640687da132SEmmanuel Grumbach 		}
641687da132SEmmanuel Grumbach 	}
642af0ed69bSJohannes Berg 
6436e0456b5SFelix Fietkau 	sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
6446e0456b5SFelix Fietkau 
645484a54c2SToke Høiland-Jørgensen 	sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD;
646484a54c2SToke Høiland-Jørgensen 	sta->cparams.target = MS2TIME(20);
647484a54c2SToke Høiland-Jørgensen 	sta->cparams.interval = MS2TIME(100);
648484a54c2SToke Høiland-Jørgensen 	sta->cparams.ecn = true;
649dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_selector = 0;
650dfcb63ceSToke Høiland-Jørgensen 	sta->cparams.ce_threshold_mask = 0;
651484a54c2SToke Høiland-Jørgensen 
652bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
653ef04a297SJohannes Berg 
654abfbc3afSJohannes Berg 	return sta;
655ba8c3d6fSFelix Fietkau 
656ba8c3d6fSFelix Fietkau free_txq:
657ba8c3d6fSFelix Fietkau 	if (sta->sta.txq[0])
658ba8c3d6fSFelix Fietkau 		kfree(to_txq_info(sta->sta.txq[0]));
659ba8c3d6fSFelix Fietkau free:
660cb71f1d1SJohannes Berg 	sta_info_free_link(&sta->deflink);
661433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH
662433f5bc1SJohannes Berg 	kfree(sta->mesh);
663433f5bc1SJohannes Berg #endif
664ba8c3d6fSFelix Fietkau 	kfree(sta);
665ba8c3d6fSFelix Fietkau 	return NULL;
66673651ee6SJohannes Berg }
66773651ee6SJohannes Berg 
6688c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta)
66934e89507SJohannes Berg {
67034e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
67134e89507SJohannes Berg 
67203e4497eSJohannes Berg 	/*
67303e4497eSJohannes Berg 	 * Can't be a WARN_ON because it can be triggered through a race:
67403e4497eSJohannes Berg 	 * something inserts a STA (on one CPU) without holding the RTNL
67503e4497eSJohannes Berg 	 * and another CPU turns off the net device.
67603e4497eSJohannes Berg 	 */
6778c71df7aSGuy Eilam 	if (unlikely(!ieee80211_sdata_running(sdata)))
6788c71df7aSGuy Eilam 		return -ENETDOWN;
67903e4497eSJohannes Berg 
680b203ca39SJoe Perches 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
681deebea0aSYueHaibing 		    !is_valid_ether_addr(sta->sta.addr)))
6828c71df7aSGuy Eilam 		return -EINVAL;
6838c71df7aSGuy Eilam 
68483e7e4ceSHerbert Xu 	/* The RCU read lock is required by rhashtable due to
68583e7e4ceSHerbert Xu 	 * asynchronous resize/rehash.  We also require the mutex
68683e7e4ceSHerbert Xu 	 * for correctness.
68731104891SJohannes Berg 	 */
68831104891SJohannes Berg 	rcu_read_lock();
68931104891SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
69031104891SJohannes Berg 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
69131104891SJohannes Berg 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
69231104891SJohannes Berg 		rcu_read_unlock();
69331104891SJohannes Berg 		return -ENOTUNIQ;
69431104891SJohannes Berg 	}
69531104891SJohannes Berg 	rcu_read_unlock();
69631104891SJohannes Berg 
6978c71df7aSGuy Eilam 	return 0;
69893e5deb1SJohannes Berg }
69944213b5eSJohannes Berg 
700f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local,
701f09603a2SJohannes Berg 				     struct ieee80211_sub_if_data *sdata,
702f09603a2SJohannes Berg 				     struct sta_info *sta)
703f09603a2SJohannes Berg {
704f09603a2SJohannes Berg 	enum ieee80211_sta_state state;
705f09603a2SJohannes Berg 	int err = 0;
706f09603a2SJohannes Berg 
707f09603a2SJohannes Berg 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
708f09603a2SJohannes Berg 		err = drv_sta_state(local, sdata, sta, state, state + 1);
709f09603a2SJohannes Berg 		if (err)
710f09603a2SJohannes Berg 			break;
711f09603a2SJohannes Berg 	}
712f09603a2SJohannes Berg 
713f09603a2SJohannes Berg 	if (!err) {
714a4ec45a4SJohannes Berg 		/*
715a4ec45a4SJohannes Berg 		 * Drivers using legacy sta_add/sta_remove callbacks only
716a4ec45a4SJohannes Berg 		 * get uploaded set to true after sta_add is called.
717a4ec45a4SJohannes Berg 		 */
718a4ec45a4SJohannes Berg 		if (!local->ops->sta_add)
719f09603a2SJohannes Berg 			sta->uploaded = true;
720f09603a2SJohannes Berg 		return 0;
721f09603a2SJohannes Berg 	}
722f09603a2SJohannes Berg 
723f09603a2SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
724bdcbd8e0SJohannes Berg 		sdata_info(sdata,
725bdcbd8e0SJohannes Berg 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
726bdcbd8e0SJohannes Berg 			   sta->sta.addr, state + 1, err);
727f09603a2SJohannes Berg 		err = 0;
728f09603a2SJohannes Berg 	}
729f09603a2SJohannes Berg 
730f09603a2SJohannes Berg 	/* unwind on error */
731f09603a2SJohannes Berg 	for (; state > IEEE80211_STA_NOTEXIST; state--)
732f09603a2SJohannes Berg 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
733f09603a2SJohannes Berg 
734f09603a2SJohannes Berg 	return err;
735f09603a2SJohannes Berg }
736f09603a2SJohannes Berg 
737d405fd8cSGregory Greenman static void
738d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
739d405fd8cSGregory Greenman {
740d405fd8cSGregory Greenman 	struct ieee80211_local *local = sdata->local;
741d405fd8cSGregory Greenman 	bool allow_p2p_go_ps = sdata->vif.p2p;
742d405fd8cSGregory Greenman 	struct sta_info *sta;
743d405fd8cSGregory Greenman 
744d405fd8cSGregory Greenman 	rcu_read_lock();
745d405fd8cSGregory Greenman 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
746d405fd8cSGregory Greenman 		if (sdata != sta->sdata ||
747d405fd8cSGregory Greenman 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
748d405fd8cSGregory Greenman 			continue;
749d405fd8cSGregory Greenman 		if (!sta->sta.support_p2p_ps) {
750d405fd8cSGregory Greenman 			allow_p2p_go_ps = false;
751d405fd8cSGregory Greenman 			break;
752d405fd8cSGregory Greenman 		}
753d405fd8cSGregory Greenman 	}
754d405fd8cSGregory Greenman 	rcu_read_unlock();
755d405fd8cSGregory Greenman 
756d405fd8cSGregory Greenman 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
757d405fd8cSGregory Greenman 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
7587b7090b4SJohannes Berg 		ieee80211_link_info_change_notify(sdata, 0, BSS_CHANGED_P2P_PS);
759d405fd8cSGregory Greenman 	}
760d405fd8cSGregory Greenman }
761d405fd8cSGregory Greenman 
76234e89507SJohannes Berg /*
7638c71df7aSGuy Eilam  * should be called with sta_mtx locked
7648c71df7aSGuy Eilam  * this function replaces the mutex lock
7658c71df7aSGuy Eilam  * with a RCU lock
7668c71df7aSGuy Eilam  */
7674d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
7688c71df7aSGuy Eilam {
7698c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
7708c71df7aSGuy Eilam 	struct ieee80211_sub_if_data *sdata = sta->sdata;
7710c2e3842SKoen Vandeputte 	struct station_info *sinfo = NULL;
7728c71df7aSGuy Eilam 	int err = 0;
7738c71df7aSGuy Eilam 
7748c71df7aSGuy Eilam 	lockdep_assert_held(&local->sta_mtx);
7758c71df7aSGuy Eilam 
7767852e361SJohannes Berg 	/* check if STA exists already */
7777852e361SJohannes Berg 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
7784d33960bSJohannes Berg 		err = -EEXIST;
7798f9dcc29SAhmed Zaki 		goto out_cleanup;
78034e89507SJohannes Berg 	}
78134e89507SJohannes Berg 
7820c2e3842SKoen Vandeputte 	sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
7830c2e3842SKoen Vandeputte 	if (!sinfo) {
7840c2e3842SKoen Vandeputte 		err = -ENOMEM;
7858f9dcc29SAhmed Zaki 		goto out_cleanup;
7860c2e3842SKoen Vandeputte 	}
7870c2e3842SKoen Vandeputte 
7884d33960bSJohannes Berg 	local->num_sta++;
7894d33960bSJohannes Berg 	local->sta_generation++;
7904d33960bSJohannes Berg 	smp_mb();
7914d33960bSJohannes Berg 
7925108ca82SJohannes Berg 	/* simplify things and don't accept BA sessions yet */
7935108ca82SJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
7945108ca82SJohannes Berg 
7954d33960bSJohannes Berg 	/* make the station visible */
79662b14b24SJohannes Berg 	err = sta_info_hash_add(local, sta);
79762b14b24SJohannes Berg 	if (err)
79862b14b24SJohannes Berg 		goto out_drop_sta;
7994d33960bSJohannes Berg 
8002bad7748SArik Nemtsov 	list_add_tail_rcu(&sta->list, &local->sta_list);
80183d5cc01SJohannes Berg 
8024dde3c36SMordechay Goodstein 	/* update channel context before notifying the driver about state
8034dde3c36SMordechay Goodstein 	 * change, this enables driver using the updated channel context right away.
8044dde3c36SMordechay Goodstein 	 */
8054dde3c36SMordechay Goodstein 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
8064dde3c36SMordechay Goodstein 		ieee80211_recalc_min_chandef(sta->sdata);
8074dde3c36SMordechay Goodstein 		if (!sta->sta.support_p2p_ps)
8084dde3c36SMordechay Goodstein 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
8094dde3c36SMordechay Goodstein 	}
8104dde3c36SMordechay Goodstein 
8115108ca82SJohannes Berg 	/* notify driver */
8125108ca82SJohannes Berg 	err = sta_info_insert_drv_state(local, sdata, sta);
8135108ca82SJohannes Berg 	if (err)
8145108ca82SJohannes Berg 		goto out_remove;
8155108ca82SJohannes Berg 
81683d5cc01SJohannes Berg 	set_sta_flag(sta, WLAN_STA_INSERTED);
817d405fd8cSGregory Greenman 
8185108ca82SJohannes Berg 	/* accept BA sessions now */
8195108ca82SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
8204d33960bSJohannes Berg 
8214d33960bSJohannes Berg 	ieee80211_sta_debugfs_add(sta);
8224d33960bSJohannes Berg 	rate_control_add_sta_debugfs(sta);
8234d33960bSJohannes Berg 
8240ef049dcSArnd Bergmann 	sinfo->generation = local->sta_generation;
8250ef049dcSArnd Bergmann 	cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
8260ef049dcSArnd Bergmann 	kfree(sinfo);
827d0709a65SJohannes Berg 
828bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
829f0706e82SJiri Benc 
83034e89507SJohannes Berg 	/* move reference to rcu-protected */
83134e89507SJohannes Berg 	rcu_read_lock();
83234e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
833e9f207f0SJiri Benc 
83473651ee6SJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif))
83573651ee6SJohannes Berg 		mesh_accept_plinks_update(sdata);
83673651ee6SJohannes Berg 
83773651ee6SJohannes Berg 	return 0;
8385108ca82SJohannes Berg  out_remove:
8395108ca82SJohannes Berg 	sta_info_hash_del(local, sta);
8405108ca82SJohannes Berg 	list_del_rcu(&sta->list);
84162b14b24SJohannes Berg  out_drop_sta:
8425108ca82SJohannes Berg 	local->num_sta--;
8435108ca82SJohannes Berg 	synchronize_net();
8448f9dcc29SAhmed Zaki  out_cleanup:
8457bc40aedSJohannes Berg 	cleanup_single_sta(sta);
8464d33960bSJohannes Berg 	mutex_unlock(&local->sta_mtx);
847ea32f065SSudip Mukherjee 	kfree(sinfo);
8484d33960bSJohannes Berg 	rcu_read_lock();
8494d33960bSJohannes Berg 	return err;
8508c71df7aSGuy Eilam }
8518c71df7aSGuy Eilam 
8528c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
8538c71df7aSGuy Eilam {
8548c71df7aSGuy Eilam 	struct ieee80211_local *local = sta->local;
855308f7fcfSZhao, Gang 	int err;
8568c71df7aSGuy Eilam 
8574d33960bSJohannes Berg 	might_sleep();
8584d33960bSJohannes Berg 
85931104891SJohannes Berg 	mutex_lock(&local->sta_mtx);
86031104891SJohannes Berg 
8618c71df7aSGuy Eilam 	err = sta_info_insert_check(sta);
8628c71df7aSGuy Eilam 	if (err) {
8637bc40aedSJohannes Berg 		sta_info_free(local, sta);
86431104891SJohannes Berg 		mutex_unlock(&local->sta_mtx);
8658c71df7aSGuy Eilam 		rcu_read_lock();
8667bc40aedSJohannes Berg 		return err;
8678c71df7aSGuy Eilam 	}
8688c71df7aSGuy Eilam 
8697bc40aedSJohannes Berg 	return sta_info_insert_finish(sta);
870f0706e82SJiri Benc }
871f0706e82SJiri Benc 
87234e89507SJohannes Berg int sta_info_insert(struct sta_info *sta)
87334e89507SJohannes Berg {
87434e89507SJohannes Berg 	int err = sta_info_insert_rcu(sta);
87534e89507SJohannes Berg 
87634e89507SJohannes Berg 	rcu_read_unlock();
87734e89507SJohannes Berg 
87834e89507SJohannes Berg 	return err;
87934e89507SJohannes Berg }
88034e89507SJohannes Berg 
881d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id)
882004c872eSJohannes Berg {
883004c872eSJohannes Berg 	/*
884004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
885004c872eSJohannes Berg 	 * so this line may not be changed to use the __set_bit() format.
886004c872eSJohannes Berg 	 */
887d012a605SMarco Porsch 	tim[id / 8] |= (1 << (id % 8));
888004c872eSJohannes Berg }
889004c872eSJohannes Berg 
890d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id)
891004c872eSJohannes Berg {
892004c872eSJohannes Berg 	/*
893004c872eSJohannes Berg 	 * This format has been mandated by the IEEE specifications,
894004c872eSJohannes Berg 	 * so this line may not be changed to use the __clear_bit() format.
895004c872eSJohannes Berg 	 */
896d012a605SMarco Porsch 	tim[id / 8] &= ~(1 << (id % 8));
897004c872eSJohannes Berg }
898004c872eSJohannes Berg 
8993d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id)
9003d5839b6SIlan Peer {
9013d5839b6SIlan Peer 	/*
9023d5839b6SIlan Peer 	 * This format has been mandated by the IEEE specifications,
9033d5839b6SIlan Peer 	 * so this line may not be changed to use the test_bit() format.
9043d5839b6SIlan Peer 	 */
9053d5839b6SIlan Peer 	return tim[id / 8] & (1 << (id % 8));
9063d5839b6SIlan Peer }
9073d5839b6SIlan Peer 
908948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac)
909004c872eSJohannes Berg {
910948d887dSJohannes Berg 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
911948d887dSJohannes Berg 	switch (ac) {
912948d887dSJohannes Berg 	case IEEE80211_AC_VO:
913948d887dSJohannes Berg 		return BIT(6) | BIT(7);
914948d887dSJohannes Berg 	case IEEE80211_AC_VI:
915948d887dSJohannes Berg 		return BIT(4) | BIT(5);
916948d887dSJohannes Berg 	case IEEE80211_AC_BE:
917948d887dSJohannes Berg 		return BIT(0) | BIT(3);
918948d887dSJohannes Berg 	case IEEE80211_AC_BK:
919948d887dSJohannes Berg 		return BIT(1) | BIT(2);
920948d887dSJohannes Berg 	default:
921948d887dSJohannes Berg 		WARN_ON(1);
922948d887dSJohannes Berg 		return 0;
923d0709a65SJohannes Berg 	}
924004c872eSJohannes Berg }
925004c872eSJohannes Berg 
9269b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
927004c872eSJohannes Berg {
928c868cb35SJohannes Berg 	struct ieee80211_local *local = sta->local;
929d012a605SMarco Porsch 	struct ps_data *ps;
930948d887dSJohannes Berg 	bool indicate_tim = false;
931948d887dSJohannes Berg 	u8 ignore_for_tim = sta->sta.uapsd_queues;
932948d887dSJohannes Berg 	int ac;
933a69bd8e6SBob Copeland 	u16 id = sta->sta.aid;
934004c872eSJohannes Berg 
935d012a605SMarco Porsch 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
936d012a605SMarco Porsch 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
937c868cb35SJohannes Berg 		if (WARN_ON_ONCE(!sta->sdata->bss))
938c868cb35SJohannes Berg 			return;
9393e122be0SJohannes Berg 
940d012a605SMarco Porsch 		ps = &sta->sdata->bss->ps;
9413f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH
9423f52b7e3SMarco Porsch 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
9433f52b7e3SMarco Porsch 		ps = &sta->sdata->u.mesh.ps;
9443f52b7e3SMarco Porsch #endif
945d012a605SMarco Porsch 	} else {
946d012a605SMarco Porsch 		return;
947d012a605SMarco Porsch 	}
948d012a605SMarco Porsch 
949c868cb35SJohannes Berg 	/* No need to do anything if the driver does all */
950d98937f4SEmmanuel Grumbach 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
951c868cb35SJohannes Berg 		return;
952004c872eSJohannes Berg 
953c868cb35SJohannes Berg 	if (sta->dead)
954c868cb35SJohannes Berg 		goto done;
9553e122be0SJohannes Berg 
956948d887dSJohannes Berg 	/*
957948d887dSJohannes Berg 	 * If all ACs are delivery-enabled then we should build
958948d887dSJohannes Berg 	 * the TIM bit for all ACs anyway; if only some are then
959948d887dSJohannes Berg 	 * we ignore those and build the TIM bit using only the
960948d887dSJohannes Berg 	 * non-enabled ones.
961948d887dSJohannes Berg 	 */
962948d887dSJohannes Berg 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
963948d887dSJohannes Berg 		ignore_for_tim = 0;
964948d887dSJohannes Berg 
9659b7a86f3SJohannes Berg 	if (ignore_pending)
9669b7a86f3SJohannes Berg 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
9679b7a86f3SJohannes Berg 
968948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
969948d887dSJohannes Berg 		unsigned long tids;
970948d887dSJohannes Berg 
971f438ceb8SEmmanuel Grumbach 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
972948d887dSJohannes Berg 			continue;
973948d887dSJohannes Berg 
974948d887dSJohannes Berg 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
975948d887dSJohannes Berg 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
976948d887dSJohannes Berg 		if (indicate_tim)
977948d887dSJohannes Berg 			break;
978948d887dSJohannes Berg 
979948d887dSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
980948d887dSJohannes Berg 
981948d887dSJohannes Berg 		indicate_tim |=
982948d887dSJohannes Berg 			sta->driver_buffered_tids & tids;
983ba8c3d6fSFelix Fietkau 		indicate_tim |=
984ba8c3d6fSFelix Fietkau 			sta->txq_buffered_tids & tids;
985004c872eSJohannes Berg 	}
986004c872eSJohannes Berg 
987c868cb35SJohannes Berg  done:
98865f704a5SJohannes Berg 	spin_lock_bh(&local->tim_lock);
989004c872eSJohannes Berg 
9903d5839b6SIlan Peer 	if (indicate_tim == __bss_tim_get(ps->tim, id))
9913d5839b6SIlan Peer 		goto out_unlock;
9923d5839b6SIlan Peer 
993948d887dSJohannes Berg 	if (indicate_tim)
994d012a605SMarco Porsch 		__bss_tim_set(ps->tim, id);
995c868cb35SJohannes Berg 	else
996d012a605SMarco Porsch 		__bss_tim_clear(ps->tim, id);
9973e122be0SJohannes Berg 
9989b7a86f3SJohannes Berg 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
999c868cb35SJohannes Berg 		local->tim_in_locked_section = true;
1000948d887dSJohannes Berg 		drv_set_tim(local, &sta->sta, indicate_tim);
1001c868cb35SJohannes Berg 		local->tim_in_locked_section = false;
1002004c872eSJohannes Berg 	}
1003004c872eSJohannes Berg 
10043d5839b6SIlan Peer out_unlock:
100565f704a5SJohannes Berg 	spin_unlock_bh(&local->tim_lock);
1006004c872eSJohannes Berg }
1007004c872eSJohannes Berg 
10089b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta)
10099b7a86f3SJohannes Berg {
10109b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, false);
10119b7a86f3SJohannes Berg }
10129b7a86f3SJohannes Berg 
1013cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1014f0706e82SJiri Benc {
1015e039fa4aSJohannes Berg 	struct ieee80211_tx_info *info;
1016f0706e82SJiri Benc 	int timeout;
1017f0706e82SJiri Benc 
1018f0706e82SJiri Benc 	if (!skb)
1019cd0b8d89SJohannes Berg 		return false;
1020f0706e82SJiri Benc 
1021e039fa4aSJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1022f0706e82SJiri Benc 
1023f0706e82SJiri Benc 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
102457c4d7b4SJohannes Berg 	timeout = (sta->listen_interval *
102557c4d7b4SJohannes Berg 		   sta->sdata->vif.bss_conf.beacon_int *
102657c4d7b4SJohannes Berg 		   32 / 15625) * HZ;
1027f0706e82SJiri Benc 	if (timeout < STA_TX_BUFFER_EXPIRE)
1028f0706e82SJiri Benc 		timeout = STA_TX_BUFFER_EXPIRE;
1029e039fa4aSJohannes Berg 	return time_after(jiffies, info->control.jiffies + timeout);
1030f0706e82SJiri Benc }
1031f0706e82SJiri Benc 
1032f0706e82SJiri Benc 
1033948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1034948d887dSJohannes Berg 						struct sta_info *sta, int ac)
1035f0706e82SJiri Benc {
1036f0706e82SJiri Benc 	unsigned long flags;
1037f0706e82SJiri Benc 	struct sk_buff *skb;
1038f0706e82SJiri Benc 
103960750397SJohannes Berg 	/*
104060750397SJohannes Berg 	 * First check for frames that should expire on the filtered
104160750397SJohannes Berg 	 * queue. Frames here were rejected by the driver and are on
104260750397SJohannes Berg 	 * a separate queue to avoid reordering with normal PS-buffered
104360750397SJohannes Berg 	 * frames. They also aren't accounted for right now in the
104460750397SJohannes Berg 	 * total_ps_buffered counter.
104560750397SJohannes Berg 	 */
1046f0706e82SJiri Benc 	for (;;) {
1047948d887dSJohannes Berg 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1048948d887dSJohannes Berg 		skb = skb_peek(&sta->tx_filtered[ac]);
104957c4d7b4SJohannes Berg 		if (sta_info_buffer_expired(sta, skb))
1050948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
1051836341a7SJohannes Berg 		else
1052f0706e82SJiri Benc 			skb = NULL;
1053948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1054f0706e82SJiri Benc 
105560750397SJohannes Berg 		/*
105660750397SJohannes Berg 		 * Frames are queued in order, so if this one
105760750397SJohannes Berg 		 * hasn't expired yet we can stop testing. If
105860750397SJohannes Berg 		 * we actually reached the end of the queue we
105960750397SJohannes Berg 		 * also need to stop, of course.
106060750397SJohannes Berg 		 */
106160750397SJohannes Berg 		if (!skb)
106260750397SJohannes Berg 			break;
1063d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
106460750397SJohannes Berg 	}
106560750397SJohannes Berg 
106660750397SJohannes Berg 	/*
106760750397SJohannes Berg 	 * Now also check the normal PS-buffered queue, this will
106860750397SJohannes Berg 	 * only find something if the filtered queue was emptied
106960750397SJohannes Berg 	 * since the filtered frames are all before the normal PS
107060750397SJohannes Berg 	 * buffered frames.
107160750397SJohannes Berg 	 */
1072f0706e82SJiri Benc 	for (;;) {
1073948d887dSJohannes Berg 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1074948d887dSJohannes Berg 		skb = skb_peek(&sta->ps_tx_buf[ac]);
1075f0706e82SJiri Benc 		if (sta_info_buffer_expired(sta, skb))
1076948d887dSJohannes Berg 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1077f0706e82SJiri Benc 		else
1078f0706e82SJiri Benc 			skb = NULL;
1079948d887dSJohannes Berg 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1080f0706e82SJiri Benc 
108160750397SJohannes Berg 		/*
108260750397SJohannes Berg 		 * frames are queued in order, so if this one
108360750397SJohannes Berg 		 * hasn't expired yet (or we reached the end of
108460750397SJohannes Berg 		 * the queue) we can stop testing
108560750397SJohannes Berg 		 */
1086836341a7SJohannes Berg 		if (!skb)
1087836341a7SJohannes Berg 			break;
1088836341a7SJohannes Berg 
1089f0706e82SJiri Benc 		local->total_ps_buffered--;
1090bdcbd8e0SJohannes Berg 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1091bdcbd8e0SJohannes Berg 		       sta->sta.addr);
1092d4fa14cdSFelix Fietkau 		ieee80211_free_txskb(&local->hw, skb);
1093f0706e82SJiri Benc 	}
10943393a608SJuuso Oikarinen 
109560750397SJohannes Berg 	/*
109660750397SJohannes Berg 	 * Finally, recalculate the TIM bit for this station -- it might
109760750397SJohannes Berg 	 * now be clear because the station was too slow to retrieve its
109860750397SJohannes Berg 	 * frames.
109960750397SJohannes Berg 	 */
110060750397SJohannes Berg 	sta_info_recalc_tim(sta);
110160750397SJohannes Berg 
110260750397SJohannes Berg 	/*
110360750397SJohannes Berg 	 * Return whether there are any frames still buffered, this is
110460750397SJohannes Berg 	 * used to check whether the cleanup timer still needs to run,
110560750397SJohannes Berg 	 * if there are no frames we don't need to rearm the timer.
110660750397SJohannes Berg 	 */
1107948d887dSJohannes Berg 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1108948d887dSJohannes Berg 		 skb_queue_empty(&sta->tx_filtered[ac]));
1109948d887dSJohannes Berg }
1110948d887dSJohannes Berg 
1111948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1112948d887dSJohannes Berg 					     struct sta_info *sta)
1113948d887dSJohannes Berg {
1114948d887dSJohannes Berg 	bool have_buffered = false;
1115948d887dSJohannes Berg 	int ac;
1116948d887dSJohannes Berg 
11173f52b7e3SMarco Porsch 	/* This is only necessary for stations on BSS/MBSS interfaces */
11183f52b7e3SMarco Porsch 	if (!sta->sdata->bss &&
11193f52b7e3SMarco Porsch 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
1120948d887dSJohannes Berg 		return false;
1121948d887dSJohannes Berg 
1122948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1123948d887dSJohannes Berg 		have_buffered |=
1124948d887dSJohannes Berg 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1125948d887dSJohannes Berg 
1126948d887dSJohannes Berg 	return have_buffered;
1127f0706e82SJiri Benc }
1128f0706e82SJiri Benc 
1129d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
113034e89507SJohannes Berg {
113134e89507SJohannes Berg 	struct ieee80211_local *local;
113234e89507SJohannes Berg 	struct ieee80211_sub_if_data *sdata;
11336d10e46bSJohannes Berg 	int ret;
113434e89507SJohannes Berg 
113534e89507SJohannes Berg 	might_sleep();
113634e89507SJohannes Berg 
113734e89507SJohannes Berg 	if (!sta)
113834e89507SJohannes Berg 		return -ENOENT;
113934e89507SJohannes Berg 
114034e89507SJohannes Berg 	local = sta->local;
114134e89507SJohannes Berg 	sdata = sta->sdata;
114234e89507SJohannes Berg 
114383d5cc01SJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
114483d5cc01SJohannes Berg 
1145098a6070SJohannes Berg 	/*
1146098a6070SJohannes Berg 	 * Before removing the station from the driver and
1147098a6070SJohannes Berg 	 * rate control, it might still start new aggregation
1148098a6070SJohannes Berg 	 * sessions -- block that to make sure the tear-down
1149098a6070SJohannes Berg 	 * will be sufficient.
1150098a6070SJohannes Berg 	 */
1151c2c98fdeSJohannes Berg 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1152c82c4a80SJohannes Berg 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1153098a6070SJohannes Berg 
1154f59374ebSSara Sharon 	/*
1155f59374ebSSara Sharon 	 * Before removing the station from the driver there might be pending
1156f59374ebSSara Sharon 	 * rx frames on RSS queues sent prior to the disassociation - wait for
1157f59374ebSSara Sharon 	 * all such frames to be processed.
1158f59374ebSSara Sharon 	 */
1159f59374ebSSara Sharon 	drv_sync_rx_queues(local, sta);
1160f59374ebSSara Sharon 
116134e89507SJohannes Berg 	ret = sta_info_hash_del(local, sta);
1162b01711beSJohannes Berg 	if (WARN_ON(ret))
116334e89507SJohannes Berg 		return ret;
116434e89507SJohannes Berg 
1165a7a6bdd0SArik Nemtsov 	/*
1166a7a6bdd0SArik Nemtsov 	 * for TDLS peers, make sure to return to the base channel before
1167a7a6bdd0SArik Nemtsov 	 * removal.
1168a7a6bdd0SArik Nemtsov 	 */
1169a7a6bdd0SArik Nemtsov 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1170a7a6bdd0SArik Nemtsov 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1171a7a6bdd0SArik Nemtsov 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1172a7a6bdd0SArik Nemtsov 	}
1173a7a6bdd0SArik Nemtsov 
1174794454ceSArik Nemtsov 	list_del_rcu(&sta->list);
1175ef044763SEliad Peller 	sta->removed = true;
11764d33960bSJohannes Berg 
11776a9d1b91SJohannes Berg 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
11786a9d1b91SJohannes Berg 
1179a710c816SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1180a710c816SJohannes Berg 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
1181a710c816SJohannes Berg 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1182a710c816SJohannes Berg 
1183d778207bSJohannes Berg 	return 0;
1184d778207bSJohannes Berg }
1185d778207bSJohannes Berg 
1186d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta)
1187d778207bSJohannes Berg {
1188d778207bSJohannes Berg 	struct ieee80211_local *local = sta->local;
1189d778207bSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
11900ef049dcSArnd Bergmann 	struct station_info *sinfo;
1191d778207bSJohannes Berg 	int ret;
1192d778207bSJohannes Berg 
1193d778207bSJohannes Berg 	/*
1194d778207bSJohannes Berg 	 * NOTE: This assumes at least synchronize_net() was done
1195d778207bSJohannes Berg 	 *	 after _part1 and before _part2!
1196d778207bSJohannes Berg 	 */
1197d778207bSJohannes Berg 
1198d778207bSJohannes Berg 	might_sleep();
1199d778207bSJohannes Berg 	lockdep_assert_held(&local->sta_mtx);
1200d778207bSJohannes Berg 
12015981fe5bSJohannes Berg 	if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1202b16798f5SJohannes Berg 		ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
1203b16798f5SJohannes Berg 		WARN_ON_ONCE(ret);
1204b16798f5SJohannes Berg 	}
1205b16798f5SJohannes Berg 
1206c8782078SJohannes Berg 	/* now keys can no longer be reached */
12076d10e46bSJohannes Berg 	ieee80211_free_sta_keys(local, sta);
120834e89507SJohannes Berg 
12099b7a86f3SJohannes Berg 	/* disable TIM bit - last chance to tell driver */
12109b7a86f3SJohannes Berg 	__sta_info_recalc_tim(sta, true);
12119b7a86f3SJohannes Berg 
121234e89507SJohannes Berg 	sta->dead = true;
121334e89507SJohannes Berg 
121434e89507SJohannes Berg 	local->num_sta--;
121534e89507SJohannes Berg 	local->sta_generation++;
121634e89507SJohannes Berg 
121783d5cc01SJohannes Berg 	while (sta->sta_state > IEEE80211_STA_NONE) {
1218f09603a2SJohannes Berg 		ret = sta_info_move_state(sta, sta->sta_state - 1);
1219f09603a2SJohannes Berg 		if (ret) {
122083d5cc01SJohannes Berg 			WARN_ON_ONCE(1);
122183d5cc01SJohannes Berg 			break;
122283d5cc01SJohannes Berg 		}
122383d5cc01SJohannes Berg 	}
1224d9a7ddb0SJohannes Berg 
1225f09603a2SJohannes Berg 	if (sta->uploaded) {
1226f09603a2SJohannes Berg 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1227f09603a2SJohannes Berg 				    IEEE80211_STA_NOTEXIST);
1228f09603a2SJohannes Berg 		WARN_ON_ONCE(ret != 0);
1229f09603a2SJohannes Berg 	}
123034e89507SJohannes Berg 
1231bdcbd8e0SJohannes Berg 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1232bdcbd8e0SJohannes Berg 
12330ef049dcSArnd Bergmann 	sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
12340ef049dcSArnd Bergmann 	if (sinfo)
12350fdf1493SJohannes Berg 		sta_set_sinfo(sta, sinfo, true);
12360ef049dcSArnd Bergmann 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
12370ef049dcSArnd Bergmann 	kfree(sinfo);
1238ec15e68bSJouni Malinen 
123934e89507SJohannes Berg 	ieee80211_sta_debugfs_remove(sta);
124034e89507SJohannes Berg 
12413a11ce08SJohannes Berg 	ieee80211_destroy_frag_cache(&sta->frags);
12423a11ce08SJohannes Berg 
1243d34ba216SJohannes Berg 	cleanup_single_sta(sta);
1244d778207bSJohannes Berg }
1245d778207bSJohannes Berg 
1246d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta)
1247d778207bSJohannes Berg {
1248d778207bSJohannes Berg 	int err = __sta_info_destroy_part1(sta);
1249d778207bSJohannes Berg 
1250d778207bSJohannes Berg 	if (err)
1251d778207bSJohannes Berg 		return err;
1252d778207bSJohannes Berg 
1253d778207bSJohannes Berg 	synchronize_net();
1254d778207bSJohannes Berg 
1255d778207bSJohannes Berg 	__sta_info_destroy_part2(sta);
125634e89507SJohannes Berg 
125734e89507SJohannes Berg 	return 0;
125834e89507SJohannes Berg }
125934e89507SJohannes Berg 
126034e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
126134e89507SJohannes Berg {
126234e89507SJohannes Berg 	struct sta_info *sta;
126334e89507SJohannes Berg 	int ret;
126434e89507SJohannes Berg 
126534e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
12667852e361SJohannes Berg 	sta = sta_info_get(sdata, addr);
126734e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
126834e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
126934e89507SJohannes Berg 
127034e89507SJohannes Berg 	return ret;
127134e89507SJohannes Berg }
127234e89507SJohannes Berg 
127334e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
127434e89507SJohannes Berg 			      const u8 *addr)
127534e89507SJohannes Berg {
127634e89507SJohannes Berg 	struct sta_info *sta;
127734e89507SJohannes Berg 	int ret;
127834e89507SJohannes Berg 
127934e89507SJohannes Berg 	mutex_lock(&sdata->local->sta_mtx);
12807852e361SJohannes Berg 	sta = sta_info_get_bss(sdata, addr);
128134e89507SJohannes Berg 	ret = __sta_info_destroy(sta);
128234e89507SJohannes Berg 	mutex_unlock(&sdata->local->sta_mtx);
128334e89507SJohannes Berg 
128434e89507SJohannes Berg 	return ret;
128534e89507SJohannes Berg }
1286f0706e82SJiri Benc 
128734f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t)
1288f0706e82SJiri Benc {
128934f11cd3SKees Cook 	struct ieee80211_local *local = from_timer(local, t, sta_cleanup);
1290f0706e82SJiri Benc 	struct sta_info *sta;
12913393a608SJuuso Oikarinen 	bool timer_needed = false;
1292f0706e82SJiri Benc 
1293d0709a65SJohannes Berg 	rcu_read_lock();
1294d0709a65SJohannes Berg 	list_for_each_entry_rcu(sta, &local->sta_list, list)
12953393a608SJuuso Oikarinen 		if (sta_info_cleanup_expire_buffered(local, sta))
12963393a608SJuuso Oikarinen 			timer_needed = true;
1297d0709a65SJohannes Berg 	rcu_read_unlock();
1298f0706e82SJiri Benc 
12995bb644a0SJohannes Berg 	if (local->quiescing)
13005bb644a0SJohannes Berg 		return;
13015bb644a0SJohannes Berg 
13023393a608SJuuso Oikarinen 	if (!timer_needed)
13033393a608SJuuso Oikarinen 		return;
13043393a608SJuuso Oikarinen 
130526d59535SJohannes Berg 	mod_timer(&local->sta_cleanup,
130626d59535SJohannes Berg 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1307f0706e82SJiri Benc }
1308f0706e82SJiri Benc 
13097bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local)
13107bedd0cfSJohannes Berg {
13117bedd0cfSJohannes Berg 	int err;
13127bedd0cfSJohannes Berg 
131383e7e4ceSHerbert Xu 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
13147bedd0cfSJohannes Berg 	if (err)
13157bedd0cfSJohannes Berg 		return err;
13167bedd0cfSJohannes Berg 
1317*ba6ddab9SJohannes Berg 	err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1318*ba6ddab9SJohannes Berg 	if (err) {
1319*ba6ddab9SJohannes Berg 		rhltable_destroy(&local->sta_hash);
1320*ba6ddab9SJohannes Berg 		return err;
1321*ba6ddab9SJohannes Berg 	}
1322*ba6ddab9SJohannes Berg 
13234d33960bSJohannes Berg 	spin_lock_init(&local->tim_lock);
132434e89507SJohannes Berg 	mutex_init(&local->sta_mtx);
1325f0706e82SJiri Benc 	INIT_LIST_HEAD(&local->sta_list);
1326f0706e82SJiri Benc 
132734f11cd3SKees Cook 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
13287bedd0cfSJohannes Berg 	return 0;
1329f0706e82SJiri Benc }
1330f0706e82SJiri Benc 
1331f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local)
1332f0706e82SJiri Benc {
1333a56f992cSJohannes Berg 	del_timer_sync(&local->sta_cleanup);
133483e7e4ceSHerbert Xu 	rhltable_destroy(&local->sta_hash);
1335*ba6ddab9SJohannes Berg 	rhltable_destroy(&local->link_sta_hash);
1336f0706e82SJiri Benc }
1337f0706e82SJiri Benc 
1338051007d9SJohannes Berg 
1339e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1340f0706e82SJiri Benc {
1341b998e8bbSJohannes Berg 	struct ieee80211_local *local = sdata->local;
1342f0706e82SJiri Benc 	struct sta_info *sta, *tmp;
1343d778207bSJohannes Berg 	LIST_HEAD(free_list);
134444213b5eSJohannes Berg 	int ret = 0;
1345f0706e82SJiri Benc 
1346d0709a65SJohannes Berg 	might_sleep();
1347d0709a65SJohannes Berg 
1348e716251dSJohannes Berg 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1349e716251dSJohannes Berg 	WARN_ON(vlans && !sdata->bss);
1350e716251dSJohannes Berg 
135134e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
135234e89507SJohannes Berg 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1353e716251dSJohannes Berg 		if (sdata == sta->sdata ||
1354e716251dSJohannes Berg 		    (vlans && sdata->bss == sta->sdata->bss)) {
1355d778207bSJohannes Berg 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1356d778207bSJohannes Berg 				list_add(&sta->free_list, &free_list);
135734316837SJohannes Berg 			ret++;
135834316837SJohannes Berg 		}
135934e89507SJohannes Berg 	}
1360d778207bSJohannes Berg 
1361d778207bSJohannes Berg 	if (!list_empty(&free_list)) {
1362d778207bSJohannes Berg 		synchronize_net();
1363d778207bSJohannes Berg 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1364d778207bSJohannes Berg 			__sta_info_destroy_part2(sta);
1365d778207bSJohannes Berg 	}
136634e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
136744213b5eSJohannes Berg 
1368051007d9SJohannes Berg 	return ret;
1369051007d9SJohannes Berg }
1370051007d9SJohannes Berg 
137124723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
137224723d1bSJohannes Berg 			  unsigned long exp_time)
137324723d1bSJohannes Berg {
137424723d1bSJohannes Berg 	struct ieee80211_local *local = sdata->local;
137524723d1bSJohannes Berg 	struct sta_info *sta, *tmp;
137624723d1bSJohannes Berg 
137734e89507SJohannes Berg 	mutex_lock(&local->sta_mtx);
1378e46a2cf9SMohammed Shafi Shajakhan 
1379e46a2cf9SMohammed Shafi Shajakhan 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1380b8da6b6aSJohannes Berg 		unsigned long last_active = ieee80211_sta_last_active(sta);
1381b8da6b6aSJohannes Berg 
1382ec2b774eSMarek Lindner 		if (sdata != sta->sdata)
1383ec2b774eSMarek Lindner 			continue;
1384ec2b774eSMarek Lindner 
1385b8da6b6aSJohannes Berg 		if (time_is_before_jiffies(last_active + exp_time)) {
1386eea57d42SMohammed Shafi Shajakhan 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1387bdcbd8e0SJohannes Berg 				sta->sta.addr);
13883f52b7e3SMarco Porsch 
13893f52b7e3SMarco Porsch 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
13903f52b7e3SMarco Porsch 			    test_sta_flag(sta, WLAN_STA_PS_STA))
13913f52b7e3SMarco Porsch 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
13923f52b7e3SMarco Porsch 
139334e89507SJohannes Berg 			WARN_ON(__sta_info_destroy(sta));
139424723d1bSJohannes Berg 		}
1395e46a2cf9SMohammed Shafi Shajakhan 	}
1396e46a2cf9SMohammed Shafi Shajakhan 
139734e89507SJohannes Berg 	mutex_unlock(&local->sta_mtx);
139824723d1bSJohannes Berg }
139917741cdcSJohannes Berg 
1400686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1401686b9cb9SBen Greear 						   const u8 *addr,
1402686b9cb9SBen Greear 						   const u8 *localaddr)
140317741cdcSJohannes Berg {
14047bedd0cfSJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
140583e7e4ceSHerbert Xu 	struct rhlist_head *tmp;
14067bedd0cfSJohannes Berg 	struct sta_info *sta;
140717741cdcSJohannes Berg 
1408686b9cb9SBen Greear 	/*
1409686b9cb9SBen Greear 	 * Just return a random station if localaddr is NULL
1410686b9cb9SBen Greear 	 * ... first in list.
1411686b9cb9SBen Greear 	 */
141283e7e4ceSHerbert Xu 	for_each_sta_info(local, addr, sta, tmp) {
1413686b9cb9SBen Greear 		if (localaddr &&
1414b203ca39SJoe Perches 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1415686b9cb9SBen Greear 			continue;
1416f7c65594SJohannes Berg 		if (!sta->uploaded)
1417f7c65594SJohannes Berg 			return NULL;
141817741cdcSJohannes Berg 		return &sta->sta;
1419f7c65594SJohannes Berg 	}
1420f7c65594SJohannes Berg 
1421abe60632SJohannes Berg 	return NULL;
142217741cdcSJohannes Berg }
1423686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
14245ed176e1SJohannes Berg 
14255ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
14265ed176e1SJohannes Berg 					 const u8 *addr)
14275ed176e1SJohannes Berg {
1428f7c65594SJohannes Berg 	struct sta_info *sta;
14295ed176e1SJohannes Berg 
14305ed176e1SJohannes Berg 	if (!vif)
14315ed176e1SJohannes Berg 		return NULL;
14325ed176e1SJohannes Berg 
1433f7c65594SJohannes Berg 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1434f7c65594SJohannes Berg 	if (!sta)
1435f7c65594SJohannes Berg 		return NULL;
14365ed176e1SJohannes Berg 
1437f7c65594SJohannes Berg 	if (!sta->uploaded)
1438f7c65594SJohannes Berg 		return NULL;
1439f7c65594SJohannes Berg 
1440f7c65594SJohannes Berg 	return &sta->sta;
14415ed176e1SJohannes Berg }
144217741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta);
1443af818581SJohannes Berg 
1444e3685e03SJohannes Berg /* powersave support code */
1445e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
144650a9432dSJohannes Berg {
1447608383bfSHelmut Schaa 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1448e3685e03SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1449e3685e03SJohannes Berg 	struct sk_buff_head pending;
1450ba8c3d6fSFelix Fietkau 	int filtered = 0, buffered = 0, ac, i;
1451e3685e03SJohannes Berg 	unsigned long flags;
1452d012a605SMarco Porsch 	struct ps_data *ps;
1453d012a605SMarco Porsch 
14543918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
14553918edb0SFelix Fietkau 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
14563918edb0SFelix Fietkau 				     u.ap);
14573918edb0SFelix Fietkau 
14583918edb0SFelix Fietkau 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1459d012a605SMarco Porsch 		ps = &sdata->bss->ps;
14603f52b7e3SMarco Porsch 	else if (ieee80211_vif_is_mesh(&sdata->vif))
14613f52b7e3SMarco Porsch 		ps = &sdata->u.mesh.ps;
1462d012a605SMarco Porsch 	else
1463d012a605SMarco Porsch 		return;
146450a9432dSJohannes Berg 
1465c2c98fdeSJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
146647086fc5SJohannes Berg 
14675a306f58SJohannes Berg 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1468948d887dSJohannes Berg 	sta->driver_buffered_tids = 0;
1469ba8c3d6fSFelix Fietkau 	sta->txq_buffered_tids = 0;
1470948d887dSJohannes Berg 
147130686bf7SJohannes Berg 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
147212375ef9SJohannes Berg 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1473af818581SJohannes Berg 
1474ba8c3d6fSFelix Fietkau 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1475adf8ed01SJohannes Berg 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1476ba8c3d6fSFelix Fietkau 			continue;
1477ba8c3d6fSFelix Fietkau 
147818667600SToke Høiland-Jørgensen 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1479ba8c3d6fSFelix Fietkau 	}
1480ba8c3d6fSFelix Fietkau 
1481948d887dSJohannes Berg 	skb_queue_head_init(&pending);
1482af818581SJohannes Berg 
14831d147bfaSEmmanuel Grumbach 	/* sync with ieee80211_tx_h_unicast_ps_buf */
14841d147bfaSEmmanuel Grumbach 	spin_lock(&sta->ps_lock);
1485af818581SJohannes Berg 	/* Send all buffered frames to the station */
1486948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1487948d887dSJohannes Berg 		int count = skb_queue_len(&pending), tmp;
1488948d887dSJohannes Berg 
1489987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1490948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1491987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1492948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1493948d887dSJohannes Berg 		filtered += tmp - count;
1494948d887dSJohannes Berg 		count = tmp;
1495948d887dSJohannes Berg 
1496987c285cSArik Nemtsov 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1497948d887dSJohannes Berg 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1498987c285cSArik Nemtsov 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1499948d887dSJohannes Berg 		tmp = skb_queue_len(&pending);
1500948d887dSJohannes Berg 		buffered += tmp - count;
1501948d887dSJohannes Berg 	}
1502948d887dSJohannes Berg 
1503e3685e03SJohannes Berg 	ieee80211_add_pending_skbs(local, &pending);
15045ac2e350SJohannes Berg 
15055ac2e350SJohannes Berg 	/* now we're no longer in the deliver code */
15065ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
15075ac2e350SJohannes Berg 
15085ac2e350SJohannes Berg 	/* The station might have polled and then woken up before we responded,
15095ac2e350SJohannes Berg 	 * so clear these flags now to avoid them sticking around.
15105ac2e350SJohannes Berg 	 */
15115ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
15125ac2e350SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_UAPSD);
15131d147bfaSEmmanuel Grumbach 	spin_unlock(&sta->ps_lock);
1514948d887dSJohannes Berg 
1515e3685e03SJohannes Berg 	atomic_dec(&ps->num_sta_ps);
1516e3685e03SJohannes Berg 
1517af818581SJohannes Berg 	local->total_ps_buffered -= buffered;
1518af818581SJohannes Berg 
1519c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
1520c868cb35SJohannes Berg 
1521bdcbd8e0SJohannes Berg 	ps_dbg(sdata,
15222595d259SSara Sharon 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1523bdcbd8e0SJohannes Berg 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
152417c18bf8SJohannes Berg 
152517c18bf8SJohannes Berg 	ieee80211_check_fast_xmit(sta);
1526af818581SJohannes Berg }
1527af818581SJohannes Berg 
15280ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1529b77cf4f8SJohannes Berg 					 enum ieee80211_frame_release_type reason,
15300ead2510SEmmanuel Grumbach 					 bool call_driver, bool more_data)
1531ce662b44SJohannes Berg {
15320ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1533ce662b44SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1534ce662b44SJohannes Berg 	struct ieee80211_qos_hdr *nullfunc;
1535ce662b44SJohannes Berg 	struct sk_buff *skb;
1536ce662b44SJohannes Berg 	int size = sizeof(*nullfunc);
1537ce662b44SJohannes Berg 	__le16 fc;
1538a74a8c84SJohannes Berg 	bool qos = sta->sta.wme;
1539ce662b44SJohannes Berg 	struct ieee80211_tx_info *info;
154055de908aSJohannes Berg 	struct ieee80211_chanctx_conf *chanctx_conf;
1541ce662b44SJohannes Berg 
1542ce662b44SJohannes Berg 	if (qos) {
1543ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1544ce662b44SJohannes Berg 				 IEEE80211_STYPE_QOS_NULLFUNC |
1545ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1546ce662b44SJohannes Berg 	} else {
1547ce662b44SJohannes Berg 		size -= 2;
1548ce662b44SJohannes Berg 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1549ce662b44SJohannes Berg 				 IEEE80211_STYPE_NULLFUNC |
1550ce662b44SJohannes Berg 				 IEEE80211_FCTL_FROMDS);
1551ce662b44SJohannes Berg 	}
1552ce662b44SJohannes Berg 
1553ce662b44SJohannes Berg 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1554ce662b44SJohannes Berg 	if (!skb)
1555ce662b44SJohannes Berg 		return;
1556ce662b44SJohannes Berg 
1557ce662b44SJohannes Berg 	skb_reserve(skb, local->hw.extra_tx_headroom);
1558ce662b44SJohannes Berg 
15594df864c1SJohannes Berg 	nullfunc = skb_put(skb, size);
1560ce662b44SJohannes Berg 	nullfunc->frame_control = fc;
1561ce662b44SJohannes Berg 	nullfunc->duration_id = 0;
1562ce662b44SJohannes Berg 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1563ce662b44SJohannes Berg 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1564ce662b44SJohannes Berg 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1565864a6040SJohannes Berg 	nullfunc->seq_ctrl = 0;
1566ce662b44SJohannes Berg 
1567ce662b44SJohannes Berg 	skb->priority = tid;
1568ce662b44SJohannes Berg 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
156959b66255SJohannes Berg 	if (qos) {
1570ce662b44SJohannes Berg 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1571ce662b44SJohannes Berg 
15720ead2510SEmmanuel Grumbach 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1573ce662b44SJohannes Berg 			nullfunc->qos_ctrl |=
1574ce662b44SJohannes Berg 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
15750ead2510SEmmanuel Grumbach 			if (more_data)
15760ead2510SEmmanuel Grumbach 				nullfunc->frame_control |=
15770ead2510SEmmanuel Grumbach 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
15780ead2510SEmmanuel Grumbach 		}
1579ce662b44SJohannes Berg 	}
1580ce662b44SJohannes Berg 
1581ce662b44SJohannes Berg 	info = IEEE80211_SKB_CB(skb);
1582ce662b44SJohannes Berg 
1583ce662b44SJohannes Berg 	/*
1584ce662b44SJohannes Berg 	 * Tell TX path to send this frame even though the
1585ce662b44SJohannes Berg 	 * STA may still remain is PS mode after this frame
1586deeaee19SJohannes Berg 	 * exchange. Also set EOSP to indicate this packet
1587deeaee19SJohannes Berg 	 * ends the poll/service period.
1588ce662b44SJohannes Berg 	 */
158902f2f1a9SJohannes Berg 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1590deeaee19SJohannes Berg 		       IEEE80211_TX_STATUS_EOSP |
1591ce662b44SJohannes Berg 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1592ce662b44SJohannes Berg 
15936b127c71SSujith Manoharan 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
15946b127c71SSujith Manoharan 
1595b77cf4f8SJohannes Berg 	if (call_driver)
1596b77cf4f8SJohannes Berg 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1597b77cf4f8SJohannes Berg 					  reason, false);
159840b96408SJohannes Berg 
159989afe614SJohannes Berg 	skb->dev = sdata->dev;
160089afe614SJohannes Berg 
160155de908aSJohannes Berg 	rcu_read_lock();
1602d0a9123eSJohannes Berg 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
160355de908aSJohannes Berg 	if (WARN_ON(!chanctx_conf)) {
160455de908aSJohannes Berg 		rcu_read_unlock();
160555de908aSJohannes Berg 		kfree_skb(skb);
160655de908aSJohannes Berg 		return;
160755de908aSJohannes Berg 	}
160855de908aSJohannes Berg 
160973c4e195SJohannes Berg 	info->band = chanctx_conf->def.chan->band;
161008aca29aSMathy Vanhoef 	ieee80211_xmit(sdata, sta, skb);
161155de908aSJohannes Berg 	rcu_read_unlock();
1612ce662b44SJohannes Berg }
1613ce662b44SJohannes Berg 
16140a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids)
16150a1cb809SJohannes Berg {
16160a1cb809SJohannes Berg 	/* lower 3 TIDs aren't ordered perfectly */
16170a1cb809SJohannes Berg 	if (tids & 0xF8)
16180a1cb809SJohannes Berg 		return fls(tids) - 1;
16190a1cb809SJohannes Berg 	/* TID 0 is BE just like TID 3 */
16200a1cb809SJohannes Berg 	if (tids & BIT(0))
16210a1cb809SJohannes Berg 		return 0;
16220a1cb809SJohannes Berg 	return fls(tids) - 1;
16230a1cb809SJohannes Berg }
16240a1cb809SJohannes Berg 
16250ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last
16260ead2510SEmmanuel Grumbach  * frame obtained by ieee80211_sta_ps_get_frames.
16270ead2510SEmmanuel Grumbach  * Note that driver_release_tids is relevant only if
16280ead2510SEmmanuel Grumbach  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
16290ead2510SEmmanuel Grumbach  */
16300ead2510SEmmanuel Grumbach static bool
16310ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
16320ead2510SEmmanuel Grumbach 			   enum ieee80211_frame_release_type reason,
16330ead2510SEmmanuel Grumbach 			   unsigned long driver_release_tids)
16340ead2510SEmmanuel Grumbach {
16350ead2510SEmmanuel Grumbach 	int ac;
16360ead2510SEmmanuel Grumbach 
16370ead2510SEmmanuel Grumbach 	/* If the driver has data on more than one TID then
16380ead2510SEmmanuel Grumbach 	 * certainly there's more data if we release just a
16390ead2510SEmmanuel Grumbach 	 * single frame now (from a single TID). This will
16400ead2510SEmmanuel Grumbach 	 * only happen for PS-Poll.
16410ead2510SEmmanuel Grumbach 	 */
16420ead2510SEmmanuel Grumbach 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
16430ead2510SEmmanuel Grumbach 	    hweight16(driver_release_tids) > 1)
16440ead2510SEmmanuel Grumbach 		return true;
16450ead2510SEmmanuel Grumbach 
16460ead2510SEmmanuel Grumbach 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1647f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
16480ead2510SEmmanuel Grumbach 			continue;
16490ead2510SEmmanuel Grumbach 
16500ead2510SEmmanuel Grumbach 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
16510ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
16520ead2510SEmmanuel Grumbach 			return true;
16530ead2510SEmmanuel Grumbach 	}
16540ead2510SEmmanuel Grumbach 
16550ead2510SEmmanuel Grumbach 	return false;
16560ead2510SEmmanuel Grumbach }
16570ead2510SEmmanuel Grumbach 
165847086fc5SJohannes Berg static void
16590ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
16600ead2510SEmmanuel Grumbach 			    enum ieee80211_frame_release_type reason,
16610ead2510SEmmanuel Grumbach 			    struct sk_buff_head *frames,
16620ead2510SEmmanuel Grumbach 			    unsigned long *driver_release_tids)
1663af818581SJohannes Berg {
1664af818581SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1665af818581SJohannes Berg 	struct ieee80211_local *local = sdata->local;
1666948d887dSJohannes Berg 	int ac;
1667af818581SJohannes Berg 
1668f9f760b4SJohannes Berg 	/* Get response frame(s) and more data bit for the last one. */
1669948d887dSJohannes Berg 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
16704049e09aSJohannes Berg 		unsigned long tids;
16714049e09aSJohannes Berg 
1672f438ceb8SEmmanuel Grumbach 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
1673948d887dSJohannes Berg 			continue;
1674948d887dSJohannes Berg 
16754049e09aSJohannes Berg 		tids = ieee80211_tids_for_ac(ac);
16764049e09aSJohannes Berg 
1677f9f760b4SJohannes Berg 		/* if we already have frames from software, then we can't also
1678f9f760b4SJohannes Berg 		 * release from hardware queues
1679f9f760b4SJohannes Berg 		 */
16800ead2510SEmmanuel Grumbach 		if (skb_queue_empty(frames)) {
16810ead2510SEmmanuel Grumbach 			*driver_release_tids |=
16820ead2510SEmmanuel Grumbach 				sta->driver_buffered_tids & tids;
16830ead2510SEmmanuel Grumbach 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1684ba8c3d6fSFelix Fietkau 		}
1685f9f760b4SJohannes Berg 
16860ead2510SEmmanuel Grumbach 		if (!*driver_release_tids) {
168747086fc5SJohannes Berg 			struct sk_buff *skb;
168847086fc5SJohannes Berg 
168947086fc5SJohannes Berg 			while (n_frames > 0) {
1690948d887dSJohannes Berg 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1691948d887dSJohannes Berg 				if (!skb) {
169247086fc5SJohannes Berg 					skb = skb_dequeue(
169347086fc5SJohannes Berg 						&sta->ps_tx_buf[ac]);
1694af818581SJohannes Berg 					if (skb)
1695af818581SJohannes Berg 						local->total_ps_buffered--;
1696af818581SJohannes Berg 				}
169747086fc5SJohannes Berg 				if (!skb)
169847086fc5SJohannes Berg 					break;
169947086fc5SJohannes Berg 				n_frames--;
17000ead2510SEmmanuel Grumbach 				__skb_queue_tail(frames, skb);
170147086fc5SJohannes Berg 			}
1702948d887dSJohannes Berg 		}
1703948d887dSJohannes Berg 
17040ead2510SEmmanuel Grumbach 		/* If we have more frames buffered on this AC, then abort the
17050ead2510SEmmanuel Grumbach 		 * loop since we can't send more data from other ACs before
17060ead2510SEmmanuel Grumbach 		 * the buffered frames from this.
17074049e09aSJohannes Berg 		 */
1708948d887dSJohannes Berg 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
17090ead2510SEmmanuel Grumbach 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1710948d887dSJohannes Berg 			break;
1711948d887dSJohannes Berg 	}
1712948d887dSJohannes Berg }
1713af818581SJohannes Berg 
17140ead2510SEmmanuel Grumbach static void
17150ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta,
17160ead2510SEmmanuel Grumbach 				  int n_frames, u8 ignored_acs,
17170ead2510SEmmanuel Grumbach 				  enum ieee80211_frame_release_type reason)
17180ead2510SEmmanuel Grumbach {
17190ead2510SEmmanuel Grumbach 	struct ieee80211_sub_if_data *sdata = sta->sdata;
17200ead2510SEmmanuel Grumbach 	struct ieee80211_local *local = sdata->local;
17210ead2510SEmmanuel Grumbach 	unsigned long driver_release_tids = 0;
17220ead2510SEmmanuel Grumbach 	struct sk_buff_head frames;
17230ead2510SEmmanuel Grumbach 	bool more_data;
17240ead2510SEmmanuel Grumbach 
17250ead2510SEmmanuel Grumbach 	/* Service or PS-Poll period starts */
17260ead2510SEmmanuel Grumbach 	set_sta_flag(sta, WLAN_STA_SP);
17270ead2510SEmmanuel Grumbach 
17280ead2510SEmmanuel Grumbach 	__skb_queue_head_init(&frames);
17290ead2510SEmmanuel Grumbach 
17300ead2510SEmmanuel Grumbach 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
17310ead2510SEmmanuel Grumbach 				    &frames, &driver_release_tids);
17320ead2510SEmmanuel Grumbach 
17330ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
17340ead2510SEmmanuel Grumbach 
17351a57081aSEmmanuel Grumbach 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
17360ead2510SEmmanuel Grumbach 		driver_release_tids =
17370ead2510SEmmanuel Grumbach 			BIT(find_highest_prio_tid(driver_release_tids));
17380ead2510SEmmanuel Grumbach 
1739f9f760b4SJohannes Berg 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1740f438ceb8SEmmanuel Grumbach 		int tid, ac;
17414049e09aSJohannes Berg 
1742ce662b44SJohannes Berg 		/*
1743ce662b44SJohannes Berg 		 * For PS-Poll, this can only happen due to a race condition
1744ce662b44SJohannes Berg 		 * when we set the TIM bit and the station notices it, but
1745ce662b44SJohannes Berg 		 * before it can poll for the frame we expire it.
1746ce662b44SJohannes Berg 		 *
1747ce662b44SJohannes Berg 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1748ce662b44SJohannes Berg 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1749ce662b44SJohannes Berg 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1750ce662b44SJohannes Berg 		 *	more than the value specified in the Max SP Length field
1751ce662b44SJohannes Berg 		 *	in the QoS Capability element from delivery-enabled ACs,
1752ce662b44SJohannes Berg 		 *	that are destined for the non-AP STA.
1753ce662b44SJohannes Berg 		 *
1754ce662b44SJohannes Berg 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1755ce662b44SJohannes Berg 		 */
1756ce662b44SJohannes Berg 
1757ce662b44SJohannes Berg 		/* This will evaluate to 1, 3, 5 or 7. */
1758f438ceb8SEmmanuel Grumbach 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
1759d7f84244SEmmanuel Grumbach 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
1760d7f84244SEmmanuel Grumbach 				break;
1761f438ceb8SEmmanuel Grumbach 		tid = 7 - 2 * ac;
1762ce662b44SJohannes Berg 
17630ead2510SEmmanuel Grumbach 		ieee80211_send_null_response(sta, tid, reason, true, false);
1764f9f760b4SJohannes Berg 	} else if (!driver_release_tids) {
176547086fc5SJohannes Berg 		struct sk_buff_head pending;
176647086fc5SJohannes Berg 		struct sk_buff *skb;
176740b96408SJohannes Berg 		int num = 0;
176840b96408SJohannes Berg 		u16 tids = 0;
1769b77cf4f8SJohannes Berg 		bool need_null = false;
177047086fc5SJohannes Berg 
177147086fc5SJohannes Berg 		skb_queue_head_init(&pending);
177247086fc5SJohannes Berg 
177347086fc5SJohannes Berg 		while ((skb = __skb_dequeue(&frames))) {
1774af818581SJohannes Berg 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
177547086fc5SJohannes Berg 			struct ieee80211_hdr *hdr = (void *) skb->data;
177640b96408SJohannes Berg 			u8 *qoshdr = NULL;
177740b96408SJohannes Berg 
177840b96408SJohannes Berg 			num++;
1779af818581SJohannes Berg 
1780af818581SJohannes Berg 			/*
178147086fc5SJohannes Berg 			 * Tell TX path to send this frame even though the
178247086fc5SJohannes Berg 			 * STA may still remain is PS mode after this frame
178347086fc5SJohannes Berg 			 * exchange.
1784af818581SJohannes Berg 			 */
17856b127c71SSujith Manoharan 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
17866b127c71SSujith Manoharan 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1787af818581SJohannes Berg 
178847086fc5SJohannes Berg 			/*
178947086fc5SJohannes Berg 			 * Use MoreData flag to indicate whether there are
179047086fc5SJohannes Berg 			 * more buffered frames for this STA
179147086fc5SJohannes Berg 			 */
179224b9c373SJanusz.Dziedzic@tieto.com 			if (more_data || !skb_queue_empty(&frames))
179347086fc5SJohannes Berg 				hdr->frame_control |=
179447086fc5SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
179524b9c373SJanusz.Dziedzic@tieto.com 			else
179624b9c373SJanusz.Dziedzic@tieto.com 				hdr->frame_control &=
179724b9c373SJanusz.Dziedzic@tieto.com 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1798af818581SJohannes Berg 
179940b96408SJohannes Berg 			if (ieee80211_is_data_qos(hdr->frame_control) ||
180040b96408SJohannes Berg 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
180140b96408SJohannes Berg 				qoshdr = ieee80211_get_qos_ctl(hdr);
180240b96408SJohannes Berg 
1803b77cf4f8SJohannes Berg 			tids |= BIT(skb->priority);
1804b77cf4f8SJohannes Berg 
1805b77cf4f8SJohannes Berg 			__skb_queue_tail(&pending, skb);
1806b77cf4f8SJohannes Berg 
1807b77cf4f8SJohannes Berg 			/* end service period after last frame or add one */
1808b77cf4f8SJohannes Berg 			if (!skb_queue_empty(&frames))
1809b77cf4f8SJohannes Berg 				continue;
1810b77cf4f8SJohannes Berg 
1811b77cf4f8SJohannes Berg 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1812b77cf4f8SJohannes Berg 				/* for PS-Poll, there's only one frame */
1813b77cf4f8SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1814b77cf4f8SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1815b77cf4f8SJohannes Berg 				break;
1816b77cf4f8SJohannes Berg 			}
1817b77cf4f8SJohannes Berg 
1818b77cf4f8SJohannes Berg 			/* For uAPSD, things are a bit more complicated. If the
1819b77cf4f8SJohannes Berg 			 * last frame has a QoS header (i.e. is a QoS-data or
1820b77cf4f8SJohannes Berg 			 * QoS-nulldata frame) then just set the EOSP bit there
1821b77cf4f8SJohannes Berg 			 * and be done.
1822b77cf4f8SJohannes Berg 			 * If the frame doesn't have a QoS header (which means
1823b77cf4f8SJohannes Berg 			 * it should be a bufferable MMPDU) then we can't set
1824b77cf4f8SJohannes Berg 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1825b77cf4f8SJohannes Berg 			 * frame to the list to send it after the MMPDU.
1826b77cf4f8SJohannes Berg 			 *
1827b77cf4f8SJohannes Berg 			 * Note that this code is only in the mac80211-release
1828b77cf4f8SJohannes Berg 			 * code path, we assume that the driver will not buffer
1829b77cf4f8SJohannes Berg 			 * anything but QoS-data frames, or if it does, will
1830b77cf4f8SJohannes Berg 			 * create the QoS-nulldata frame by itself if needed.
1831b77cf4f8SJohannes Berg 			 *
1832b77cf4f8SJohannes Berg 			 * Cf. 802.11-2012 10.2.1.10 (c).
1833b77cf4f8SJohannes Berg 			 */
1834b77cf4f8SJohannes Berg 			if (qoshdr) {
183540b96408SJohannes Berg 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1836deeaee19SJohannes Berg 
183747086fc5SJohannes Berg 				info->flags |= IEEE80211_TX_STATUS_EOSP |
183847086fc5SJohannes Berg 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1839b77cf4f8SJohannes Berg 			} else {
1840b77cf4f8SJohannes Berg 				/* The standard isn't completely clear on this
1841b77cf4f8SJohannes Berg 				 * as it says the more-data bit should be set
1842b77cf4f8SJohannes Berg 				 * if there are more BUs. The QoS-Null frame
1843b77cf4f8SJohannes Berg 				 * we're about to send isn't buffered yet, we
1844b77cf4f8SJohannes Berg 				 * only create it below, but let's pretend it
1845b77cf4f8SJohannes Berg 				 * was buffered just in case some clients only
1846b77cf4f8SJohannes Berg 				 * expect more-data=0 when eosp=1.
1847b77cf4f8SJohannes Berg 				 */
1848b77cf4f8SJohannes Berg 				hdr->frame_control |=
1849b77cf4f8SJohannes Berg 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1850b77cf4f8SJohannes Berg 				need_null = true;
1851b77cf4f8SJohannes Berg 				num++;
185252a3f20cSMarco Porsch 			}
1853b77cf4f8SJohannes Berg 			break;
185447086fc5SJohannes Berg 		}
185547086fc5SJohannes Berg 
185640b96408SJohannes Berg 		drv_allow_buffered_frames(local, sta, tids, num,
185740b96408SJohannes Berg 					  reason, more_data);
185840b96408SJohannes Berg 
185947086fc5SJohannes Berg 		ieee80211_add_pending_skbs(local, &pending);
1860af818581SJohannes Berg 
1861b77cf4f8SJohannes Berg 		if (need_null)
1862b77cf4f8SJohannes Berg 			ieee80211_send_null_response(
18630ead2510SEmmanuel Grumbach 				sta, find_highest_prio_tid(tids),
18640ead2510SEmmanuel Grumbach 				reason, false, false);
1865b77cf4f8SJohannes Berg 
1866c868cb35SJohannes Berg 		sta_info_recalc_tim(sta);
1867af818581SJohannes Berg 	} else {
1868ba8c3d6fSFelix Fietkau 		int tid;
1869ba8c3d6fSFelix Fietkau 
1870af818581SJohannes Berg 		/*
18714049e09aSJohannes Berg 		 * We need to release a frame that is buffered somewhere in the
18724049e09aSJohannes Berg 		 * driver ... it'll have to handle that.
1873f9f760b4SJohannes Berg 		 * Note that the driver also has to check the number of frames
1874f9f760b4SJohannes Berg 		 * on the TIDs we're releasing from - if there are more than
1875f9f760b4SJohannes Berg 		 * n_frames it has to set the more-data bit (if we didn't ask
1876f9f760b4SJohannes Berg 		 * it to set it anyway due to other buffered frames); if there
1877f9f760b4SJohannes Berg 		 * are fewer than n_frames it has to make sure to adjust that
1878f9f760b4SJohannes Berg 		 * to allow the service period to end properly.
1879af818581SJohannes Berg 		 */
18804049e09aSJohannes Berg 		drv_release_buffered_frames(local, sta, driver_release_tids,
188147086fc5SJohannes Berg 					    n_frames, reason, more_data);
18824049e09aSJohannes Berg 
18834049e09aSJohannes Berg 		/*
18844049e09aSJohannes Berg 		 * Note that we don't recalculate the TIM bit here as it would
18854049e09aSJohannes Berg 		 * most likely have no effect at all unless the driver told us
1886f9f760b4SJohannes Berg 		 * that the TID(s) became empty before returning here from the
18874049e09aSJohannes Berg 		 * release function.
1888f9f760b4SJohannes Berg 		 * Either way, however, when the driver tells us that the TID(s)
1889ba8c3d6fSFelix Fietkau 		 * became empty or we find that a txq became empty, we'll do the
1890ba8c3d6fSFelix Fietkau 		 * TIM recalculation.
18914049e09aSJohannes Berg 		 */
1892ba8c3d6fSFelix Fietkau 
1893ba8c3d6fSFelix Fietkau 		if (!sta->sta.txq[0])
1894ba8c3d6fSFelix Fietkau 			return;
1895ba8c3d6fSFelix Fietkau 
1896ba8c3d6fSFelix Fietkau 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1897adf8ed01SJohannes Berg 			if (!sta->sta.txq[tid] ||
1898adf8ed01SJohannes Berg 			    !(driver_release_tids & BIT(tid)) ||
18991e1430d5SJohannes Berg 			    txq_has_queue(sta->sta.txq[tid]))
1900ba8c3d6fSFelix Fietkau 				continue;
1901ba8c3d6fSFelix Fietkau 
1902ba8c3d6fSFelix Fietkau 			sta_info_recalc_tim(sta);
1903ba8c3d6fSFelix Fietkau 			break;
1904ba8c3d6fSFelix Fietkau 		}
1905af818581SJohannes Berg 	}
1906af818581SJohannes Berg }
1907af818581SJohannes Berg 
1908af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1909af818581SJohannes Berg {
191047086fc5SJohannes Berg 	u8 ignore_for_response = sta->sta.uapsd_queues;
1911af818581SJohannes Berg 
1912af818581SJohannes Berg 	/*
191347086fc5SJohannes Berg 	 * If all ACs are delivery-enabled then we should reply
191447086fc5SJohannes Berg 	 * from any of them, if only some are enabled we reply
191547086fc5SJohannes Berg 	 * only from the non-enabled ones.
1916af818581SJohannes Berg 	 */
191747086fc5SJohannes Berg 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
191847086fc5SJohannes Berg 		ignore_for_response = 0;
1919af818581SJohannes Berg 
192047086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
192147086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1922af818581SJohannes Berg }
192347086fc5SJohannes Berg 
192447086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
192547086fc5SJohannes Berg {
192647086fc5SJohannes Berg 	int n_frames = sta->sta.max_sp;
192747086fc5SJohannes Berg 	u8 delivery_enabled = sta->sta.uapsd_queues;
192847086fc5SJohannes Berg 
192947086fc5SJohannes Berg 	/*
193047086fc5SJohannes Berg 	 * If we ever grow support for TSPEC this might happen if
193147086fc5SJohannes Berg 	 * the TSPEC update from hostapd comes in between a trigger
193247086fc5SJohannes Berg 	 * frame setting WLAN_STA_UAPSD in the RX path and this
193347086fc5SJohannes Berg 	 * actually getting called.
193447086fc5SJohannes Berg 	 */
193547086fc5SJohannes Berg 	if (!delivery_enabled)
193647086fc5SJohannes Berg 		return;
193747086fc5SJohannes Berg 
193847086fc5SJohannes Berg 	switch (sta->sta.max_sp) {
193947086fc5SJohannes Berg 	case 1:
194047086fc5SJohannes Berg 		n_frames = 2;
194147086fc5SJohannes Berg 		break;
194247086fc5SJohannes Berg 	case 2:
194347086fc5SJohannes Berg 		n_frames = 4;
194447086fc5SJohannes Berg 		break;
194547086fc5SJohannes Berg 	case 3:
194647086fc5SJohannes Berg 		n_frames = 6;
194747086fc5SJohannes Berg 		break;
194847086fc5SJohannes Berg 	case 0:
194947086fc5SJohannes Berg 		/* XXX: what is a good value? */
195013a8098aSAndrei Otcheretianski 		n_frames = 128;
195147086fc5SJohannes Berg 		break;
195247086fc5SJohannes Berg 	}
195347086fc5SJohannes Berg 
195447086fc5SJohannes Berg 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
195547086fc5SJohannes Berg 					  IEEE80211_FRAME_RELEASE_UAPSD);
1956af818581SJohannes Berg }
1957af818581SJohannes Berg 
1958af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1959af818581SJohannes Berg 			       struct ieee80211_sta *pubsta, bool block)
1960af818581SJohannes Berg {
1961af818581SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1962af818581SJohannes Berg 
1963b5878a2dSJohannes Berg 	trace_api_sta_block_awake(sta->local, pubsta, block);
1964b5878a2dSJohannes Berg 
19655ac2e350SJohannes Berg 	if (block) {
1966c2c98fdeSJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
196717c18bf8SJohannes Berg 		ieee80211_clear_fast_xmit(sta);
19685ac2e350SJohannes Berg 		return;
19695ac2e350SJohannes Berg 	}
19705ac2e350SJohannes Berg 
19715ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
19725ac2e350SJohannes Berg 		return;
19735ac2e350SJohannes Berg 
19745ac2e350SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
19755ac2e350SJohannes Berg 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
19765ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
19775ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
19785ac2e350SJohannes Berg 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
19795ac2e350SJohannes Berg 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
19805ac2e350SJohannes Berg 		/* must be asleep in this case */
19815ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
19825ac2e350SJohannes Berg 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
19835ac2e350SJohannes Berg 	} else {
19845ac2e350SJohannes Berg 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
198517c18bf8SJohannes Berg 		ieee80211_check_fast_xmit(sta);
19865ac2e350SJohannes Berg 	}
1987af818581SJohannes Berg }
1988af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake);
1989dcf55fb5SFelix Fietkau 
1990e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
199137fbd908SJohannes Berg {
199237fbd908SJohannes Berg 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
199337fbd908SJohannes Berg 	struct ieee80211_local *local = sta->local;
199437fbd908SJohannes Berg 
199537fbd908SJohannes Berg 	trace_api_eosp(local, pubsta);
199637fbd908SJohannes Berg 
199737fbd908SJohannes Berg 	clear_sta_flag(sta, WLAN_STA_SP);
199837fbd908SJohannes Berg }
1999e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp);
200037fbd908SJohannes Berg 
20010ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
20020ead2510SEmmanuel Grumbach {
20030ead2510SEmmanuel Grumbach 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
20040ead2510SEmmanuel Grumbach 	enum ieee80211_frame_release_type reason;
20050ead2510SEmmanuel Grumbach 	bool more_data;
20060ead2510SEmmanuel Grumbach 
20070ead2510SEmmanuel Grumbach 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
20080ead2510SEmmanuel Grumbach 
20090ead2510SEmmanuel Grumbach 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
20100ead2510SEmmanuel Grumbach 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
20110ead2510SEmmanuel Grumbach 					       reason, 0);
20120ead2510SEmmanuel Grumbach 
20130ead2510SEmmanuel Grumbach 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
20140ead2510SEmmanuel Grumbach }
20150ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
20160ead2510SEmmanuel Grumbach 
2017042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2018042ec453SJohannes Berg 				u8 tid, bool buffered)
2019dcf55fb5SFelix Fietkau {
2020dcf55fb5SFelix Fietkau 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2021dcf55fb5SFelix Fietkau 
20225a306f58SJohannes Berg 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2023042ec453SJohannes Berg 		return;
2024042ec453SJohannes Berg 
20251b000789SJohannes Berg 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
20261b000789SJohannes Berg 
2027948d887dSJohannes Berg 	if (buffered)
2028948d887dSJohannes Berg 		set_bit(tid, &sta->driver_buffered_tids);
2029948d887dSJohannes Berg 	else
2030948d887dSJohannes Berg 		clear_bit(tid, &sta->driver_buffered_tids);
2031948d887dSJohannes Berg 
2032c868cb35SJohannes Berg 	sta_info_recalc_tim(sta);
2033dcf55fb5SFelix Fietkau }
2034042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2035d9a7ddb0SJohannes Berg 
20362433647bSToke Høiland-Jørgensen void ieee80211_register_airtime(struct ieee80211_txq *txq,
20372433647bSToke Høiland-Jørgensen 				u32 tx_airtime, u32 rx_airtime)
20382433647bSToke Høiland-Jørgensen {
20392433647bSToke Høiland-Jørgensen 	struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif);
20402433647bSToke Høiland-Jørgensen 	struct ieee80211_local *local = sdata->local;
20412433647bSToke Høiland-Jørgensen 	u64 weight_sum, weight_sum_reciprocal;
20422433647bSToke Høiland-Jørgensen 	struct airtime_sched_info *air_sched;
20432433647bSToke Høiland-Jørgensen 	struct airtime_info *air_info;
20442433647bSToke Høiland-Jørgensen 	u32 airtime = 0;
20452433647bSToke Høiland-Jørgensen 
20462433647bSToke Høiland-Jørgensen 	air_sched = &local->airtime[txq->ac];
20472433647bSToke Høiland-Jørgensen 	air_info = to_airtime_info(txq);
20482433647bSToke Høiland-Jørgensen 
20492433647bSToke Høiland-Jørgensen 	if (local->airtime_flags & AIRTIME_USE_TX)
20502433647bSToke Høiland-Jørgensen 		airtime += tx_airtime;
20512433647bSToke Høiland-Jørgensen 	if (local->airtime_flags & AIRTIME_USE_RX)
20522433647bSToke Høiland-Jørgensen 		airtime += rx_airtime;
20532433647bSToke Høiland-Jørgensen 
20542433647bSToke Høiland-Jørgensen 	/* Weights scale so the unit weight is 256 */
20552433647bSToke Høiland-Jørgensen 	airtime <<= 8;
20562433647bSToke Høiland-Jørgensen 
20572433647bSToke Høiland-Jørgensen 	spin_lock_bh(&air_sched->lock);
20582433647bSToke Høiland-Jørgensen 
20592433647bSToke Høiland-Jørgensen 	air_info->tx_airtime += tx_airtime;
20602433647bSToke Høiland-Jørgensen 	air_info->rx_airtime += rx_airtime;
20612433647bSToke Høiland-Jørgensen 
20622433647bSToke Høiland-Jørgensen 	if (air_sched->weight_sum) {
20632433647bSToke Høiland-Jørgensen 		weight_sum = air_sched->weight_sum;
20642433647bSToke Høiland-Jørgensen 		weight_sum_reciprocal = air_sched->weight_sum_reciprocal;
20652433647bSToke Høiland-Jørgensen 	} else {
20662433647bSToke Høiland-Jørgensen 		weight_sum = air_info->weight;
20672433647bSToke Høiland-Jørgensen 		weight_sum_reciprocal = air_info->weight_reciprocal;
20682433647bSToke Høiland-Jørgensen 	}
20692433647bSToke Høiland-Jørgensen 
20702433647bSToke Høiland-Jørgensen 	/* Round the calculation of global vt */
20712433647bSToke Høiland-Jørgensen 	air_sched->v_t += (u64)((airtime + (weight_sum >> 1)) *
20722433647bSToke Høiland-Jørgensen 				weight_sum_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_64;
20732433647bSToke Høiland-Jørgensen 	air_info->v_t += (u32)((airtime + (air_info->weight >> 1)) *
20742433647bSToke Høiland-Jørgensen 			       air_info->weight_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_32;
20752433647bSToke Høiland-Jørgensen 	ieee80211_resort_txq(&local->hw, txq);
20762433647bSToke Høiland-Jørgensen 
20772433647bSToke Høiland-Jørgensen 	spin_unlock_bh(&air_sched->lock);
20782433647bSToke Høiland-Jørgensen }
20792433647bSToke Høiland-Jørgensen 
2080b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2081b4809e94SToke Høiland-Jørgensen 				    u32 tx_airtime, u32 rx_airtime)
2082b4809e94SToke Høiland-Jørgensen {
20832433647bSToke Høiland-Jørgensen 	struct ieee80211_txq *txq = pubsta->txq[tid];
2084b4809e94SToke Høiland-Jørgensen 
20852433647bSToke Høiland-Jørgensen 	if (!txq)
20862433647bSToke Høiland-Jørgensen 		return;
2087b4809e94SToke Høiland-Jørgensen 
20882433647bSToke Høiland-Jørgensen 	ieee80211_register_airtime(txq, tx_airtime, rx_airtime);
2089b4809e94SToke Høiland-Jørgensen }
2090b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2091b4809e94SToke Høiland-Jørgensen 
20923ace10f5SKan Yan void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
20933ace10f5SKan Yan 					  struct sta_info *sta, u8 ac,
20943ace10f5SKan Yan 					  u16 tx_airtime, bool tx_completed)
20953ace10f5SKan Yan {
20963ace10f5SKan Yan 	int tx_pending;
20973ace10f5SKan Yan 
2098911bde0fSToke Høiland-Jørgensen 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2099911bde0fSToke Høiland-Jørgensen 		return;
2100911bde0fSToke Høiland-Jørgensen 
21013ace10f5SKan Yan 	if (!tx_completed) {
21023ace10f5SKan Yan 		if (sta)
21033ace10f5SKan Yan 			atomic_add(tx_airtime,
21043ace10f5SKan Yan 				   &sta->airtime[ac].aql_tx_pending);
21053ace10f5SKan Yan 
21063ace10f5SKan Yan 		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
21073ace10f5SKan Yan 		return;
21083ace10f5SKan Yan 	}
21093ace10f5SKan Yan 
21103ace10f5SKan Yan 	if (sta) {
21113ace10f5SKan Yan 		tx_pending = atomic_sub_return(tx_airtime,
21123ace10f5SKan Yan 					       &sta->airtime[ac].aql_tx_pending);
211304e35caaSFelix Fietkau 		if (tx_pending < 0)
21143ace10f5SKan Yan 			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
21153ace10f5SKan Yan 				       tx_pending, 0);
21163ace10f5SKan Yan 	}
21173ace10f5SKan Yan 
21183ace10f5SKan Yan 	tx_pending = atomic_sub_return(tx_airtime,
21193ace10f5SKan Yan 				       &local->aql_total_pending_airtime);
21203ace10f5SKan Yan 	if (WARN_ONCE(tx_pending < 0,
21213ace10f5SKan Yan 		      "Device %s AC %d pending airtime underflow: %u, %u",
21223ace10f5SKan Yan 		      wiphy_name(local->hw.wiphy), ac, tx_pending,
21233ace10f5SKan Yan 		      tx_airtime))
21243ace10f5SKan Yan 		atomic_cmpxchg(&local->aql_total_pending_airtime,
21253ace10f5SKan Yan 			       tx_pending, 0);
21263ace10f5SKan Yan }
21273ace10f5SKan Yan 
212883d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta,
2129d9a7ddb0SJohannes Berg 			enum ieee80211_sta_state new_state)
2130d9a7ddb0SJohannes Berg {
21318bf11d8dSJohannes Berg 	might_sleep();
2132d9a7ddb0SJohannes Berg 
2133d9a7ddb0SJohannes Berg 	if (sta->sta_state == new_state)
2134d9a7ddb0SJohannes Berg 		return 0;
2135d9a7ddb0SJohannes Berg 
2136f09603a2SJohannes Berg 	/* check allowed transitions first */
2137f09603a2SJohannes Berg 
2138d9a7ddb0SJohannes Berg 	switch (new_state) {
2139d9a7ddb0SJohannes Berg 	case IEEE80211_STA_NONE:
2140f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH)
2141d9a7ddb0SJohannes Berg 			return -EINVAL;
2142d9a7ddb0SJohannes Berg 		break;
2143d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTH:
2144f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_NONE &&
2145f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_ASSOC)
2146d9a7ddb0SJohannes Berg 			return -EINVAL;
2147d9a7ddb0SJohannes Berg 		break;
2148d9a7ddb0SJohannes Berg 	case IEEE80211_STA_ASSOC:
2149f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_AUTH &&
2150f09603a2SJohannes Berg 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
2151d9a7ddb0SJohannes Berg 			return -EINVAL;
2152d9a7ddb0SJohannes Berg 		break;
2153d9a7ddb0SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2154f09603a2SJohannes Berg 		if (sta->sta_state != IEEE80211_STA_ASSOC)
2155d9a7ddb0SJohannes Berg 			return -EINVAL;
2156d9a7ddb0SJohannes Berg 		break;
2157d9a7ddb0SJohannes Berg 	default:
2158d9a7ddb0SJohannes Berg 		WARN(1, "invalid state %d", new_state);
2159d9a7ddb0SJohannes Berg 		return -EINVAL;
2160d9a7ddb0SJohannes Berg 	}
2161d9a7ddb0SJohannes Berg 
2162bdcbd8e0SJohannes Berg 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
2163bdcbd8e0SJohannes Berg 		sta->sta.addr, new_state);
2164f09603a2SJohannes Berg 
2165f09603a2SJohannes Berg 	/*
2166f09603a2SJohannes Berg 	 * notify the driver before the actual changes so it can
2167f09603a2SJohannes Berg 	 * fail the transition
2168f09603a2SJohannes Berg 	 */
2169f09603a2SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
2170f09603a2SJohannes Berg 		int err = drv_sta_state(sta->local, sta->sdata, sta,
2171f09603a2SJohannes Berg 					sta->sta_state, new_state);
2172f09603a2SJohannes Berg 		if (err)
2173f09603a2SJohannes Berg 			return err;
2174f09603a2SJohannes Berg 	}
2175f09603a2SJohannes Berg 
2176f09603a2SJohannes Berg 	/* reflect the change in all state variables */
2177f09603a2SJohannes Berg 
2178f09603a2SJohannes Berg 	switch (new_state) {
2179f09603a2SJohannes Berg 	case IEEE80211_STA_NONE:
2180f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH)
2181f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
2182f09603a2SJohannes Berg 		break;
2183f09603a2SJohannes Berg 	case IEEE80211_STA_AUTH:
2184a7201a6cSIlan Peer 		if (sta->sta_state == IEEE80211_STA_NONE) {
2185f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTH, &sta->_flags);
2186a7201a6cSIlan Peer 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
2187f09603a2SJohannes Berg 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
2188a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
218952cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
219052cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2191a7201a6cSIlan Peer 		}
2192f09603a2SJohannes Berg 		break;
2193f09603a2SJohannes Berg 	case IEEE80211_STA_ASSOC:
2194f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_AUTH) {
2195f09603a2SJohannes Berg 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
21969cf02338SBen Greear 			sta->assoc_at = ktime_get_boottime_ns();
2197a7201a6cSIlan Peer 			ieee80211_recalc_min_chandef(sta->sdata);
219852cfa1d6SAyala Beker 			if (!sta->sta.support_p2p_ps)
219952cfa1d6SAyala Beker 				ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
2200f09603a2SJohannes Berg 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
220172f15d53SMichael Braun 			ieee80211_vif_dec_num_mcast(sta->sdata);
2202f09603a2SJohannes Berg 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
220317c18bf8SJohannes Berg 			ieee80211_clear_fast_xmit(sta);
220449ddf8e6SJohannes Berg 			ieee80211_clear_fast_rx(sta);
2205f09603a2SJohannes Berg 		}
2206f09603a2SJohannes Berg 		break;
2207f09603a2SJohannes Berg 	case IEEE80211_STA_AUTHORIZED:
2208f09603a2SJohannes Berg 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
220972f15d53SMichael Braun 			ieee80211_vif_inc_num_mcast(sta->sdata);
2210f09603a2SJohannes Berg 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
221117c18bf8SJohannes Berg 			ieee80211_check_fast_xmit(sta);
221249ddf8e6SJohannes Berg 			ieee80211_check_fast_rx(sta);
2213f09603a2SJohannes Berg 		}
22143e493173SJouni Malinen 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
22153e493173SJouni Malinen 		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
22163e493173SJouni Malinen 			cfg80211_send_layer2_update(sta->sdata->dev,
22173e493173SJouni Malinen 						    sta->sta.addr);
2218f09603a2SJohannes Berg 		break;
2219f09603a2SJohannes Berg 	default:
2220f09603a2SJohannes Berg 		break;
2221f09603a2SJohannes Berg 	}
2222f09603a2SJohannes Berg 
2223d9a7ddb0SJohannes Berg 	sta->sta_state = new_state;
2224d9a7ddb0SJohannes Berg 
2225d9a7ddb0SJohannes Berg 	return 0;
2226d9a7ddb0SJohannes Berg }
2227687da132SEmmanuel Grumbach 
2228c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats *
2229c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta)
2230c9c5962bSJohannes Berg {
2231046d2e7cSSriram R 	struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats;
2232c9c5962bSJohannes Berg 	int cpu;
2233c9c5962bSJohannes Berg 
2234046d2e7cSSriram R 	if (!sta->deflink.pcpu_rx_stats)
2235c9c5962bSJohannes Berg 		return stats;
2236c9c5962bSJohannes Berg 
2237c9c5962bSJohannes Berg 	for_each_possible_cpu(cpu) {
2238c9c5962bSJohannes Berg 		struct ieee80211_sta_rx_stats *cpustats;
2239c9c5962bSJohannes Berg 
2240046d2e7cSSriram R 		cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2241c9c5962bSJohannes Berg 
2242c9c5962bSJohannes Berg 		if (time_after(cpustats->last_rx, stats->last_rx))
2243c9c5962bSJohannes Berg 			stats = cpustats;
2244c9c5962bSJohannes Berg 	}
2245c9c5962bSJohannes Berg 
2246c9c5962bSJohannes Berg 	return stats;
2247c9c5962bSJohannes Berg }
2248c9c5962bSJohannes Berg 
224941cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
22504f6b1b3dSJohannes Berg 				  struct rate_info *rinfo)
2251fbd6ff5cSJohannes Berg {
2252dcba665bSJohannes Berg 	rinfo->bw = STA_STATS_GET(BW, rate);
2253fbd6ff5cSJohannes Berg 
2254dcba665bSJohannes Berg 	switch (STA_STATS_GET(TYPE, rate)) {
22557f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_VHT:
22564f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2257dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2258dcba665bSJohannes Berg 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2259dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2260dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
22617f406cd1SJohannes Berg 		break;
22627f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_HT:
22634f6b1b3dSJohannes Berg 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2264dcba665bSJohannes Berg 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2265dcba665bSJohannes Berg 		if (STA_STATS_GET(SGI, rate))
2266dcba665bSJohannes Berg 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
22677f406cd1SJohannes Berg 		break;
22687f406cd1SJohannes Berg 	case STA_STATS_RATE_TYPE_LEGACY: {
2269fbd6ff5cSJohannes Berg 		struct ieee80211_supported_band *sband;
2270fbd6ff5cSJohannes Berg 		u16 brate;
22714f6b1b3dSJohannes Berg 		unsigned int shift;
2272dcba665bSJohannes Berg 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2273dcba665bSJohannes Berg 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2274fbd6ff5cSJohannes Berg 
2275dcba665bSJohannes Berg 		sband = local->hw.wiphy->bands[band];
22768b783d10SThomas Pedersen 
22778b783d10SThomas Pedersen 		if (WARN_ON_ONCE(!sband->bitrates))
22788b783d10SThomas Pedersen 			break;
22798b783d10SThomas Pedersen 
2280dcba665bSJohannes Berg 		brate = sband->bitrates[rate_idx].bitrate;
22814f6b1b3dSJohannes Berg 		if (rinfo->bw == RATE_INFO_BW_5)
22824f6b1b3dSJohannes Berg 			shift = 2;
22834f6b1b3dSJohannes Berg 		else if (rinfo->bw == RATE_INFO_BW_10)
22844f6b1b3dSJohannes Berg 			shift = 1;
22854f6b1b3dSJohannes Berg 		else
22864f6b1b3dSJohannes Berg 			shift = 0;
2287fbd6ff5cSJohannes Berg 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
22887f406cd1SJohannes Berg 		break;
22897f406cd1SJohannes Berg 		}
229041cbb0f5SLuca Coelho 	case STA_STATS_RATE_TYPE_HE:
229141cbb0f5SLuca Coelho 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
229241cbb0f5SLuca Coelho 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
229341cbb0f5SLuca Coelho 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
229441cbb0f5SLuca Coelho 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
229541cbb0f5SLuca Coelho 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
229641cbb0f5SLuca Coelho 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
229741cbb0f5SLuca Coelho 		break;
2298fbd6ff5cSJohannes Berg 	}
22994f6b1b3dSJohannes Berg }
2300fbd6ff5cSJohannes Berg 
2301a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
23024f6b1b3dSJohannes Berg {
23036aa7de05SMark Rutland 	u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate);
23044f6b1b3dSJohannes Berg 
23054f6b1b3dSJohannes Berg 	if (rate == STA_STATS_RATE_INVALID)
2306a17d93ffSBen Greear 		return -EINVAL;
2307a17d93ffSBen Greear 
23084f6b1b3dSJohannes Berg 	sta_stats_decode_rate(sta->local, rate, rinfo);
2309a17d93ffSBen Greear 	return 0;
2310fbd6ff5cSJohannes Berg }
2311fbd6ff5cSJohannes Berg 
2312b255b72bSSeevalamuthu Mariappan static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2313b255b72bSSeevalamuthu Mariappan 					int tid)
2314b255b72bSSeevalamuthu Mariappan {
2315b255b72bSSeevalamuthu Mariappan 	unsigned int start;
2316b255b72bSSeevalamuthu Mariappan 	u64 value;
2317b255b72bSSeevalamuthu Mariappan 
2318b255b72bSSeevalamuthu Mariappan 	do {
2319b255b72bSSeevalamuthu Mariappan 		start = u64_stats_fetch_begin(&rxstats->syncp);
2320b255b72bSSeevalamuthu Mariappan 		value = rxstats->msdu[tid];
2321b255b72bSSeevalamuthu Mariappan 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2322b255b72bSSeevalamuthu Mariappan 
2323b255b72bSSeevalamuthu Mariappan 	return value;
2324b255b72bSSeevalamuthu Mariappan }
2325b255b72bSSeevalamuthu Mariappan 
23260f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta,
23270f9c5a61SJohannes Berg 			     struct cfg80211_tid_stats *tidstats,
23280f9c5a61SJohannes Berg 			     int tid)
23290f9c5a61SJohannes Berg {
23300f9c5a61SJohannes Berg 	struct ieee80211_local *local = sta->local;
2331b255b72bSSeevalamuthu Mariappan 	int cpu;
23320f9c5a61SJohannes Berg 
23330f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2334046d2e7cSSriram R 		tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats,
2335046d2e7cSSriram R 							   tid);
23360f9c5a61SJohannes Berg 
2337046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2338b255b72bSSeevalamuthu Mariappan 			for_each_possible_cpu(cpu) {
2339b255b72bSSeevalamuthu Mariappan 				struct ieee80211_sta_rx_stats *cpurxs;
2340b255b72bSSeevalamuthu Mariappan 
2341046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2342046d2e7cSSriram R 						     cpu);
2343b255b72bSSeevalamuthu Mariappan 				tidstats->rx_msdu +=
2344b255b72bSSeevalamuthu Mariappan 					sta_get_tidstats_msdu(cpurxs, tid);
2345b255b72bSSeevalamuthu Mariappan 			}
2346b255b72bSSeevalamuthu Mariappan 		}
23470f9c5a61SJohannes Berg 
23480f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
23490f9c5a61SJohannes Berg 	}
23500f9c5a61SJohannes Berg 
23510f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
23520f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2353046d2e7cSSriram R 		tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid];
23540f9c5a61SJohannes Berg 	}
23550f9c5a61SJohannes Berg 
23560f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
23570f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
23580f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2359046d2e7cSSriram R 		tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid];
23600f9c5a61SJohannes Berg 	}
23610f9c5a61SJohannes Berg 
23620f9c5a61SJohannes Berg 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
23630f9c5a61SJohannes Berg 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
23640f9c5a61SJohannes Berg 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2365046d2e7cSSriram R 		tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid];
23660f9c5a61SJohannes Berg 	}
23672fe4a29aSToke Høiland-Jørgensen 
23682fe4a29aSToke Høiland-Jørgensen 	if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) {
23692fe4a29aSToke Høiland-Jørgensen 		spin_lock_bh(&local->fq.lock);
23702fe4a29aSToke Høiland-Jørgensen 		rcu_read_lock();
23712fe4a29aSToke Høiland-Jørgensen 
23722fe4a29aSToke Høiland-Jørgensen 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
23732fe4a29aSToke Høiland-Jørgensen 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
23742fe4a29aSToke Høiland-Jørgensen 					 to_txq_info(sta->sta.txq[tid]));
23752fe4a29aSToke Høiland-Jørgensen 
23762fe4a29aSToke Høiland-Jørgensen 		rcu_read_unlock();
23772fe4a29aSToke Høiland-Jørgensen 		spin_unlock_bh(&local->fq.lock);
23782fe4a29aSToke Høiland-Jørgensen 	}
23790f9c5a61SJohannes Berg }
23800f9c5a61SJohannes Berg 
2381c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2382c9c5962bSJohannes Berg {
2383c9c5962bSJohannes Berg 	unsigned int start;
2384c9c5962bSJohannes Berg 	u64 value;
2385c9c5962bSJohannes Berg 
2386c9c5962bSJohannes Berg 	do {
2387c9c5962bSJohannes Berg 		start = u64_stats_fetch_begin(&rxstats->syncp);
2388c9c5962bSJohannes Berg 		value = rxstats->bytes;
2389c9c5962bSJohannes Berg 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2390c9c5962bSJohannes Berg 
2391c9c5962bSJohannes Berg 	return value;
2392c9c5962bSJohannes Berg }
2393c9c5962bSJohannes Berg 
23940fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
23950fdf1493SJohannes Berg 		   bool tidstats)
2396b7ffbd7eSJohannes Berg {
2397b7ffbd7eSJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2398b7ffbd7eSJohannes Berg 	struct ieee80211_local *local = sdata->local;
2399b7ffbd7eSJohannes Berg 	u32 thr = 0;
2400c9c5962bSJohannes Berg 	int i, ac, cpu;
2401c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *last_rxstats;
2402c9c5962bSJohannes Berg 
2403c9c5962bSJohannes Berg 	last_rxstats = sta_get_last_rx_stats(sta);
2404b7ffbd7eSJohannes Berg 
2405b7ffbd7eSJohannes Berg 	sinfo->generation = sdata->local->sta_generation;
2406b7ffbd7eSJohannes Berg 
2407225b8189SJohannes Berg 	/* do before driver, so beacon filtering drivers have a
2408225b8189SJohannes Berg 	 * chance to e.g. just add the number of filtered beacons
2409225b8189SJohannes Berg 	 * (or just modify the value entirely, of course)
2410225b8189SJohannes Berg 	 */
2411225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2412bfd8403aSJohannes Berg 		sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
2413225b8189SJohannes Berg 
24142b9a7e1bSJohannes Berg 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
2415a4217750SOmer Efrat 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2416a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
2417a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2418a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
24199cf02338SBen Greear 			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
2420a4217750SOmer Efrat 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2421976bd9efSJohannes Berg 
2422976bd9efSJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2423bfd8403aSJohannes Berg 		sinfo->beacon_loss_count =
2424bfd8403aSJohannes Berg 			sdata->deflink.u.mgd.beacon_loss_count;
2425a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2426976bd9efSJohannes Berg 	}
2427b7ffbd7eSJohannes Berg 
242884b00607SArnd Bergmann 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
24299cf02338SBen Greear 	sinfo->assoc_at = sta->assoc_at;
2430e5a9f8d0SJohannes Berg 	sinfo->inactive_time =
2431b8da6b6aSJohannes Berg 		jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta));
24322b9a7e1bSJohannes Berg 
2433a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2434a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2435b7ffbd7eSJohannes Berg 		sinfo->tx_bytes = 0;
24362b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2437046d2e7cSSriram R 			sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
2438a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2439b7ffbd7eSJohannes Berg 	}
24402b9a7e1bSJohannes Berg 
2441a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
24422b9a7e1bSJohannes Berg 		sinfo->tx_packets = 0;
24432b9a7e1bSJohannes Berg 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2444046d2e7cSSriram R 			sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
2445a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
24462b9a7e1bSJohannes Berg 	}
24472b9a7e1bSJohannes Berg 
2448a4217750SOmer Efrat 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2449a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2450046d2e7cSSriram R 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
24510f9c5a61SJohannes Berg 
2452046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2453c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2454c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2455c9c5962bSJohannes Berg 
2456046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2457046d2e7cSSriram R 						     cpu);
2458c9c5962bSJohannes Berg 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
2459c9c5962bSJohannes Berg 			}
2460c9c5962bSJohannes Berg 		}
2461c9c5962bSJohannes Berg 
2462a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
24632b9a7e1bSJohannes Berg 	}
24642b9a7e1bSJohannes Berg 
2465a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2466046d2e7cSSriram R 		sinfo->rx_packets = sta->deflink.rx_stats.packets;
2467046d2e7cSSriram R 		if (sta->deflink.pcpu_rx_stats) {
2468c9c5962bSJohannes Berg 			for_each_possible_cpu(cpu) {
2469c9c5962bSJohannes Berg 				struct ieee80211_sta_rx_stats *cpurxs;
2470c9c5962bSJohannes Berg 
2471046d2e7cSSriram R 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
2472046d2e7cSSriram R 						     cpu);
2473c9c5962bSJohannes Berg 				sinfo->rx_packets += cpurxs->packets;
2474c9c5962bSJohannes Berg 			}
2475c9c5962bSJohannes Berg 		}
2476a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
24772b9a7e1bSJohannes Berg 	}
24782b9a7e1bSJohannes Berg 
2479a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2480046d2e7cSSriram R 		sinfo->tx_retries = sta->deflink.status_stats.retry_count;
2481a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
24822b9a7e1bSJohannes Berg 	}
24832b9a7e1bSJohannes Berg 
2484a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2485046d2e7cSSriram R 		sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
2486a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
24872b9a7e1bSJohannes Berg 	}
24882b9a7e1bSJohannes Berg 
2489b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2490b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2491b4809e94SToke Høiland-Jørgensen 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2492b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2493b4809e94SToke Høiland-Jørgensen 	}
2494b4809e94SToke Høiland-Jørgensen 
2495b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2496b4809e94SToke Høiland-Jørgensen 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2497b4809e94SToke Høiland-Jørgensen 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2498b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2499b4809e94SToke Høiland-Jørgensen 	}
2500b4809e94SToke Høiland-Jørgensen 
2501b4809e94SToke Høiland-Jørgensen 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
25022433647bSToke Høiland-Jørgensen 		sinfo->airtime_weight = sta->airtime[0].weight;
2503b4809e94SToke Høiland-Jørgensen 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2504b4809e94SToke Høiland-Jørgensen 	}
2505b4809e94SToke Høiland-Jørgensen 
2506046d2e7cSSriram R 	sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
2507046d2e7cSSriram R 	if (sta->deflink.pcpu_rx_stats) {
2508c9c5962bSJohannes Berg 		for_each_possible_cpu(cpu) {
2509c9c5962bSJohannes Berg 			struct ieee80211_sta_rx_stats *cpurxs;
2510c9c5962bSJohannes Berg 
2511046d2e7cSSriram R 			cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
2512e165bc02SJohannes Berg 			sinfo->rx_dropped_misc += cpurxs->dropped;
2513c9c5962bSJohannes Berg 		}
2514c9c5962bSJohannes Berg 	}
2515b7ffbd7eSJohannes Berg 
2516225b8189SJohannes Berg 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2517225b8189SJohannes Berg 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2518a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2519a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2520225b8189SJohannes Berg 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2521225b8189SJohannes Berg 	}
2522225b8189SJohannes Berg 
252330686bf7SJohannes Berg 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
252430686bf7SJohannes Berg 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2525a4217750SOmer Efrat 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2526c9c5962bSJohannes Berg 			sinfo->signal = (s8)last_rxstats->last_signal;
2527a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2528b7ffbd7eSJohannes Berg 		}
25292b9a7e1bSJohannes Berg 
2530046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats &&
2531a4217750SOmer Efrat 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
253240d9a38aSJohannes Berg 			sinfo->signal_avg =
2533046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
2534a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
25352b9a7e1bSJohannes Berg 		}
25362b9a7e1bSJohannes Berg 	}
25372b9a7e1bSJohannes Berg 
2538c9c5962bSJohannes Berg 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2539c9c5962bSJohannes Berg 	 * the sta->rx_stats struct, so the check here is fine with and without
2540c9c5962bSJohannes Berg 	 * pcpu statistics
2541c9c5962bSJohannes Berg 	 */
2542c9c5962bSJohannes Berg 	if (last_rxstats->chains &&
2543a4217750SOmer Efrat 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
2544a4217750SOmer Efrat 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2545a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2546046d2e7cSSriram R 		if (!sta->deflink.pcpu_rx_stats)
2547a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2548b7ffbd7eSJohannes Berg 
2549c9c5962bSJohannes Berg 		sinfo->chains = last_rxstats->chains;
2550c9c5962bSJohannes Berg 
2551b7ffbd7eSJohannes Berg 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2552e5a9f8d0SJohannes Berg 			sinfo->chain_signal[i] =
2553c9c5962bSJohannes Berg 				last_rxstats->chain_signal_last[i];
2554b7ffbd7eSJohannes Berg 			sinfo->chain_signal_avg[i] =
2555046d2e7cSSriram R 				-ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
2556b7ffbd7eSJohannes Berg 		}
2557b7ffbd7eSJohannes Berg 	}
2558b7ffbd7eSJohannes Berg 
2559a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
2560046d2e7cSSriram R 		sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
2561e5a9f8d0SJohannes Berg 				     &sinfo->txrate);
2562a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
25632b9a7e1bSJohannes Berg 	}
25642b9a7e1bSJohannes Berg 
2565a4217750SOmer Efrat 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
2566a17d93ffSBen Greear 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0)
2567a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
25682b9a7e1bSJohannes Berg 	}
2569b7ffbd7eSJohannes Berg 
25700fdf1493SJohannes Berg 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
25716af8354fSJohannes Berg 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
25726af8354fSJohannes Berg 			sta_set_tidstats(sta, &sinfo->pertid[i], i);
25738689c051SArend van Spriel 	}
257479c892b8SJohannes Berg 
2575b7ffbd7eSJohannes Berg 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2576b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH
2577a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2578a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLID) |
2579a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2580a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2581a4217750SOmer Efrat 				 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2582dbdaee7aSBob Copeland 				 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
25831303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
25841303a51cSMarkus Theil 				 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2585b7ffbd7eSJohannes Berg 
2586433f5bc1SJohannes Berg 		sinfo->llid = sta->mesh->llid;
2587433f5bc1SJohannes Berg 		sinfo->plid = sta->mesh->plid;
2588433f5bc1SJohannes Berg 		sinfo->plink_state = sta->mesh->plink_state;
2589b7ffbd7eSJohannes Berg 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2590a4217750SOmer Efrat 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2591433f5bc1SJohannes Berg 			sinfo->t_offset = sta->mesh->t_offset;
2592b7ffbd7eSJohannes Berg 		}
2593433f5bc1SJohannes Berg 		sinfo->local_pm = sta->mesh->local_pm;
2594433f5bc1SJohannes Berg 		sinfo->peer_pm = sta->mesh->peer_pm;
2595433f5bc1SJohannes Berg 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2596dbdaee7aSBob Copeland 		sinfo->connected_to_gate = sta->mesh->connected_to_gate;
25971303a51cSMarkus Theil 		sinfo->connected_to_as = sta->mesh->connected_to_as;
2598b7ffbd7eSJohannes Berg #endif
2599b7ffbd7eSJohannes Berg 	}
2600b7ffbd7eSJohannes Berg 
2601b7ffbd7eSJohannes Berg 	sinfo->bss_param.flags = 0;
2602b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_cts_prot)
2603b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2604b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_preamble)
2605b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2606b7ffbd7eSJohannes Berg 	if (sdata->vif.bss_conf.use_short_slot)
2607b7ffbd7eSJohannes Berg 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2608785e21a8SEmmanuel Grumbach 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2609b7ffbd7eSJohannes Berg 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2610b7ffbd7eSJohannes Berg 
2611b7ffbd7eSJohannes Berg 	sinfo->sta_flags.set = 0;
2612b7ffbd7eSJohannes Berg 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2613b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2614b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_WME) |
2615b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_MFP) |
2616b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2617b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2618b7ffbd7eSJohannes Berg 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2619b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2620b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2621b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2622b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2623a74a8c84SJohannes Berg 	if (sta->sta.wme)
2624b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2625b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_MFP))
2626b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2627b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2628b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2629b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2630b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2631b7ffbd7eSJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2632b7ffbd7eSJohannes Berg 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2633b7ffbd7eSJohannes Berg 
26343b17fbf8SMaxim Altshul 	thr = sta_get_expected_throughput(sta);
26353b17fbf8SMaxim Altshul 
26363b17fbf8SMaxim Altshul 	if (thr != 0) {
2637a4217750SOmer Efrat 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
26383b17fbf8SMaxim Altshul 		sinfo->expected_throughput = thr;
26393b17fbf8SMaxim Altshul 	}
2640a78b26ffSVenkateswara Naralasetty 
2641a78b26ffSVenkateswara Naralasetty 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
2642046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2643046d2e7cSSriram R 		sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
2644a78b26ffSVenkateswara Naralasetty 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
2645a78b26ffSVenkateswara Naralasetty 	}
2646cc60dbbfSBalaji Pothunoori 
26479c06602bSBalaji Pothunoori 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
2648046d2e7cSSriram R 	    sta->deflink.status_stats.ack_signal_filled) {
2649cc60dbbfSBalaji Pothunoori 		sinfo->avg_ack_signal =
2650cc60dbbfSBalaji Pothunoori 			-(s8)ewma_avg_signal_read(
2651046d2e7cSSriram R 				&sta->deflink.status_stats.avg_ack_signal);
2652cc60dbbfSBalaji Pothunoori 		sinfo->filled |=
26539c06602bSBalaji Pothunoori 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
2654cc60dbbfSBalaji Pothunoori 	}
2655ab60633cSNarayanraddi Masti 
2656ab60633cSNarayanraddi Masti 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2657ab60633cSNarayanraddi Masti 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2658ab60633cSNarayanraddi Masti 		sinfo->airtime_link_metric =
2659ab60633cSNarayanraddi Masti 			airtime_link_metric_get(local, sta);
2660ab60633cSNarayanraddi Masti 	}
26613b17fbf8SMaxim Altshul }
26623b17fbf8SMaxim Altshul 
26633b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta)
26643b17fbf8SMaxim Altshul {
26653b17fbf8SMaxim Altshul 	struct ieee80211_sub_if_data *sdata = sta->sdata;
26663b17fbf8SMaxim Altshul 	struct ieee80211_local *local = sdata->local;
26673b17fbf8SMaxim Altshul 	struct rate_control_ref *ref = NULL;
26683b17fbf8SMaxim Altshul 	u32 thr = 0;
26693b17fbf8SMaxim Altshul 
26703b17fbf8SMaxim Altshul 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
26713b17fbf8SMaxim Altshul 		ref = local->rate_ctrl;
26723b17fbf8SMaxim Altshul 
2673b7ffbd7eSJohannes Berg 	/* check if the driver has a SW RC implementation */
2674b7ffbd7eSJohannes Berg 	if (ref && ref->ops->get_expected_throughput)
2675b7ffbd7eSJohannes Berg 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2676b7ffbd7eSJohannes Berg 	else
26774fdbc67aSMaxim Altshul 		thr = drv_get_expected_throughput(local, sta);
2678b7ffbd7eSJohannes Berg 
26793b17fbf8SMaxim Altshul 	return thr;
2680b7ffbd7eSJohannes Berg }
2681b8da6b6aSJohannes Berg 
2682b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta)
2683b8da6b6aSJohannes Berg {
2684c9c5962bSJohannes Berg 	struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta);
2685c9c5962bSJohannes Berg 
2686046d2e7cSSriram R 	if (!sta->deflink.status_stats.last_ack ||
2687046d2e7cSSriram R 	    time_after(stats->last_rx, sta->deflink.status_stats.last_ack))
2688c9c5962bSJohannes Berg 		return stats->last_rx;
2689046d2e7cSSriram R 	return sta->deflink.status_stats.last_ack;
2690b8da6b6aSJohannes Berg }
2691484a54c2SToke Høiland-Jørgensen 
2692484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr)
2693484a54c2SToke Høiland-Jørgensen {
2694484a54c2SToke Høiland-Jørgensen 	if (!sta->sdata->local->ops->wake_tx_queue)
2695484a54c2SToke Høiland-Jørgensen 		return;
2696484a54c2SToke Høiland-Jørgensen 
2697484a54c2SToke Høiland-Jørgensen 	if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) {
2698484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(50);
2699484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(300);
2700484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = false;
2701484a54c2SToke Høiland-Jørgensen 	} else {
2702484a54c2SToke Høiland-Jørgensen 		sta->cparams.target = MS2TIME(20);
2703484a54c2SToke Høiland-Jørgensen 		sta->cparams.interval = MS2TIME(100);
2704484a54c2SToke Høiland-Jørgensen 		sta->cparams.ecn = true;
2705484a54c2SToke Høiland-Jørgensen 	}
2706484a54c2SToke Høiland-Jørgensen }
2707484a54c2SToke Høiland-Jørgensen 
2708484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta,
2709484a54c2SToke Høiland-Jørgensen 					   u32 thr)
2710484a54c2SToke Høiland-Jørgensen {
2711484a54c2SToke Høiland-Jørgensen 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2712484a54c2SToke Høiland-Jørgensen 
2713484a54c2SToke Høiland-Jørgensen 	sta_update_codel_params(sta, thr);
2714484a54c2SToke Høiland-Jørgensen }
2715cb71f1d1SJohannes Berg 
2716cb71f1d1SJohannes Berg int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
2717cb71f1d1SJohannes Berg {
2718cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2719cb71f1d1SJohannes Berg 	struct sta_link_alloc *alloc;
2720cb71f1d1SJohannes Berg 	int ret;
2721cb71f1d1SJohannes Berg 
2722cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2723cb71f1d1SJohannes Berg 
2724cb71f1d1SJohannes Berg 	/* must represent an MLD from the start */
2725cb71f1d1SJohannes Berg 	if (WARN_ON(!sta->sta.valid_links))
2726cb71f1d1SJohannes Berg 		return -EINVAL;
2727cb71f1d1SJohannes Berg 
2728cb71f1d1SJohannes Berg 	if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
2729cb71f1d1SJohannes Berg 		    sta->link[link_id]))
2730cb71f1d1SJohannes Berg 		return -EBUSY;
2731cb71f1d1SJohannes Berg 
2732cb71f1d1SJohannes Berg 	alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
2733cb71f1d1SJohannes Berg 	if (!alloc)
2734cb71f1d1SJohannes Berg 		return -ENOMEM;
2735cb71f1d1SJohannes Berg 
2736cb71f1d1SJohannes Berg 	ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
2737cb71f1d1SJohannes Berg 	if (ret) {
2738cb71f1d1SJohannes Berg 		kfree(alloc);
2739cb71f1d1SJohannes Berg 		return ret;
2740cb71f1d1SJohannes Berg 	}
2741cb71f1d1SJohannes Berg 
2742cb71f1d1SJohannes Berg 	sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
2743cb71f1d1SJohannes Berg 
2744cb71f1d1SJohannes Berg 	return 0;
2745cb71f1d1SJohannes Berg }
2746cb71f1d1SJohannes Berg 
2747*ba6ddab9SJohannes Berg static int link_sta_info_hash_add(struct ieee80211_local *local,
2748*ba6ddab9SJohannes Berg 				  struct link_sta_info *link_sta)
2749*ba6ddab9SJohannes Berg {
2750*ba6ddab9SJohannes Berg 	return rhltable_insert(&local->link_sta_hash,
2751*ba6ddab9SJohannes Berg 			       &link_sta->link_hash_node,
2752*ba6ddab9SJohannes Berg 			       link_sta_rht_params);
2753*ba6ddab9SJohannes Berg }
2754*ba6ddab9SJohannes Berg 
2755cb71f1d1SJohannes Berg int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
2756cb71f1d1SJohannes Berg {
2757cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2758c71420dbSJohannes Berg 	struct link_sta_info *link_sta;
2759cb71f1d1SJohannes Berg 	u16 old_links = sta->sta.valid_links;
2760cb71f1d1SJohannes Berg 	u16 new_links = old_links | BIT(link_id);
2761cb71f1d1SJohannes Berg 	int ret;
2762cb71f1d1SJohannes Berg 
2763c71420dbSJohannes Berg 	link_sta = rcu_dereference_protected(sta->link[link_id],
2764c71420dbSJohannes Berg 					     lockdep_is_held(&sdata->local->sta_mtx));
2765cb71f1d1SJohannes Berg 
2766c71420dbSJohannes Berg 	if (WARN_ON(old_links == new_links || !link_sta))
2767cb71f1d1SJohannes Berg 		return -EINVAL;
2768cb71f1d1SJohannes Berg 
2769cb71f1d1SJohannes Berg 	sta->sta.valid_links = new_links;
2770cb71f1d1SJohannes Berg 
2771*ba6ddab9SJohannes Berg 	if (!test_sta_flag(sta, WLAN_STA_INSERTED)) {
2772*ba6ddab9SJohannes Berg 		ret = 0;
2773*ba6ddab9SJohannes Berg 		goto hash;
2774*ba6ddab9SJohannes Berg 	}
2775cb71f1d1SJohannes Berg 
2776cb71f1d1SJohannes Berg 	ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
2777cb71f1d1SJohannes Berg 				   old_links, new_links);
2778cb71f1d1SJohannes Berg 	if (ret) {
2779cb71f1d1SJohannes Berg 		sta->sta.valid_links = old_links;
2780*ba6ddab9SJohannes Berg 		sta_remove_link(sta, link_id, false);
2781cb71f1d1SJohannes Berg 	}
2782cb71f1d1SJohannes Berg 
2783*ba6ddab9SJohannes Berg hash:
2784*ba6ddab9SJohannes Berg 	link_sta_info_hash_add(sdata->local, link_sta);
2785*ba6ddab9SJohannes Berg 
2786cb71f1d1SJohannes Berg 	return ret;
2787cb71f1d1SJohannes Berg }
2788cb71f1d1SJohannes Berg 
2789cb71f1d1SJohannes Berg void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
2790cb71f1d1SJohannes Berg {
2791cb71f1d1SJohannes Berg 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2792cb71f1d1SJohannes Berg 
2793cb71f1d1SJohannes Berg 	lockdep_assert_held(&sdata->local->sta_mtx);
2794cb71f1d1SJohannes Berg 
2795cb71f1d1SJohannes Berg 	sta->sta.valid_links &= ~BIT(link_id);
2796cb71f1d1SJohannes Berg 
2797cb71f1d1SJohannes Berg 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
2798cb71f1d1SJohannes Berg 		drv_change_sta_links(sdata->local, sdata, &sta->sta,
2799cb71f1d1SJohannes Berg 				     sta->sta.valid_links,
2800cb71f1d1SJohannes Berg 				     sta->sta.valid_links & ~BIT(link_id));
2801cb71f1d1SJohannes Berg 
2802*ba6ddab9SJohannes Berg 	sta_remove_link(sta, link_id, true);
2803cb71f1d1SJohannes Berg }
2804