xref: /linux/net/mac80211/sta_info.c (revision 67a9e80c3ca20c9044cd6ee72cd6278dd19b5023)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2002-2005, Instant802 Networks, Inc.
4  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
5  * Copyright 2013-2014  Intel Mobile Communications GmbH
6  * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
7  * Copyright (C) 2018-2026 Intel Corporation
8  */
9 
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netdevice.h>
14 #include <linux/types.h>
15 #include <linux/slab.h>
16 #include <linux/skbuff.h>
17 #include <linux/if_arp.h>
18 #include <linux/timer.h>
19 #include <linux/rtnetlink.h>
20 
21 #include <net/mac80211.h>
22 #include "ieee80211_i.h"
23 #include "driver-ops.h"
24 #include "rate.h"
25 #include "sta_info.h"
26 #include "debugfs_sta.h"
27 #include "mesh.h"
28 #include "wme.h"
29 
30 /**
31  * DOC: STA information lifetime rules
32  *
33  * STA info structures (&struct sta_info) are managed in a hash table
34  * for faster lookup and a list for iteration. They are managed using
35  * RCU, i.e. access to the list and hash table is protected by RCU.
36  *
37  * Upon allocating a STA info structure with sta_info_alloc(), the caller
38  * owns that structure. It must then insert it into the hash table using
39  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
40  * case (which acquires an rcu read section but must not be called from
41  * within one) will the pointer still be valid after the call. Note that
42  * the caller may not do much with the STA info before inserting it; in
43  * particular, it may not start any mesh peer link management or add
44  * encryption keys.
45  *
46  * When the insertion fails (sta_info_insert()) returns non-zero), the
47  * structure will have been freed by sta_info_insert()!
48  *
49  * Station entries are added by mac80211 when you establish a link with a
50  * peer. This means different things for the different type of interfaces
51  * we support. For a regular station this mean we add the AP sta when we
52  * receive an association response from the AP. For IBSS this occurs when
53  * get to know about a peer on the same IBSS. For WDS we add the sta for
54  * the peer immediately upon device open. When using AP mode we add stations
55  * for each respective station upon request from userspace through nl80211.
56  *
57  * In order to remove a STA info structure, various sta_info_destroy_*()
58  * calls are available.
59  *
60  * There is no concept of ownership on a STA entry; each structure is
61  * owned by the global hash table/list until it is removed. All users of
62  * the structure need to be RCU protected so that the structure won't be
63  * freed before they are done using it.
64  */
65 
66 struct sta_link_alloc {
67 	struct link_sta_info info;
68 	struct ieee80211_link_sta sta;
69 	struct rcu_head rcu_head;
70 };
71 
72 static const struct rhashtable_params sta_rht_params = {
73 	.nelem_hint = 3, /* start small */
74 	.automatic_shrinking = true,
75 	.head_offset = offsetof(struct sta_info, hash_node),
76 	.key_offset = offsetof(struct sta_info, addr),
77 	.key_len = ETH_ALEN,
78 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
79 };
80 
81 static const struct rhashtable_params link_sta_rht_params = {
82 	.nelem_hint = 3, /* start small */
83 	.automatic_shrinking = true,
84 	.head_offset = offsetof(struct link_sta_info, link_hash_node),
85 	.key_offset = offsetof(struct link_sta_info, addr),
86 	.key_len = ETH_ALEN,
87 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
88 };
89 
90 static int sta_info_hash_del(struct ieee80211_local *local,
91 			     struct sta_info *sta)
92 {
93 	return rhltable_remove(&local->sta_hash, &sta->hash_node,
94 			       sta_rht_params);
95 }
96 
97 static int link_sta_info_hash_add(struct ieee80211_local *local,
98 				  struct link_sta_info *link_sta)
99 {
100 	lockdep_assert_wiphy(local->hw.wiphy);
101 
102 	return rhltable_insert(&local->link_sta_hash,
103 			       &link_sta->link_hash_node, link_sta_rht_params);
104 }
105 
106 static int link_sta_info_hash_del(struct ieee80211_local *local,
107 				  struct link_sta_info *link_sta)
108 {
109 	lockdep_assert_wiphy(local->hw.wiphy);
110 
111 	return rhltable_remove(&local->link_sta_hash,
112 			       &link_sta->link_hash_node, link_sta_rht_params);
113 }
114 
115 void ieee80211_purge_sta_txqs(struct sta_info *sta)
116 {
117 	struct ieee80211_local *local = sta->sdata->local;
118 	int i;
119 
120 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
121 		struct txq_info *txqi;
122 
123 		if (!sta->sta.txq[i])
124 			continue;
125 
126 		txqi = to_txq_info(sta->sta.txq[i]);
127 
128 		ieee80211_txq_purge(local, txqi);
129 	}
130 }
131 
132 static void __cleanup_single_sta(struct sta_info *sta)
133 {
134 	int ac, i;
135 	struct tid_ampdu_tx *tid_tx;
136 	struct ieee80211_sub_if_data *sdata = sta->sdata;
137 	struct ieee80211_local *local = sdata->local;
138 	struct ps_data *ps;
139 
140 	cancel_work_sync(&sta->drv_deliver_wk);
141 
142 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
143 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
144 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
145 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
146 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
147 			ps = &sdata->bss->ps;
148 		else if (ieee80211_vif_is_mesh(&sdata->vif))
149 			ps = &sdata->u.mesh.ps;
150 		else
151 			return;
152 
153 		clear_sta_flag(sta, WLAN_STA_PS_STA);
154 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
155 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
156 
157 		atomic_dec(&ps->num_sta_ps);
158 	}
159 
160 	ieee80211_purge_sta_txqs(sta);
161 
162 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
163 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
164 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
165 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
166 	}
167 
168 	if (ieee80211_vif_is_mesh(&sdata->vif))
169 		mesh_sta_cleanup(sta);
170 
171 	/*
172 	 * Destroy aggregation state here. It would be nice to wait for the
173 	 * driver to finish aggregation stop and then clean up, but for now
174 	 * drivers have to handle aggregation stop being requested, followed
175 	 * directly by station destruction.
176 	 */
177 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
178 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
179 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
180 		if (!tid_tx)
181 			continue;
182 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
183 		kfree(tid_tx);
184 	}
185 }
186 
187 static void cleanup_single_sta(struct sta_info *sta)
188 {
189 	struct ieee80211_sub_if_data *sdata = sta->sdata;
190 	struct ieee80211_local *local = sdata->local;
191 
192 	__cleanup_single_sta(sta);
193 	sta_info_free(local, sta);
194 }
195 
196 struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local,
197 					 const u8 *addr)
198 {
199 	return rhltable_lookup(&local->sta_hash, addr, sta_rht_params);
200 }
201 
202 /* protected by RCU */
203 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
204 			      const u8 *addr)
205 {
206 	struct ieee80211_local *local = sdata->local;
207 	struct rhlist_head *tmp;
208 	struct sta_info *sta;
209 
210 	rcu_read_lock();
211 	for_each_sta_info(local, addr, sta, tmp) {
212 		if (sta->sdata == sdata) {
213 			rcu_read_unlock();
214 			/* this is safe as the caller must already hold
215 			 * another rcu read section or the mutex
216 			 */
217 			return sta;
218 		}
219 	}
220 	rcu_read_unlock();
221 	return NULL;
222 }
223 
224 /*
225  * Get sta info either from the specified interface
226  * or from one of its vlans
227  */
228 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
229 				  const u8 *addr)
230 {
231 	struct ieee80211_local *local = sdata->local;
232 	struct rhlist_head *tmp;
233 	struct sta_info *sta;
234 
235 	rcu_read_lock();
236 	for_each_sta_info(local, addr, sta, tmp) {
237 		if (sta->sdata == sdata ||
238 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
239 			rcu_read_unlock();
240 			/* this is safe as the caller must already hold
241 			 * another rcu read section or the mutex
242 			 */
243 			return sta;
244 		}
245 	}
246 	rcu_read_unlock();
247 	return NULL;
248 }
249 
250 struct rhlist_head *link_sta_info_hash_lookup(struct ieee80211_local *local,
251 					      const u8 *addr)
252 {
253 	return rhltable_lookup(&local->link_sta_hash, addr,
254 			       link_sta_rht_params);
255 }
256 
257 struct link_sta_info *
258 link_sta_info_get_bss(struct ieee80211_sub_if_data *sdata, const u8 *addr)
259 {
260 	struct ieee80211_local *local = sdata->local;
261 	struct rhlist_head *tmp;
262 	struct link_sta_info *link_sta;
263 
264 	rcu_read_lock();
265 	for_each_link_sta_info(local, addr, link_sta, tmp) {
266 		struct sta_info *sta = link_sta->sta;
267 
268 		if (sta->sdata == sdata ||
269 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
270 			rcu_read_unlock();
271 			/* this is safe as the caller must already hold
272 			 * another rcu read section or the mutex
273 			 */
274 			return link_sta;
275 		}
276 	}
277 	rcu_read_unlock();
278 	return NULL;
279 }
280 
281 struct ieee80211_sta *
282 ieee80211_find_sta_by_link_addrs(struct ieee80211_hw *hw,
283 				 const u8 *addr,
284 				 const u8 *localaddr,
285 				 unsigned int *link_id)
286 {
287 	struct ieee80211_local *local = hw_to_local(hw);
288 	struct link_sta_info *link_sta;
289 	struct rhlist_head *tmp;
290 
291 	for_each_link_sta_info(local, addr, link_sta, tmp) {
292 		struct sta_info *sta = link_sta->sta;
293 		struct ieee80211_link_data *link;
294 		u8 _link_id = link_sta->link_id;
295 
296 		if (!localaddr) {
297 			if (link_id)
298 				*link_id = _link_id;
299 			return &sta->sta;
300 		}
301 
302 		link = rcu_dereference(sta->sdata->link[_link_id]);
303 		if (!link)
304 			continue;
305 
306 		if (memcmp(link->conf->addr, localaddr, ETH_ALEN))
307 			continue;
308 
309 		if (link_id)
310 			*link_id = _link_id;
311 		return &sta->sta;
312 	}
313 
314 	return NULL;
315 }
316 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_link_addrs);
317 
318 struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local,
319 				       const u8 *sta_addr, const u8 *vif_addr)
320 {
321 	struct rhlist_head *tmp;
322 	struct sta_info *sta;
323 
324 	for_each_sta_info(local, sta_addr, sta, tmp) {
325 		if (ether_addr_equal(vif_addr, sta->sdata->vif.addr))
326 			return sta;
327 	}
328 
329 	return NULL;
330 }
331 
332 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
333 				     int idx)
334 {
335 	struct ieee80211_local *local = sdata->local;
336 	struct sta_info *sta;
337 	int i = 0;
338 
339 	list_for_each_entry_rcu(sta, &local->sta_list, list,
340 				lockdep_is_held(&local->hw.wiphy->mtx)) {
341 		if (sdata != sta->sdata)
342 			continue;
343 		if (i < idx) {
344 			++i;
345 			continue;
346 		}
347 		return sta;
348 	}
349 
350 	return NULL;
351 }
352 
353 static void sta_info_free_link(struct link_sta_info *link_sta)
354 {
355 	free_percpu(link_sta->pcpu_rx_stats);
356 }
357 
358 static void sta_link_free_rcu(struct rcu_head *head)
359 {
360 	struct sta_link_alloc *alloc =
361 		container_of(head, struct sta_link_alloc, rcu_head);
362 
363 	sta_info_free_link(&alloc->info);
364 	kfree(alloc);
365 }
366 
367 static void sta_accumulate_removed_link_stats(struct sta_info *sta, int link_id)
368 {
369 	struct link_sta_info *link_sta = wiphy_dereference(sta->local->hw.wiphy,
370 							   sta->link[link_id]);
371 	struct ieee80211_link_data *link;
372 	unsigned int start;
373 	int ac, tid;
374 	u64 value;
375 	u32 thr;
376 
377 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
378 		sta->rem_link_stats.tx_packets +=
379 			link_sta->tx_stats.packets[ac];
380 		sta->rem_link_stats.tx_bytes += link_sta->tx_stats.bytes[ac];
381 	}
382 
383 	do {
384 		start = u64_stats_fetch_begin(&link_sta->rx_stats.syncp);
385 		value = u64_stats_read(&link_sta->rx_stats.bytes);
386 	} while (u64_stats_fetch_retry(&link_sta->rx_stats.syncp, start));
387 
388 	sta->rem_link_stats.rx_packets += link_sta->rx_stats.packets;
389 	sta->rem_link_stats.rx_bytes += value;
390 	sta->rem_link_stats.tx_retries += link_sta->status_stats.retry_count;
391 	sta->rem_link_stats.tx_failed += link_sta->status_stats.retry_failed;
392 	sta->rem_link_stats.rx_dropped_misc += link_sta->rx_stats.dropped;
393 
394 	thr = sta_get_expected_throughput(sta);
395 	if (thr != 0)
396 		sta->rem_link_stats.expected_throughput += thr;
397 
398 	for (tid = 0; tid < IEEE80211_NUM_TIDS; tid++) {
399 		do {
400 			start = u64_stats_fetch_begin(&link_sta->rx_stats.syncp);
401 			value = u64_stats_read(&link_sta->rx_stats.msdu[tid]);
402 		} while (u64_stats_fetch_retry(&link_sta->rx_stats.syncp,
403 					       start));
404 
405 		sta->rem_link_stats.pertid_stats.rx_msdu += value;
406 		sta->rem_link_stats.pertid_stats.tx_msdu +=
407 			link_sta->tx_stats.msdu[tid];
408 		sta->rem_link_stats.pertid_stats.tx_msdu_retries +=
409 			link_sta->status_stats.msdu_retries[tid];
410 		sta->rem_link_stats.pertid_stats.tx_msdu_failed +=
411 			link_sta->status_stats.msdu_failed[tid];
412 	}
413 
414 	if (sta->sdata->vif.type == NL80211_IFTYPE_STATION) {
415 		link = wiphy_dereference(sta->sdata->local->hw.wiphy,
416 					 sta->sdata->link[link_id]);
417 		if (link)
418 			sta->rem_link_stats.beacon_loss_count +=
419 				link->u.mgd.beacon_loss_count;
420 	}
421 }
422 
423 static void sta_remove_link(struct sta_info *sta, unsigned int link_id,
424 			    bool unhash)
425 {
426 	struct sta_link_alloc *alloc = NULL;
427 	struct link_sta_info *link_sta;
428 
429 	lockdep_assert_wiphy(sta->local->hw.wiphy);
430 
431 	link_sta = rcu_access_pointer(sta->link[link_id]);
432 	if (WARN_ON(!link_sta))
433 		return;
434 
435 	if (unhash)
436 		link_sta_info_hash_del(sta->local, link_sta);
437 
438 	if (test_sta_flag(sta, WLAN_STA_INSERTED))
439 		ieee80211_link_sta_debugfs_remove(link_sta);
440 
441 	if (link_sta != &sta->deflink)
442 		alloc = container_of(link_sta, typeof(*alloc), info);
443 
444 	sta->sta.valid_links &= ~BIT(link_id);
445 
446 	/* store removed link info for accumulated stats consistency */
447 	sta_accumulate_removed_link_stats(sta, link_id);
448 
449 	RCU_INIT_POINTER(sta->link[link_id], NULL);
450 	RCU_INIT_POINTER(sta->sta.link[link_id], NULL);
451 	if (alloc)
452 		call_rcu(&alloc->rcu_head, sta_link_free_rcu);
453 
454 	ieee80211_sta_recalc_aggregates(&sta->sta);
455 }
456 
457 /**
458  * sta_info_free - free STA
459  *
460  * @local: pointer to the global information
461  * @sta: STA info to free
462  *
463  * This function must undo everything done by sta_info_alloc()
464  * that may happen before sta_info_insert(). It may only be
465  * called when sta_info_insert() has not been attempted (and
466  * if that fails, the station is freed anyway.)
467  */
468 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
469 {
470 	int i;
471 
472 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
473 		struct link_sta_info *link_sta;
474 
475 		link_sta = rcu_access_pointer(sta->link[i]);
476 		if (!link_sta)
477 			continue;
478 
479 		sta_remove_link(sta, i, false);
480 	}
481 
482 	/*
483 	 * If we had used sta_info_pre_move_state() then we might not
484 	 * have gone through the state transitions down again, so do
485 	 * it here now (and warn if it's inserted).
486 	 *
487 	 * This will clear state such as fast TX/RX that may have been
488 	 * allocated during state transitions.
489 	 */
490 	while (sta->sta_state > IEEE80211_STA_NONE) {
491 		int ret;
492 
493 		WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED));
494 
495 		ret = sta_info_move_state(sta, sta->sta_state - 1);
496 		if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret))
497 			break;
498 	}
499 
500 	if (sta->rate_ctrl)
501 		rate_control_free_sta(sta);
502 
503 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
504 
505 	kfree(to_txq_info(sta->sta.txq[0]));
506 	kfree(rcu_dereference_raw(sta->sta.rates));
507 #ifdef CONFIG_MAC80211_MESH
508 	kfree(sta->mesh);
509 #endif
510 
511 	sta_info_free_link(&sta->deflink);
512 	kfree(sta);
513 }
514 
515 static int sta_info_hash_add(struct ieee80211_local *local,
516 			     struct sta_info *sta)
517 {
518 	return rhltable_insert(&local->sta_hash, &sta->hash_node,
519 			       sta_rht_params);
520 }
521 
522 static void sta_deliver_ps_frames(struct work_struct *wk)
523 {
524 	struct sta_info *sta;
525 
526 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
527 
528 	if (sta->dead)
529 		return;
530 
531 	local_bh_disable();
532 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
533 		ieee80211_sta_ps_deliver_wakeup(sta);
534 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
535 		ieee80211_sta_ps_deliver_poll_response(sta);
536 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
537 		ieee80211_sta_ps_deliver_uapsd(sta);
538 	local_bh_enable();
539 }
540 
541 static int sta_prepare_rate_control(struct ieee80211_local *local,
542 				    struct sta_info *sta, gfp_t gfp)
543 {
544 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
545 		return 0;
546 
547 	sta->rate_ctrl = local->rate_ctrl;
548 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
549 						     sta, gfp);
550 	if (!sta->rate_ctrl_priv)
551 		return -ENOMEM;
552 
553 	return 0;
554 }
555 
556 static int sta_info_alloc_link(struct ieee80211_local *local,
557 			       struct link_sta_info *link_info,
558 			       gfp_t gfp)
559 {
560 	struct ieee80211_hw *hw = &local->hw;
561 	int i;
562 
563 	if (ieee80211_hw_check(hw, USES_RSS)) {
564 		link_info->pcpu_rx_stats =
565 			alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp);
566 		if (!link_info->pcpu_rx_stats)
567 			return -ENOMEM;
568 	}
569 
570 	link_info->rx_stats.last_rx = jiffies;
571 	u64_stats_init(&link_info->rx_stats.syncp);
572 
573 	ewma_signal_init(&link_info->rx_stats_avg.signal);
574 	ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal);
575 	for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++)
576 		ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]);
577 
578 	link_info->rx_omi_bw_rx = IEEE80211_STA_RX_BW_MAX;
579 	link_info->rx_omi_bw_tx = IEEE80211_STA_RX_BW_MAX;
580 	link_info->rx_omi_bw_staging = IEEE80211_STA_RX_BW_MAX;
581 
582 	/*
583 	 * This will always be taken into account, so set to MAX.
584 	 * When mac80211 is the client on a UHR AP, it'll be used
585 	 * for the TX side, to limit the bandwidth to TX to the AP
586 	 * with, to limit to the BSS width during DBE enablement.
587 	 *
588 	 * This is needed since the chanreq, which normally has
589 	 * maximum bandwidth to use with the AP, will already be
590 	 * set to the DBE width during enablement to prepare for
591 	 * RX (and not be racy), but the TX can only use higher
592 	 * bandwidth after enablement finishes.
593 	 */
594 	link_info->uhr_usable_tx_width = IEEE80211_STA_RX_BW_MAX;
595 
596 	link_info->op_mode_bw = IEEE80211_STA_RX_BW_MAX;
597 
598 	/*
599 	 * Cause (a) warning(s) if IEEE80211_STA_RX_BW_MAX != 320
600 	 * or if new values are added to the enum.
601 	 */
602 	switch (link_info->op_mode_bw) {
603 	case IEEE80211_STA_RX_BW_20:
604 	case IEEE80211_STA_RX_BW_40:
605 	case IEEE80211_STA_RX_BW_80:
606 	case IEEE80211_STA_RX_BW_160:
607 	case IEEE80211_STA_RX_BW_MAX:
608 		/* intentionally nothing */
609 		break;
610 	}
611 
612 	return 0;
613 }
614 
615 static void sta_info_add_link(struct sta_info *sta,
616 			      unsigned int link_id,
617 			      struct link_sta_info *link_info,
618 			      struct ieee80211_link_sta *link_sta)
619 {
620 	link_info->sta = sta;
621 	link_info->link_id = link_id;
622 	link_info->pub = link_sta;
623 	link_info->pub->sta = &sta->sta;
624 	link_sta->link_id = link_id;
625 	rcu_assign_pointer(sta->link[link_id], link_info);
626 	rcu_assign_pointer(sta->sta.link[link_id], link_sta);
627 
628 	link_sta->smps_mode = IEEE80211_SMPS_OFF;
629 	link_sta->agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
630 }
631 
632 static struct sta_info *
633 __sta_info_alloc(struct ieee80211_sub_if_data *sdata,
634 		 const u8 *addr, int link_id, const u8 *link_addr,
635 		 gfp_t gfp)
636 {
637 	struct ieee80211_local *local = sdata->local;
638 	struct ieee80211_hw *hw = &local->hw;
639 	struct sta_info *sta;
640 	void *txq_data;
641 	int size;
642 	int i;
643 
644 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
645 	if (!sta)
646 		return NULL;
647 
648 	sta->local = local;
649 	sta->sdata = sdata;
650 
651 	if (sta_info_alloc_link(local, &sta->deflink, gfp))
652 		goto free;
653 
654 	if (link_id >= 0) {
655 		sta_info_add_link(sta, link_id, &sta->deflink,
656 				  &sta->sta.deflink);
657 		sta->sta.valid_links = BIT(link_id);
658 	} else {
659 		sta_info_add_link(sta, 0, &sta->deflink, &sta->sta.deflink);
660 	}
661 
662 	sta->sta.cur = &sta->sta.deflink.agg;
663 
664 	spin_lock_init(&sta->lock);
665 	spin_lock_init(&sta->ps_lock);
666 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
667 	wiphy_work_init(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
668 #ifdef CONFIG_MAC80211_MESH
669 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
670 		sta->mesh = kzalloc_obj(*sta->mesh, gfp);
671 		if (!sta->mesh)
672 			goto free;
673 		sta->mesh->plink_sta = sta;
674 		spin_lock_init(&sta->mesh->plink_lock);
675 		if (!sdata->u.mesh.user_mpm)
676 			timer_setup(&sta->mesh->plink_timer, mesh_plink_timer,
677 				    0);
678 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
679 	}
680 #endif
681 
682 	memcpy(sta->addr, addr, ETH_ALEN);
683 	memcpy(sta->sta.addr, addr, ETH_ALEN);
684 	memcpy(sta->deflink.addr, link_addr, ETH_ALEN);
685 	memcpy(sta->sta.deflink.addr, link_addr, ETH_ALEN);
686 	sta->sta.max_rx_aggregation_subframes =
687 		local->hw.max_rx_aggregation_subframes;
688 
689 	/* TODO link specific alloc and assignments for MLO Link STA */
690 
691 	/* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only.
692 	 * The Tx path starts to use a key as soon as the key slot ptk_idx
693 	 * references to is not NULL. To not use the initial Rx-only key
694 	 * prematurely for Tx initialize ptk_idx to an impossible PTK keyid
695 	 * which always will refer to a NULL key.
696 	 */
697 	BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX);
698 	sta->ptk_idx = INVALID_PTK_KEYIDX;
699 
700 
701 	ieee80211_init_frag_cache(&sta->frags);
702 
703 	sta->sta_state = IEEE80211_STA_NONE;
704 
705 	if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
706 		sta->amsdu_mesh_control = -1;
707 
708 	/* Mark TID as unreserved */
709 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
710 
711 	sta->last_connected = ktime_get_seconds();
712 
713 	size = sizeof(struct txq_info) +
714 	       ALIGN(hw->txq_data_size, sizeof(void *));
715 
716 	txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
717 	if (!txq_data)
718 		goto free;
719 
720 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
721 		struct txq_info *txq = txq_data + i * size;
722 
723 		/* might not do anything for the (bufferable) MMPDU TXQ */
724 		ieee80211_txq_init(sdata, sta, txq, i);
725 	}
726 
727 	if (sta_prepare_rate_control(local, sta, gfp))
728 		goto free_txq;
729 
730 	sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT;
731 
732 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
733 		skb_queue_head_init(&sta->ps_tx_buf[i]);
734 		skb_queue_head_init(&sta->tx_filtered[i]);
735 		sta->airtime[i].deficit = sta->airtime_weight;
736 		atomic_set(&sta->airtime[i].aql_tx_pending, 0);
737 		sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i];
738 		sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i];
739 	}
740 
741 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
742 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
743 
744 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
745 		u32 mandatory = 0;
746 		int r;
747 
748 		if (!hw->wiphy->bands[i])
749 			continue;
750 
751 		switch (i) {
752 		case NL80211_BAND_2GHZ:
753 		case NL80211_BAND_LC:
754 			/*
755 			 * We use both here, even if we cannot really know for
756 			 * sure the station will support both, but the only use
757 			 * for this is when we don't know anything yet and send
758 			 * management frames, and then we'll pick the lowest
759 			 * possible rate anyway.
760 			 * If we don't include _G here, we cannot find a rate
761 			 * in P2P, and thus trigger the WARN_ONCE() in rate.c
762 			 */
763 			mandatory = IEEE80211_RATE_MANDATORY_B |
764 				    IEEE80211_RATE_MANDATORY_G;
765 			break;
766 		case NL80211_BAND_5GHZ:
767 		case NL80211_BAND_6GHZ:
768 			mandatory = IEEE80211_RATE_MANDATORY_A;
769 			break;
770 		case NL80211_BAND_60GHZ:
771 			WARN_ON(1);
772 			mandatory = 0;
773 			break;
774 		}
775 
776 		for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) {
777 			struct ieee80211_rate *rate;
778 
779 			rate = &hw->wiphy->bands[i]->bitrates[r];
780 
781 			if (!(rate->flags & mandatory))
782 				continue;
783 			sta->sta.deflink.supp_rates[i] |= BIT(r);
784 		}
785 	}
786 
787 
788 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
789 
790 	return sta;
791 
792 free_txq:
793 	kfree(to_txq_info(sta->sta.txq[0]));
794 free:
795 	sta_info_free_link(&sta->deflink);
796 #ifdef CONFIG_MAC80211_MESH
797 	kfree(sta->mesh);
798 #endif
799 	kfree(sta);
800 	return NULL;
801 }
802 
803 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
804 				const u8 *addr, gfp_t gfp)
805 {
806 	return __sta_info_alloc(sdata, addr, -1, addr, gfp);
807 }
808 
809 struct sta_info *sta_info_alloc_with_link(struct ieee80211_sub_if_data *sdata,
810 					  const u8 *mld_addr,
811 					  unsigned int link_id,
812 					  const u8 *link_addr,
813 					  gfp_t gfp)
814 {
815 	return __sta_info_alloc(sdata, mld_addr, link_id, link_addr, gfp);
816 }
817 
818 static int sta_info_insert_check(struct sta_info *sta)
819 {
820 	struct ieee80211_sub_if_data *sdata = sta->sdata;
821 	struct ieee80211_sta *same_addr_sta;
822 
823 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
824 
825 	/*
826 	 * Can't be a WARN_ON because it can be triggered through a race:
827 	 * something inserts a STA (on one CPU) without holding the RTNL
828 	 * and another CPU turns off the net device.
829 	 */
830 	if (unlikely(!ieee80211_sdata_running(sdata)))
831 		return -ENETDOWN;
832 
833 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
834 		    !is_valid_ether_addr(sta->sta.addr)))
835 		return -EINVAL;
836 
837 	if (!ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR))
838 		return 0;
839 
840 	/* The RCU read lock is required by rhashtable due to
841 	 * asynchronous resize/rehash.  We also require the mutex
842 	 * for correctness.
843 	 */
844 	rcu_read_lock();
845 	same_addr_sta = ieee80211_find_sta_by_ifaddr(&sdata->local->hw,
846 						     sta->addr, NULL);
847 	/* For NAN, a peer can re-use */
848 	if (same_addr_sta && same_addr_sta != rcu_access_pointer(sta->sta.nmi)) {
849 		rcu_read_unlock();
850 		return -ENOTUNIQ;
851 	}
852 	rcu_read_unlock();
853 
854 	return 0;
855 }
856 
857 static int sta_info_insert_drv_state(struct ieee80211_local *local,
858 				     struct ieee80211_sub_if_data *sdata,
859 				     struct sta_info *sta)
860 {
861 	enum ieee80211_sta_state state;
862 	int err = 0;
863 
864 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
865 		err = drv_sta_state(local, sdata, sta, state, state + 1);
866 		if (err)
867 			break;
868 	}
869 
870 	if (!err) {
871 		/*
872 		 * Drivers using legacy sta_add/sta_remove callbacks only
873 		 * get uploaded set to true after sta_add is called.
874 		 */
875 		if (!local->ops->sta_add)
876 			sta->uploaded = true;
877 		return 0;
878 	}
879 
880 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
881 		sdata_info(sdata,
882 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
883 			   sta->sta.addr, state + 1, err);
884 		err = 0;
885 	}
886 
887 	/* unwind on error */
888 	for (; state > IEEE80211_STA_NOTEXIST; state--)
889 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
890 
891 	return err;
892 }
893 
894 static void
895 ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata)
896 {
897 	struct ieee80211_local *local = sdata->local;
898 	bool allow_p2p_go_ps = sdata->vif.p2p;
899 	struct sta_info *sta;
900 
901 	rcu_read_lock();
902 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
903 		if (sdata != sta->sdata ||
904 		    !test_sta_flag(sta, WLAN_STA_ASSOC))
905 			continue;
906 		if (!sta->sta.support_p2p_ps) {
907 			allow_p2p_go_ps = false;
908 			break;
909 		}
910 	}
911 	rcu_read_unlock();
912 
913 	if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) {
914 		sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps;
915 		ieee80211_link_info_change_notify(sdata, &sdata->deflink,
916 						  BSS_CHANGED_P2P_PS);
917 	}
918 }
919 
920 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
921 {
922 	struct ieee80211_local *local = sta->local;
923 	struct ieee80211_sub_if_data *sdata = sta->sdata;
924 	struct station_info *sinfo = NULL;
925 	int err = 0;
926 
927 	lockdep_assert_wiphy(local->hw.wiphy);
928 
929 	/* check if STA exists already */
930 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
931 		err = -EEXIST;
932 		goto out_cleanup;
933 	}
934 
935 	sinfo = kzalloc_obj(struct station_info);
936 	if (!sinfo) {
937 		err = -ENOMEM;
938 		goto out_cleanup;
939 	}
940 
941 	local->num_sta++;
942 	local->sta_generation++;
943 	smp_mb();
944 
945 	/* simplify things and don't accept BA sessions yet */
946 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
947 
948 	/* make the station visible */
949 	err = sta_info_hash_add(local, sta);
950 	if (err)
951 		goto out_drop_sta;
952 
953 	if (sta->sta.valid_links) {
954 		err = link_sta_info_hash_add(local, &sta->deflink);
955 		if (err) {
956 			sta_info_hash_del(local, sta);
957 			goto out_drop_sta;
958 		}
959 	}
960 
961 	list_add_tail_rcu(&sta->list, &local->sta_list);
962 
963 	/* update channel context before notifying the driver about state
964 	 * change, this enables driver using the updated channel context right away.
965 	 */
966 	if (sta->sta_state >= IEEE80211_STA_ASSOC) {
967 		ieee80211_recalc_min_chandef(sta->sdata, -1);
968 		if (!sta->sta.support_p2p_ps)
969 			ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
970 	}
971 
972 	/* notify driver */
973 	err = sta_info_insert_drv_state(local, sdata, sta);
974 	if (err)
975 		goto out_remove;
976 
977 	set_sta_flag(sta, WLAN_STA_INSERTED);
978 
979 	/* accept BA sessions now */
980 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
981 
982 	ieee80211_sta_debugfs_add(sta);
983 	rate_control_add_sta_debugfs(sta);
984 	if (sta->sta.valid_links) {
985 		int i;
986 
987 		for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
988 			struct link_sta_info *link_sta;
989 
990 			link_sta = rcu_dereference_protected(sta->link[i],
991 							     lockdep_is_held(&local->hw.wiphy->mtx));
992 
993 			if (!link_sta)
994 				continue;
995 
996 			ieee80211_link_sta_debugfs_add(link_sta);
997 			if (sdata->vif.active_links & BIT(i))
998 				ieee80211_link_sta_debugfs_drv_add(link_sta);
999 		}
1000 	} else {
1001 		ieee80211_link_sta_debugfs_add(&sta->deflink);
1002 		ieee80211_link_sta_debugfs_drv_add(&sta->deflink);
1003 	}
1004 
1005 	sinfo->generation = local->sta_generation;
1006 	cfg80211_new_sta(&sdata->wdev, sta->sta.addr, sinfo, GFP_KERNEL);
1007 	kfree(sinfo);
1008 
1009 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
1010 
1011 	/* move reference to rcu-protected */
1012 	rcu_read_lock();
1013 
1014 	if (ieee80211_vif_is_mesh(&sdata->vif))
1015 		mesh_accept_plinks_update(sdata);
1016 
1017 	ieee80211_check_fast_xmit(sta);
1018 
1019 	return 0;
1020  out_remove:
1021 	if (sta->sta.valid_links)
1022 		link_sta_info_hash_del(local, &sta->deflink);
1023 	sta_info_hash_del(local, sta);
1024 	list_del_rcu(&sta->list);
1025  out_drop_sta:
1026 	local->num_sta--;
1027 	synchronize_net();
1028  out_cleanup:
1029 	cleanup_single_sta(sta);
1030 	kfree(sinfo);
1031 	rcu_read_lock();
1032 	return err;
1033 }
1034 
1035 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
1036 {
1037 	struct ieee80211_local *local = sta->local;
1038 	int err;
1039 
1040 	might_sleep();
1041 	lockdep_assert_wiphy(local->hw.wiphy);
1042 
1043 	err = sta_info_insert_check(sta);
1044 	if (err) {
1045 		sta_info_free(local, sta);
1046 		rcu_read_lock();
1047 		return err;
1048 	}
1049 
1050 	return sta_info_insert_finish(sta);
1051 }
1052 
1053 int sta_info_insert(struct sta_info *sta)
1054 {
1055 	int err = sta_info_insert_rcu(sta);
1056 
1057 	rcu_read_unlock();
1058 
1059 	return err;
1060 }
1061 
1062 static inline void __bss_tim_set(u8 *tim, u16 id)
1063 {
1064 	/*
1065 	 * This format has been mandated by the IEEE specifications,
1066 	 * so this line may not be changed to use the __set_bit() format.
1067 	 */
1068 	tim[id / 8] |= (1 << (id % 8));
1069 }
1070 
1071 static inline void __bss_tim_clear(u8 *tim, u16 id)
1072 {
1073 	/*
1074 	 * This format has been mandated by the IEEE specifications,
1075 	 * so this line may not be changed to use the __clear_bit() format.
1076 	 */
1077 	tim[id / 8] &= ~(1 << (id % 8));
1078 }
1079 
1080 static inline bool __bss_tim_get(u8 *tim, u16 id)
1081 {
1082 	/*
1083 	 * This format has been mandated by the IEEE specifications,
1084 	 * so this line may not be changed to use the test_bit() format.
1085 	 */
1086 	return tim[id / 8] & (1 << (id % 8));
1087 }
1088 
1089 static unsigned long ieee80211_tids_for_ac(int ac)
1090 {
1091 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
1092 	switch (ac) {
1093 	case IEEE80211_AC_VO:
1094 		return BIT(6) | BIT(7);
1095 	case IEEE80211_AC_VI:
1096 		return BIT(4) | BIT(5);
1097 	case IEEE80211_AC_BE:
1098 		return BIT(0) | BIT(3);
1099 	case IEEE80211_AC_BK:
1100 		return BIT(1) | BIT(2);
1101 	default:
1102 		WARN_ON(1);
1103 		return 0;
1104 	}
1105 }
1106 
1107 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
1108 {
1109 	struct ieee80211_local *local = sta->local;
1110 	struct ps_data *ps;
1111 	bool indicate_tim = false;
1112 	u8 ignore_for_tim = sta->sta.uapsd_queues;
1113 	int ac;
1114 	u16 id = sta->sta.aid;
1115 
1116 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1117 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1118 		if (WARN_ON_ONCE(!sta->sdata->bss))
1119 			return;
1120 
1121 		ps = &sta->sdata->bss->ps;
1122 #ifdef CONFIG_MAC80211_MESH
1123 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
1124 		ps = &sta->sdata->u.mesh.ps;
1125 #endif
1126 	} else {
1127 		return;
1128 	}
1129 
1130 	/* No need to do anything if the driver does all */
1131 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim)
1132 		return;
1133 
1134 	if (sta->dead)
1135 		goto done;
1136 
1137 	/*
1138 	 * If all ACs are delivery-enabled then we should build
1139 	 * the TIM bit for all ACs anyway; if only some are then
1140 	 * we ignore those and build the TIM bit using only the
1141 	 * non-enabled ones.
1142 	 */
1143 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
1144 		ignore_for_tim = 0;
1145 
1146 	if (ignore_pending)
1147 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
1148 
1149 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1150 		unsigned long tids;
1151 
1152 		if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac])
1153 			continue;
1154 
1155 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
1156 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
1157 		if (indicate_tim)
1158 			break;
1159 
1160 		tids = ieee80211_tids_for_ac(ac);
1161 
1162 		indicate_tim |=
1163 			sta->driver_buffered_tids & tids;
1164 		indicate_tim |=
1165 			sta->txq_buffered_tids & tids;
1166 	}
1167 
1168  done:
1169 	spin_lock_bh(&local->tim_lock);
1170 
1171 	if (indicate_tim == __bss_tim_get(ps->tim, id))
1172 		goto out_unlock;
1173 
1174 	if (indicate_tim)
1175 		__bss_tim_set(ps->tim, id);
1176 	else
1177 		__bss_tim_clear(ps->tim, id);
1178 
1179 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
1180 		local->tim_in_locked_section = true;
1181 		drv_set_tim(local, &sta->sta, indicate_tim);
1182 		local->tim_in_locked_section = false;
1183 	}
1184 
1185 out_unlock:
1186 	spin_unlock_bh(&local->tim_lock);
1187 }
1188 
1189 void sta_info_recalc_tim(struct sta_info *sta)
1190 {
1191 	__sta_info_recalc_tim(sta, false);
1192 }
1193 
1194 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
1195 {
1196 	struct ieee80211_tx_info *info;
1197 	int timeout;
1198 
1199 	if (!skb)
1200 		return false;
1201 
1202 	info = IEEE80211_SKB_CB(skb);
1203 
1204 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
1205 	timeout = (sta->listen_interval *
1206 		   sta->sdata->vif.bss_conf.beacon_int *
1207 		   32 / 15625) * HZ;
1208 	if (timeout < STA_TX_BUFFER_EXPIRE)
1209 		timeout = STA_TX_BUFFER_EXPIRE;
1210 	return time_after(jiffies, info->control.jiffies + timeout);
1211 }
1212 
1213 
1214 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
1215 						struct sta_info *sta, int ac)
1216 {
1217 	unsigned long flags;
1218 	struct sk_buff *skb;
1219 
1220 	/*
1221 	 * First check for frames that should expire on the filtered
1222 	 * queue. Frames here were rejected by the driver and are on
1223 	 * a separate queue to avoid reordering with normal PS-buffered
1224 	 * frames. They also aren't accounted for right now in the
1225 	 * total_ps_buffered counter.
1226 	 */
1227 	for (;;) {
1228 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1229 		skb = skb_peek(&sta->tx_filtered[ac]);
1230 		if (sta_info_buffer_expired(sta, skb))
1231 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
1232 		else
1233 			skb = NULL;
1234 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1235 
1236 		/*
1237 		 * Frames are queued in order, so if this one
1238 		 * hasn't expired yet we can stop testing. If
1239 		 * we actually reached the end of the queue we
1240 		 * also need to stop, of course.
1241 		 */
1242 		if (!skb)
1243 			break;
1244 		ieee80211_free_txskb(&local->hw, skb);
1245 	}
1246 
1247 	/*
1248 	 * Now also check the normal PS-buffered queue, this will
1249 	 * only find something if the filtered queue was emptied
1250 	 * since the filtered frames are all before the normal PS
1251 	 * buffered frames.
1252 	 */
1253 	for (;;) {
1254 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1255 		skb = skb_peek(&sta->ps_tx_buf[ac]);
1256 		if (sta_info_buffer_expired(sta, skb))
1257 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
1258 		else
1259 			skb = NULL;
1260 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1261 
1262 		/*
1263 		 * frames are queued in order, so if this one
1264 		 * hasn't expired yet (or we reached the end of
1265 		 * the queue) we can stop testing
1266 		 */
1267 		if (!skb)
1268 			break;
1269 
1270 		local->total_ps_buffered--;
1271 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
1272 		       sta->sta.addr);
1273 		ieee80211_free_txskb(&local->hw, skb);
1274 	}
1275 
1276 	/*
1277 	 * Finally, recalculate the TIM bit for this station -- it might
1278 	 * now be clear because the station was too slow to retrieve its
1279 	 * frames.
1280 	 */
1281 	sta_info_recalc_tim(sta);
1282 
1283 	/*
1284 	 * Return whether there are any frames still buffered, this is
1285 	 * used to check whether the cleanup timer still needs to run,
1286 	 * if there are no frames we don't need to rearm the timer.
1287 	 */
1288 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
1289 		 skb_queue_empty(&sta->tx_filtered[ac]));
1290 }
1291 
1292 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
1293 					     struct sta_info *sta)
1294 {
1295 	bool have_buffered = false;
1296 	int ac;
1297 
1298 	/* This is only necessary for stations on BSS/MBSS interfaces */
1299 	if (!sta->sdata->bss &&
1300 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
1301 		return false;
1302 
1303 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1304 		have_buffered |=
1305 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
1306 
1307 	return have_buffered;
1308 }
1309 
1310 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
1311 {
1312 	struct ieee80211_local *local;
1313 	struct ieee80211_sub_if_data *sdata;
1314 	int ret, i;
1315 
1316 	might_sleep();
1317 
1318 	if (!sta)
1319 		return -ENOENT;
1320 
1321 	local = sta->local;
1322 	sdata = sta->sdata;
1323 
1324 	lockdep_assert_wiphy(local->hw.wiphy);
1325 
1326 	if (sdata->vif.type == NL80211_IFTYPE_NAN) {
1327 		struct sta_info *sta_iter, *tmp;
1328 
1329 		/* Remove all NDI stations associated with this NMI STA */
1330 		list_for_each_entry_safe(sta_iter, tmp, &local->sta_list, list) {
1331 			if (rcu_access_pointer(sta_iter->sta.nmi) != &sta->sta)
1332 				continue;
1333 			sta_info_destroy_addr(sta_iter->sdata, sta_iter->addr);
1334 		}
1335 
1336 		/* Free and clear the local peer schedule */
1337 		ieee80211_nan_free_peer_sched(sta->sta.nan_sched);
1338 		sta->sta.nan_sched = NULL;
1339 	}
1340 
1341 	/*
1342 	 * Before removing the station from the driver and
1343 	 * rate control, it might still start new aggregation
1344 	 * sessions -- block that to make sure the tear-down
1345 	 * will be sufficient.
1346 	 */
1347 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
1348 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1349 
1350 	/*
1351 	 * Before removing the station from the driver there might be pending
1352 	 * rx frames on RSS queues sent prior to the disassociation - wait for
1353 	 * all such frames to be processed.
1354 	 */
1355 	drv_sync_rx_queues(local, sta);
1356 
1357 	for (i = 0; i < ARRAY_SIZE(sta->link); i++) {
1358 		struct link_sta_info *link_sta;
1359 
1360 		if (!(sta->sta.valid_links & BIT(i)))
1361 			continue;
1362 
1363 		link_sta = rcu_dereference_protected(sta->link[i],
1364 						     lockdep_is_held(&local->hw.wiphy->mtx));
1365 
1366 		link_sta_info_hash_del(local, link_sta);
1367 	}
1368 
1369 	ret = sta_info_hash_del(local, sta);
1370 	if (WARN_ON(ret))
1371 		return ret;
1372 
1373 	/*
1374 	 * for TDLS peers, make sure to return to the base channel before
1375 	 * removal.
1376 	 */
1377 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
1378 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
1379 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
1380 	}
1381 
1382 	list_del_rcu(&sta->list);
1383 	sta->removed = true;
1384 
1385 	if (sta->uploaded)
1386 		drv_sta_pre_rcu_remove(local, sta->sdata, sta);
1387 
1388 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1389 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
1390 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
1391 
1392 	return 0;
1393 }
1394 
1395 static int _sta_info_move_state(struct sta_info *sta,
1396 				enum ieee80211_sta_state new_state,
1397 				bool recalc)
1398 {
1399 	struct ieee80211_local *local = sta->local;
1400 
1401 	might_sleep();
1402 
1403 	if (sta->sta_state == new_state)
1404 		return 0;
1405 
1406 	/* check allowed transitions first */
1407 
1408 	switch (new_state) {
1409 	case IEEE80211_STA_NONE:
1410 		if (sta->sta_state != IEEE80211_STA_AUTH)
1411 			return -EINVAL;
1412 		break;
1413 	case IEEE80211_STA_AUTH:
1414 		if (sta->sta_state != IEEE80211_STA_NONE &&
1415 		    sta->sta_state != IEEE80211_STA_ASSOC)
1416 			return -EINVAL;
1417 		break;
1418 	case IEEE80211_STA_ASSOC:
1419 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1420 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1421 			return -EINVAL;
1422 		break;
1423 	case IEEE80211_STA_AUTHORIZED:
1424 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1425 			return -EINVAL;
1426 		break;
1427 	default:
1428 		WARN(1, "invalid state %d", new_state);
1429 		return -EINVAL;
1430 	}
1431 
1432 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1433 		sta->sta.addr, new_state);
1434 
1435 	/* notify the driver before the actual changes so it can
1436 	 * fail the transition if the state is increasing.
1437 	 * The driver is required not to fail when the transition
1438 	 * is decreasing the state, so first, do all the preparation
1439 	 * work and only then, notify the driver.
1440 	 */
1441 	if (new_state > sta->sta_state &&
1442 	    test_sta_flag(sta, WLAN_STA_INSERTED)) {
1443 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1444 					sta->sta_state, new_state);
1445 		if (err)
1446 			return err;
1447 	}
1448 
1449 	/* reflect the change in all state variables */
1450 
1451 	switch (new_state) {
1452 	case IEEE80211_STA_NONE:
1453 		if (sta->sta_state == IEEE80211_STA_AUTH)
1454 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1455 		break;
1456 	case IEEE80211_STA_AUTH:
1457 		if (sta->sta_state == IEEE80211_STA_NONE) {
1458 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1459 		} else if (sta->sta_state == IEEE80211_STA_ASSOC) {
1460 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1461 			if (recalc) {
1462 				ieee80211_recalc_min_chandef(sta->sdata, -1);
1463 				if (!sta->sta.support_p2p_ps)
1464 					ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1465 			}
1466 		}
1467 		break;
1468 	case IEEE80211_STA_ASSOC:
1469 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1470 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1471 			sta->assoc_at = ktime_get_boottime_ns();
1472 			if (recalc) {
1473 				ieee80211_recalc_min_chandef(sta->sdata, -1);
1474 				if (!sta->sta.support_p2p_ps)
1475 					ieee80211_recalc_p2p_go_ps_allowed(sta->sdata);
1476 			}
1477 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1478 			ieee80211_vif_dec_num_mcast(sta->sdata);
1479 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1480 			if (sta->sdata->vif.type == NL80211_IFTYPE_NAN_DATA)
1481 				ieee80211_nan_update_ndi_carrier(sta->sdata);
1482 
1483 			/*
1484 			 * If we have encryption offload, flush (station) queues
1485 			 * (after ensuring concurrent TX completed) so we won't
1486 			 * transmit anything later unencrypted if/when keys are
1487 			 * also removed, which might otherwise happen depending
1488 			 * on how the hardware offload works.
1489 			 */
1490 			if (local->ops->set_key) {
1491 				synchronize_net();
1492 				if (local->ops->flush_sta)
1493 					drv_flush_sta(local, sta->sdata, sta);
1494 				else
1495 					ieee80211_flush_queues(local,
1496 							       sta->sdata,
1497 							       false);
1498 			}
1499 
1500 			ieee80211_clear_fast_xmit(sta);
1501 			ieee80211_clear_fast_rx(sta);
1502 		}
1503 		break;
1504 	case IEEE80211_STA_AUTHORIZED:
1505 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
1506 			ieee80211_vif_inc_num_mcast(sta->sdata);
1507 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1508 			ieee80211_check_fast_xmit(sta);
1509 			ieee80211_check_fast_rx(sta);
1510 			if (sta->sdata->vif.type == NL80211_IFTYPE_NAN_DATA)
1511 				ieee80211_nan_update_ndi_carrier(sta->sdata);
1512 		}
1513 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1514 		    sta->sdata->vif.type == NL80211_IFTYPE_AP)
1515 			cfg80211_send_layer2_update(sta->sdata->dev,
1516 						    sta->sta.addr);
1517 		break;
1518 	default:
1519 		break;
1520 	}
1521 
1522 	if (new_state < sta->sta_state &&
1523 	    test_sta_flag(sta, WLAN_STA_INSERTED)) {
1524 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1525 					sta->sta_state, new_state);
1526 
1527 		WARN_ONCE(err,
1528 			  "Driver is not allowed to fail if the sta_state is transitioning down the list: %d\n",
1529 			  err);
1530 	}
1531 
1532 	sta->sta_state = new_state;
1533 
1534 	return 0;
1535 }
1536 
1537 int sta_info_move_state(struct sta_info *sta,
1538 			enum ieee80211_sta_state new_state)
1539 {
1540 	return _sta_info_move_state(sta, new_state, true);
1541 }
1542 
1543 static void __sta_info_destroy_part2(struct sta_info *sta, bool recalc)
1544 {
1545 	struct ieee80211_local *local = sta->local;
1546 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1547 	struct station_info *sinfo;
1548 	int ret;
1549 
1550 	/*
1551 	 * NOTE: This assumes at least synchronize_net() was done
1552 	 *	 after _part1 and before _part2!
1553 	 */
1554 
1555 	/*
1556 	 * There's a potential race in _part1 where we set WLAN_STA_BLOCK_BA
1557 	 * but someone might have just gotten past a check, and not yet into
1558 	 * queuing the work/creating the data/etc.
1559 	 *
1560 	 * Do another round of destruction so that the worker is certainly
1561 	 * canceled before we later free the station.
1562 	 *
1563 	 * Since this is after synchronize_rcu()/synchronize_net() we're now
1564 	 * certain that nobody can actually hold a reference to the STA and
1565 	 * be calling e.g. ieee80211_start_tx_ba_session().
1566 	 */
1567 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
1568 
1569 	might_sleep();
1570 	lockdep_assert_wiphy(local->hw.wiphy);
1571 
1572 	if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1573 		ret = _sta_info_move_state(sta, IEEE80211_STA_ASSOC, recalc);
1574 		WARN_ON_ONCE(ret);
1575 	}
1576 
1577 	/* now keys can no longer be reached */
1578 	ieee80211_free_sta_keys(local, sta);
1579 
1580 	/* disable TIM bit - last chance to tell driver */
1581 	__sta_info_recalc_tim(sta, true);
1582 
1583 	sta->dead = true;
1584 
1585 	cancel_work_sync(&sta->drv_deliver_wk);
1586 
1587 	local->num_sta--;
1588 	local->sta_generation++;
1589 
1590 	while (sta->sta_state > IEEE80211_STA_NONE) {
1591 		ret = _sta_info_move_state(sta, sta->sta_state - 1, recalc);
1592 		if (ret) {
1593 			WARN_ON_ONCE(1);
1594 			break;
1595 		}
1596 	}
1597 
1598 	sinfo = kzalloc_obj(*sinfo);
1599 	if (sinfo)
1600 		sta_set_sinfo(sta, sinfo, true);
1601 
1602 	if (sta->uploaded) {
1603 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
1604 				    IEEE80211_STA_NOTEXIST);
1605 		WARN_ON_ONCE(ret != 0);
1606 	}
1607 
1608 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
1609 
1610 	cfg80211_del_sta_sinfo(&sdata->wdev, sta->sta.addr, sinfo, GFP_KERNEL);
1611 	kfree(sinfo);
1612 
1613 	ieee80211_sta_debugfs_remove(sta);
1614 
1615 	ieee80211_destroy_frag_cache(&sta->frags);
1616 
1617 	cleanup_single_sta(sta);
1618 }
1619 
1620 int __must_check __sta_info_destroy(struct sta_info *sta)
1621 {
1622 	int err = __sta_info_destroy_part1(sta);
1623 
1624 	if (err)
1625 		return err;
1626 
1627 	synchronize_net();
1628 
1629 	__sta_info_destroy_part2(sta, true);
1630 
1631 	return 0;
1632 }
1633 
1634 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
1635 {
1636 	struct sta_info *sta;
1637 
1638 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
1639 
1640 	sta = sta_info_get(sdata, addr);
1641 	return __sta_info_destroy(sta);
1642 }
1643 
1644 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
1645 			      const u8 *addr)
1646 {
1647 	struct sta_info *sta;
1648 
1649 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
1650 
1651 	sta = sta_info_get_bss(sdata, addr);
1652 	return __sta_info_destroy(sta);
1653 }
1654 
1655 static void sta_info_cleanup(struct timer_list *t)
1656 {
1657 	struct ieee80211_local *local = timer_container_of(local, t,
1658 							   sta_cleanup);
1659 	struct sta_info *sta;
1660 	bool timer_needed = false;
1661 
1662 	rcu_read_lock();
1663 	list_for_each_entry_rcu(sta, &local->sta_list, list)
1664 		if (sta_info_cleanup_expire_buffered(local, sta))
1665 			timer_needed = true;
1666 	rcu_read_unlock();
1667 
1668 	if (local->quiescing)
1669 		return;
1670 
1671 	if (!timer_needed)
1672 		return;
1673 
1674 	mod_timer(&local->sta_cleanup,
1675 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1676 }
1677 
1678 int sta_info_init(struct ieee80211_local *local)
1679 {
1680 	int err;
1681 
1682 	err = rhltable_init(&local->sta_hash, &sta_rht_params);
1683 	if (err)
1684 		return err;
1685 
1686 	err = rhltable_init(&local->link_sta_hash, &link_sta_rht_params);
1687 	if (err) {
1688 		rhltable_destroy(&local->sta_hash);
1689 		return err;
1690 	}
1691 
1692 	spin_lock_init(&local->tim_lock);
1693 	INIT_LIST_HEAD(&local->sta_list);
1694 
1695 	timer_setup(&local->sta_cleanup, sta_info_cleanup, 0);
1696 	return 0;
1697 }
1698 
1699 void sta_info_stop(struct ieee80211_local *local)
1700 {
1701 	timer_delete_sync(&local->sta_cleanup);
1702 	rhltable_destroy(&local->sta_hash);
1703 	rhltable_destroy(&local->link_sta_hash);
1704 }
1705 
1706 
1707 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans,
1708 		     int link_id, struct sta_info *do_not_flush_sta)
1709 {
1710 	struct ieee80211_local *local = sdata->local;
1711 	struct sta_info *sta, *tmp;
1712 	LIST_HEAD(free_list);
1713 	int ret = 0;
1714 
1715 	might_sleep();
1716 	lockdep_assert_wiphy(local->hw.wiphy);
1717 
1718 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1719 	WARN_ON(vlans && !sdata->bss);
1720 
1721 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1722 		if (sdata != sta->sdata &&
1723 		    (!vlans || sdata->bss != sta->sdata->bss))
1724 			continue;
1725 
1726 		if (sta == do_not_flush_sta)
1727 			continue;
1728 
1729 		if (link_id >= 0 && sta->sta.valid_links &&
1730 		    !(sta->sta.valid_links & BIT(link_id)))
1731 			continue;
1732 
1733 		if (!WARN_ON(__sta_info_destroy_part1(sta)))
1734 			list_add(&sta->free_list, &free_list);
1735 
1736 		ret++;
1737 	}
1738 
1739 	if (!list_empty(&free_list)) {
1740 		bool support_p2p_ps = true;
1741 
1742 		synchronize_net();
1743 		list_for_each_entry_safe(sta, tmp, &free_list, free_list) {
1744 			if (!sta->sta.support_p2p_ps)
1745 				support_p2p_ps = false;
1746 			__sta_info_destroy_part2(sta, false);
1747 		}
1748 
1749 		ieee80211_recalc_min_chandef(sdata, -1);
1750 		if (!support_p2p_ps)
1751 			ieee80211_recalc_p2p_go_ps_allowed(sdata);
1752 	}
1753 
1754 	return ret;
1755 }
1756 
1757 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1758 			  unsigned long exp_time)
1759 {
1760 	struct ieee80211_local *local = sdata->local;
1761 	struct sta_info *sta, *tmp;
1762 
1763 	lockdep_assert_wiphy(local->hw.wiphy);
1764 
1765 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1766 		unsigned long last_active = ieee80211_sta_last_active(sta, -1);
1767 
1768 		if (sdata != sta->sdata)
1769 			continue;
1770 
1771 		if (time_is_before_jiffies(last_active + exp_time)) {
1772 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1773 				sta->sta.addr);
1774 
1775 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
1776 			    test_sta_flag(sta, WLAN_STA_PS_STA))
1777 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1778 
1779 			WARN_ON(__sta_info_destroy(sta));
1780 		}
1781 	}
1782 }
1783 
1784 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1785 						   const u8 *addr,
1786 						   const u8 *localaddr)
1787 {
1788 	struct ieee80211_local *local = hw_to_local(hw);
1789 	struct rhlist_head *tmp;
1790 	struct sta_info *sta;
1791 
1792 	/*
1793 	 * Just return a random station if localaddr is NULL
1794 	 * ... first in list.
1795 	 */
1796 	for_each_sta_info(local, addr, sta, tmp) {
1797 		if (localaddr &&
1798 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1799 			continue;
1800 		if (!sta->uploaded)
1801 			return NULL;
1802 		return &sta->sta;
1803 	}
1804 
1805 	return NULL;
1806 }
1807 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1808 
1809 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1810 					 const u8 *addr)
1811 {
1812 	struct sta_info *sta;
1813 
1814 	if (!vif)
1815 		return NULL;
1816 
1817 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1818 	if (!sta)
1819 		return NULL;
1820 
1821 	if (!sta->uploaded)
1822 		return NULL;
1823 
1824 	return &sta->sta;
1825 }
1826 EXPORT_SYMBOL(ieee80211_find_sta);
1827 
1828 /* powersave support code */
1829 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1830 {
1831 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1832 	struct ieee80211_local *local = sdata->local;
1833 	struct sk_buff_head pending;
1834 	int filtered = 0, buffered = 0, ac, i;
1835 	unsigned long flags;
1836 	struct ps_data *ps;
1837 
1838 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1839 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1840 				     u.ap);
1841 
1842 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1843 		ps = &sdata->bss->ps;
1844 	else if (ieee80211_vif_is_mesh(&sdata->vif))
1845 		ps = &sdata->u.mesh.ps;
1846 	else
1847 		return;
1848 
1849 	clear_sta_flag(sta, WLAN_STA_SP);
1850 
1851 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1852 	sta->driver_buffered_tids = 0;
1853 	sta->txq_buffered_tids = 0;
1854 
1855 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1856 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1857 
1858 	for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1859 		if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i]))
1860 			continue;
1861 
1862 		schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i]));
1863 	}
1864 
1865 	skb_queue_head_init(&pending);
1866 
1867 	/* sync with ieee80211_tx_h_unicast_ps_buf */
1868 	spin_lock_bh(&sta->ps_lock);
1869 	/* Send all buffered frames to the station */
1870 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1871 		int count = skb_queue_len(&pending), tmp;
1872 
1873 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1874 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1875 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1876 		tmp = skb_queue_len(&pending);
1877 		filtered += tmp - count;
1878 		count = tmp;
1879 
1880 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1881 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1882 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1883 		tmp = skb_queue_len(&pending);
1884 		buffered += tmp - count;
1885 	}
1886 
1887 	ieee80211_add_pending_skbs(local, &pending);
1888 
1889 	/* now we're no longer in the deliver code */
1890 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1891 
1892 	/* The station might have polled and then woken up before we responded,
1893 	 * so clear these flags now to avoid them sticking around.
1894 	 */
1895 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
1896 	clear_sta_flag(sta, WLAN_STA_UAPSD);
1897 	spin_unlock_bh(&sta->ps_lock);
1898 
1899 	atomic_dec(&ps->num_sta_ps);
1900 
1901 	local->total_ps_buffered -= buffered;
1902 
1903 	sta_info_recalc_tim(sta);
1904 
1905 	ps_dbg(sdata,
1906 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n",
1907 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1908 
1909 	ieee80211_check_fast_xmit(sta);
1910 }
1911 
1912 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1913 					 enum ieee80211_frame_release_type reason,
1914 					 bool call_driver, bool more_data)
1915 {
1916 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1917 	struct ieee80211_local *local = sdata->local;
1918 	struct ieee80211_qos_hdr *nullfunc;
1919 	struct sk_buff *skb;
1920 	int size = sizeof(*nullfunc);
1921 	__le16 fc;
1922 	bool qos = sta->sta.wme;
1923 	struct ieee80211_tx_info *info;
1924 	struct ieee80211_chanctx_conf *chanctx_conf;
1925 
1926 	if (qos) {
1927 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1928 				 IEEE80211_STYPE_QOS_NULLFUNC |
1929 				 IEEE80211_FCTL_FROMDS);
1930 	} else {
1931 		size -= 2;
1932 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1933 				 IEEE80211_STYPE_NULLFUNC |
1934 				 IEEE80211_FCTL_FROMDS);
1935 	}
1936 
1937 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1938 	if (!skb)
1939 		return;
1940 
1941 	skb_reserve(skb, local->hw.extra_tx_headroom);
1942 
1943 	nullfunc = skb_put(skb, size);
1944 	nullfunc->frame_control = fc;
1945 	nullfunc->duration_id = 0;
1946 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1947 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1948 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1949 	nullfunc->seq_ctrl = 0;
1950 
1951 	skb->priority = tid;
1952 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1953 	if (qos) {
1954 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1955 
1956 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1957 			nullfunc->qos_ctrl |=
1958 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1959 			if (more_data)
1960 				nullfunc->frame_control |=
1961 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1962 		}
1963 	}
1964 
1965 	info = IEEE80211_SKB_CB(skb);
1966 
1967 	/*
1968 	 * Tell TX path to send this frame even though the
1969 	 * STA may still remain is PS mode after this frame
1970 	 * exchange. Also set EOSP to indicate this packet
1971 	 * ends the poll/service period.
1972 	 */
1973 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1974 		       IEEE80211_TX_STATUS_EOSP |
1975 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1976 
1977 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1978 
1979 	if (call_driver)
1980 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1981 					  reason, false);
1982 
1983 	skb->dev = sdata->dev;
1984 
1985 	rcu_read_lock();
1986 	chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf);
1987 	if (WARN_ON(!chanctx_conf)) {
1988 		rcu_read_unlock();
1989 		kfree_skb(skb);
1990 		return;
1991 	}
1992 
1993 	info->band = chanctx_conf->def.chan->band;
1994 	ieee80211_xmit(sdata, sta, skb);
1995 	rcu_read_unlock();
1996 }
1997 
1998 static int find_highest_prio_tid(unsigned long tids)
1999 {
2000 	/* lower 3 TIDs aren't ordered perfectly */
2001 	if (tids & 0xF8)
2002 		return fls(tids) - 1;
2003 	/* TID 0 is BE just like TID 3 */
2004 	if (tids & BIT(0))
2005 		return 0;
2006 	return fls(tids) - 1;
2007 }
2008 
2009 /* Indicates if the MORE_DATA bit should be set in the last
2010  * frame obtained by ieee80211_sta_ps_get_frames.
2011  * Note that driver_release_tids is relevant only if
2012  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
2013  */
2014 static bool
2015 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
2016 			   enum ieee80211_frame_release_type reason,
2017 			   unsigned long driver_release_tids)
2018 {
2019 	int ac;
2020 
2021 	/* If the driver has data on more than one TID then
2022 	 * certainly there's more data if we release just a
2023 	 * single frame now (from a single TID). This will
2024 	 * only happen for PS-Poll.
2025 	 */
2026 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
2027 	    hweight16(driver_release_tids) > 1)
2028 		return true;
2029 
2030 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2031 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
2032 			continue;
2033 
2034 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
2035 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
2036 			return true;
2037 	}
2038 
2039 	return false;
2040 }
2041 
2042 static void
2043 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
2044 			    enum ieee80211_frame_release_type reason,
2045 			    struct sk_buff_head *frames,
2046 			    unsigned long *driver_release_tids)
2047 {
2048 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2049 	struct ieee80211_local *local = sdata->local;
2050 	int ac;
2051 
2052 	/* Get response frame(s) and more data bit for the last one. */
2053 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2054 		unsigned long tids;
2055 
2056 		if (ignored_acs & ieee80211_ac_to_qos_mask[ac])
2057 			continue;
2058 
2059 		tids = ieee80211_tids_for_ac(ac);
2060 
2061 		/* if we already have frames from software, then we can't also
2062 		 * release from hardware queues
2063 		 */
2064 		if (skb_queue_empty(frames)) {
2065 			*driver_release_tids |=
2066 				sta->driver_buffered_tids & tids;
2067 			*driver_release_tids |= sta->txq_buffered_tids & tids;
2068 		}
2069 
2070 		if (!*driver_release_tids) {
2071 			struct sk_buff *skb;
2072 
2073 			while (n_frames > 0) {
2074 				skb = skb_dequeue(&sta->tx_filtered[ac]);
2075 				if (!skb) {
2076 					skb = skb_dequeue(
2077 						&sta->ps_tx_buf[ac]);
2078 					if (skb)
2079 						local->total_ps_buffered--;
2080 				}
2081 				if (!skb)
2082 					break;
2083 				n_frames--;
2084 				__skb_queue_tail(frames, skb);
2085 			}
2086 		}
2087 
2088 		/* If we have more frames buffered on this AC, then abort the
2089 		 * loop since we can't send more data from other ACs before
2090 		 * the buffered frames from this.
2091 		 */
2092 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
2093 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
2094 			break;
2095 	}
2096 }
2097 
2098 static void
2099 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
2100 				  int n_frames, u8 ignored_acs,
2101 				  enum ieee80211_frame_release_type reason)
2102 {
2103 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2104 	struct ieee80211_local *local = sdata->local;
2105 	unsigned long driver_release_tids = 0;
2106 	struct sk_buff_head frames;
2107 	bool more_data;
2108 
2109 	/* Service or PS-Poll period starts */
2110 	set_sta_flag(sta, WLAN_STA_SP);
2111 
2112 	__skb_queue_head_init(&frames);
2113 
2114 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
2115 				    &frames, &driver_release_tids);
2116 
2117 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
2118 
2119 	if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL)
2120 		driver_release_tids =
2121 			BIT(find_highest_prio_tid(driver_release_tids));
2122 
2123 	if (skb_queue_empty(&frames) && !driver_release_tids) {
2124 		int tid, ac;
2125 
2126 		/*
2127 		 * For PS-Poll, this can only happen due to a race condition
2128 		 * when we set the TIM bit and the station notices it, but
2129 		 * before it can poll for the frame we expire it.
2130 		 *
2131 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
2132 		 *	At each unscheduled SP for a non-AP STA, the AP shall
2133 		 *	attempt to transmit at least one MSDU or MMPDU, but no
2134 		 *	more than the value specified in the Max SP Length field
2135 		 *	in the QoS Capability element from delivery-enabled ACs,
2136 		 *	that are destined for the non-AP STA.
2137 		 *
2138 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
2139 		 */
2140 
2141 		/* This will evaluate to 1, 3, 5 or 7. */
2142 		for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++)
2143 			if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac]))
2144 				break;
2145 		tid = 7 - 2 * ac;
2146 
2147 		ieee80211_send_null_response(sta, tid, reason, true, false);
2148 	} else if (!driver_release_tids) {
2149 		struct sk_buff_head pending;
2150 		struct sk_buff *skb;
2151 		int num = 0;
2152 		u16 tids = 0;
2153 		bool need_null = false;
2154 
2155 		skb_queue_head_init(&pending);
2156 
2157 		while ((skb = __skb_dequeue(&frames))) {
2158 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2159 			struct ieee80211_hdr *hdr = (void *) skb->data;
2160 			u8 *qoshdr = NULL;
2161 
2162 			num++;
2163 
2164 			/*
2165 			 * Tell TX path to send this frame even though the
2166 			 * STA may still remain is PS mode after this frame
2167 			 * exchange.
2168 			 */
2169 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
2170 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
2171 
2172 			/*
2173 			 * Use MoreData flag to indicate whether there are
2174 			 * more buffered frames for this STA
2175 			 */
2176 			if (more_data || !skb_queue_empty(&frames))
2177 				hdr->frame_control |=
2178 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2179 			else
2180 				hdr->frame_control &=
2181 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
2182 
2183 			if (ieee80211_is_data_qos(hdr->frame_control) ||
2184 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
2185 				qoshdr = ieee80211_get_qos_ctl(hdr);
2186 
2187 			tids |= BIT(skb->priority);
2188 
2189 			__skb_queue_tail(&pending, skb);
2190 
2191 			/* end service period after last frame or add one */
2192 			if (!skb_queue_empty(&frames))
2193 				continue;
2194 
2195 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
2196 				/* for PS-Poll, there's only one frame */
2197 				info->flags |= IEEE80211_TX_STATUS_EOSP |
2198 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
2199 				break;
2200 			}
2201 
2202 			/* For uAPSD, things are a bit more complicated. If the
2203 			 * last frame has a QoS header (i.e. is a QoS-data or
2204 			 * QoS-nulldata frame) then just set the EOSP bit there
2205 			 * and be done.
2206 			 * If the frame doesn't have a QoS header (which means
2207 			 * it should be a bufferable MMPDU) then we can't set
2208 			 * the EOSP bit in the QoS header; add a QoS-nulldata
2209 			 * frame to the list to send it after the MMPDU.
2210 			 *
2211 			 * Note that this code is only in the mac80211-release
2212 			 * code path, we assume that the driver will not buffer
2213 			 * anything but QoS-data frames, or if it does, will
2214 			 * create the QoS-nulldata frame by itself if needed.
2215 			 *
2216 			 * Cf. 802.11-2012 10.2.1.10 (c).
2217 			 */
2218 			if (qoshdr) {
2219 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
2220 
2221 				info->flags |= IEEE80211_TX_STATUS_EOSP |
2222 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
2223 			} else {
2224 				/* The standard isn't completely clear on this
2225 				 * as it says the more-data bit should be set
2226 				 * if there are more BUs. The QoS-Null frame
2227 				 * we're about to send isn't buffered yet, we
2228 				 * only create it below, but let's pretend it
2229 				 * was buffered just in case some clients only
2230 				 * expect more-data=0 when eosp=1.
2231 				 */
2232 				hdr->frame_control |=
2233 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2234 				need_null = true;
2235 				num++;
2236 			}
2237 			break;
2238 		}
2239 
2240 		drv_allow_buffered_frames(local, sta, tids, num,
2241 					  reason, more_data);
2242 
2243 		ieee80211_add_pending_skbs(local, &pending);
2244 
2245 		if (need_null)
2246 			ieee80211_send_null_response(
2247 				sta, find_highest_prio_tid(tids),
2248 				reason, false, false);
2249 
2250 		sta_info_recalc_tim(sta);
2251 	} else {
2252 		int tid;
2253 
2254 		/*
2255 		 * We need to release a frame that is buffered somewhere in the
2256 		 * driver ... it'll have to handle that.
2257 		 * Note that the driver also has to check the number of frames
2258 		 * on the TIDs we're releasing from - if there are more than
2259 		 * n_frames it has to set the more-data bit (if we didn't ask
2260 		 * it to set it anyway due to other buffered frames); if there
2261 		 * are fewer than n_frames it has to make sure to adjust that
2262 		 * to allow the service period to end properly.
2263 		 */
2264 		drv_release_buffered_frames(local, sta, driver_release_tids,
2265 					    n_frames, reason, more_data);
2266 
2267 		/*
2268 		 * Note that we don't recalculate the TIM bit here as it would
2269 		 * most likely have no effect at all unless the driver told us
2270 		 * that the TID(s) became empty before returning here from the
2271 		 * release function.
2272 		 * Either way, however, when the driver tells us that the TID(s)
2273 		 * became empty or we find that a txq became empty, we'll do the
2274 		 * TIM recalculation.
2275 		 */
2276 
2277 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
2278 			if (!sta->sta.txq[tid] ||
2279 			    !(driver_release_tids & BIT(tid)) ||
2280 			    txq_has_queue(sta->sta.txq[tid]))
2281 				continue;
2282 
2283 			sta_info_recalc_tim(sta);
2284 			break;
2285 		}
2286 	}
2287 }
2288 
2289 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
2290 {
2291 	u8 ignore_for_response = sta->sta.uapsd_queues;
2292 
2293 	/*
2294 	 * If all ACs are delivery-enabled then we should reply
2295 	 * from any of them, if only some are enabled we reply
2296 	 * only from the non-enabled ones.
2297 	 */
2298 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
2299 		ignore_for_response = 0;
2300 
2301 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
2302 					  IEEE80211_FRAME_RELEASE_PSPOLL);
2303 }
2304 
2305 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
2306 {
2307 	int n_frames = sta->sta.max_sp;
2308 	u8 delivery_enabled = sta->sta.uapsd_queues;
2309 
2310 	/*
2311 	 * If we ever grow support for TSPEC this might happen if
2312 	 * the TSPEC update from hostapd comes in between a trigger
2313 	 * frame setting WLAN_STA_UAPSD in the RX path and this
2314 	 * actually getting called.
2315 	 */
2316 	if (!delivery_enabled)
2317 		return;
2318 
2319 	switch (sta->sta.max_sp) {
2320 	case 1:
2321 		n_frames = 2;
2322 		break;
2323 	case 2:
2324 		n_frames = 4;
2325 		break;
2326 	case 3:
2327 		n_frames = 6;
2328 		break;
2329 	case 0:
2330 		/* XXX: what is a good value? */
2331 		n_frames = 128;
2332 		break;
2333 	}
2334 
2335 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
2336 					  IEEE80211_FRAME_RELEASE_UAPSD);
2337 }
2338 
2339 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
2340 			       struct ieee80211_sta *pubsta, bool block)
2341 {
2342 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2343 
2344 	trace_api_sta_block_awake(sta->local, pubsta, block);
2345 
2346 	if (block) {
2347 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
2348 		ieee80211_clear_fast_xmit(sta);
2349 		return;
2350 	}
2351 
2352 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
2353 		return;
2354 
2355 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
2356 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
2357 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2358 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2359 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
2360 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
2361 		/* must be asleep in this case */
2362 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2363 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
2364 	} else {
2365 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
2366 		ieee80211_check_fast_xmit(sta);
2367 	}
2368 }
2369 EXPORT_SYMBOL(ieee80211_sta_block_awake);
2370 
2371 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
2372 {
2373 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2374 	struct ieee80211_local *local = sta->local;
2375 
2376 	trace_api_eosp(local, pubsta);
2377 
2378 	clear_sta_flag(sta, WLAN_STA_SP);
2379 }
2380 EXPORT_SYMBOL(ieee80211_sta_eosp);
2381 
2382 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
2383 {
2384 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2385 	enum ieee80211_frame_release_type reason;
2386 	bool more_data;
2387 
2388 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
2389 
2390 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
2391 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
2392 					       reason, 0);
2393 
2394 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
2395 }
2396 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
2397 
2398 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
2399 				u8 tid, bool buffered)
2400 {
2401 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2402 
2403 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
2404 		return;
2405 
2406 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
2407 
2408 	if (buffered)
2409 		set_bit(tid, &sta->driver_buffered_tids);
2410 	else
2411 		clear_bit(tid, &sta->driver_buffered_tids);
2412 
2413 	sta_info_recalc_tim(sta);
2414 }
2415 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
2416 
2417 void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid,
2418 				    u32 tx_airtime, u32 rx_airtime)
2419 {
2420 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2421 	struct ieee80211_local *local = sta->sdata->local;
2422 	u8 ac = ieee80211_ac_from_tid(tid);
2423 	u32 airtime = 0;
2424 
2425 	if (sta->local->airtime_flags & AIRTIME_USE_TX)
2426 		airtime += tx_airtime;
2427 	if (sta->local->airtime_flags & AIRTIME_USE_RX)
2428 		airtime += rx_airtime;
2429 
2430 	spin_lock_bh(&local->active_txq_lock[ac]);
2431 	sta->airtime[ac].tx_airtime += tx_airtime;
2432 	sta->airtime[ac].rx_airtime += rx_airtime;
2433 
2434 	if (ieee80211_sta_keep_active(sta, ac))
2435 		sta->airtime[ac].deficit -= airtime;
2436 
2437 	spin_unlock_bh(&local->active_txq_lock[ac]);
2438 }
2439 EXPORT_SYMBOL(ieee80211_sta_register_airtime);
2440 
2441 void __ieee80211_sta_recalc_aggregates(struct sta_info *sta, u16 active_links)
2442 {
2443 	bool first = true;
2444 	int link_id;
2445 
2446 	if (!sta->sta.valid_links || !sta->sta.mlo) {
2447 		sta->sta.cur = &sta->sta.deflink.agg;
2448 		return;
2449 	}
2450 
2451 	rcu_read_lock();
2452 	for (link_id = 0; link_id < ARRAY_SIZE((sta)->link); link_id++) {
2453 		struct ieee80211_link_sta *link_sta;
2454 		int i;
2455 
2456 		if (!(active_links & BIT(link_id)))
2457 			continue;
2458 
2459 		link_sta = rcu_dereference(sta->sta.link[link_id]);
2460 		if (!link_sta)
2461 			continue;
2462 
2463 		if (first) {
2464 			sta->cur = sta->sta.deflink.agg;
2465 			first = false;
2466 			continue;
2467 		}
2468 
2469 		sta->cur.max_amsdu_len =
2470 			min(sta->cur.max_amsdu_len,
2471 			    link_sta->agg.max_amsdu_len);
2472 		sta->cur.max_rc_amsdu_len =
2473 			min(sta->cur.max_rc_amsdu_len,
2474 			    link_sta->agg.max_rc_amsdu_len);
2475 
2476 		for (i = 0; i < ARRAY_SIZE(sta->cur.max_tid_amsdu_len); i++)
2477 			sta->cur.max_tid_amsdu_len[i] =
2478 				min(sta->cur.max_tid_amsdu_len[i],
2479 				    link_sta->agg.max_tid_amsdu_len[i]);
2480 	}
2481 	rcu_read_unlock();
2482 
2483 	sta->sta.cur = &sta->cur;
2484 }
2485 
2486 void ieee80211_sta_recalc_aggregates(struct ieee80211_sta *pubsta)
2487 {
2488 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
2489 
2490 	__ieee80211_sta_recalc_aggregates(sta, sta->sdata->vif.active_links);
2491 }
2492 EXPORT_SYMBOL(ieee80211_sta_recalc_aggregates);
2493 
2494 void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local,
2495 					  struct sta_info *sta, u8 ac,
2496 					  u16 tx_airtime, bool tx_completed,
2497 					  bool mcast)
2498 {
2499 	int tx_pending;
2500 
2501 	if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
2502 		return;
2503 
2504 	if (mcast) {
2505 		if (!tx_completed) {
2506 			atomic_add(tx_airtime, &local->aql_mc_pending_airtime);
2507 			return;
2508 		}
2509 
2510 		tx_pending = atomic_sub_return(tx_airtime,
2511 					       &local->aql_mc_pending_airtime);
2512 		if (tx_pending < 0)
2513 			atomic_cmpxchg(&local->aql_mc_pending_airtime,
2514 				       tx_pending, 0);
2515 		return;
2516 	}
2517 
2518 	if (!tx_completed) {
2519 		if (sta)
2520 			atomic_add(tx_airtime,
2521 				   &sta->airtime[ac].aql_tx_pending);
2522 
2523 		atomic_add(tx_airtime, &local->aql_total_pending_airtime);
2524 		atomic_add(tx_airtime, &local->aql_ac_pending_airtime[ac]);
2525 		return;
2526 	}
2527 
2528 	if (sta) {
2529 		tx_pending = atomic_sub_return(tx_airtime,
2530 					       &sta->airtime[ac].aql_tx_pending);
2531 		if (tx_pending < 0)
2532 			atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending,
2533 				       tx_pending, 0);
2534 	}
2535 
2536 	atomic_sub(tx_airtime, &local->aql_total_pending_airtime);
2537 	tx_pending = atomic_sub_return(tx_airtime,
2538 				       &local->aql_ac_pending_airtime[ac]);
2539 	if (WARN_ONCE(tx_pending < 0,
2540 		      "Device %s AC %d pending airtime underflow: %u, %u",
2541 		      wiphy_name(local->hw.wiphy), ac, tx_pending,
2542 		      tx_airtime)) {
2543 		atomic_cmpxchg(&local->aql_ac_pending_airtime[ac],
2544 			       tx_pending, 0);
2545 		atomic_sub(tx_pending, &local->aql_total_pending_airtime);
2546 	}
2547 }
2548 
2549 static struct ieee80211_sta_rx_stats *
2550 sta_get_last_rx_stats(struct sta_info *sta, int link_id)
2551 {
2552 	struct ieee80211_sta_rx_stats *stats;
2553 	struct link_sta_info *link_sta_info;
2554 	int cpu;
2555 
2556 	if (link_id < 0)
2557 		link_sta_info = &sta->deflink;
2558 	else
2559 		link_sta_info = wiphy_dereference(sta->local->hw.wiphy,
2560 						  sta->link[link_id]);
2561 
2562 	stats = &link_sta_info->rx_stats;
2563 
2564 	if (!link_sta_info->pcpu_rx_stats)
2565 		return stats;
2566 
2567 	for_each_possible_cpu(cpu) {
2568 		struct ieee80211_sta_rx_stats *cpustats;
2569 
2570 		cpustats = per_cpu_ptr(link_sta_info->pcpu_rx_stats, cpu);
2571 
2572 		if (time_after(cpustats->last_rx, stats->last_rx))
2573 			stats = cpustats;
2574 	}
2575 
2576 	return stats;
2577 }
2578 
2579 static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate,
2580 				  struct rate_info *rinfo)
2581 {
2582 	rinfo->bw = STA_STATS_GET(BW, rate);
2583 
2584 	switch (STA_STATS_GET(TYPE, rate)) {
2585 	case STA_STATS_RATE_TYPE_VHT:
2586 		rinfo->flags = RATE_INFO_FLAGS_VHT_MCS;
2587 		rinfo->mcs = STA_STATS_GET(VHT_MCS, rate);
2588 		rinfo->nss = STA_STATS_GET(VHT_NSS, rate);
2589 		if (STA_STATS_GET(SGI, rate))
2590 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2591 		break;
2592 	case STA_STATS_RATE_TYPE_HT:
2593 		rinfo->flags = RATE_INFO_FLAGS_MCS;
2594 		rinfo->mcs = STA_STATS_GET(HT_MCS, rate);
2595 		if (STA_STATS_GET(SGI, rate))
2596 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2597 		break;
2598 	case STA_STATS_RATE_TYPE_LEGACY: {
2599 		struct ieee80211_supported_band *sband;
2600 		u16 brate;
2601 		unsigned int shift;
2602 		int band = STA_STATS_GET(LEGACY_BAND, rate);
2603 		int rate_idx = STA_STATS_GET(LEGACY_IDX, rate);
2604 
2605 		sband = local->hw.wiphy->bands[band];
2606 
2607 		if (WARN_ON_ONCE(!sband->bitrates))
2608 			break;
2609 
2610 		brate = sband->bitrates[rate_idx].bitrate;
2611 		if (rinfo->bw == RATE_INFO_BW_5)
2612 			shift = 2;
2613 		else if (rinfo->bw == RATE_INFO_BW_10)
2614 			shift = 1;
2615 		else
2616 			shift = 0;
2617 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
2618 		break;
2619 		}
2620 	case STA_STATS_RATE_TYPE_HE:
2621 		rinfo->flags = RATE_INFO_FLAGS_HE_MCS;
2622 		rinfo->mcs = STA_STATS_GET(HE_MCS, rate);
2623 		rinfo->nss = STA_STATS_GET(HE_NSS, rate);
2624 		rinfo->he_gi = STA_STATS_GET(HE_GI, rate);
2625 		rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate);
2626 		rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate);
2627 		break;
2628 	case STA_STATS_RATE_TYPE_EHT:
2629 		rinfo->flags = RATE_INFO_FLAGS_EHT_MCS;
2630 		rinfo->mcs = STA_STATS_GET(EHT_MCS, rate);
2631 		rinfo->nss = STA_STATS_GET(EHT_NSS, rate);
2632 		rinfo->eht_gi = STA_STATS_GET(EHT_GI, rate);
2633 		rinfo->eht_ru_alloc = STA_STATS_GET(EHT_RU, rate);
2634 		break;
2635 	case STA_STATS_RATE_TYPE_UHR:
2636 		rinfo->flags = RATE_INFO_FLAGS_UHR_MCS;
2637 		rinfo->mcs = STA_STATS_GET(UHR_MCS, rate);
2638 		rinfo->nss = STA_STATS_GET(UHR_NSS, rate);
2639 		rinfo->eht_gi = STA_STATS_GET(UHR_GI, rate);
2640 		rinfo->eht_ru_alloc = STA_STATS_GET(UHR_RU, rate);
2641 		if (STA_STATS_GET(UHR_ELR, rate))
2642 			rinfo->flags |= RATE_INFO_FLAGS_UHR_ELR_MCS;
2643 		if (STA_STATS_GET(UHR_IM, rate))
2644 			rinfo->flags |= RATE_INFO_FLAGS_UHR_IM;
2645 		break;
2646 	case STA_STATS_RATE_TYPE_S1G:
2647 		rinfo->flags = RATE_INFO_FLAGS_S1G_MCS;
2648 		rinfo->mcs = STA_STATS_GET(S1G_MCS, rate);
2649 		rinfo->nss = STA_STATS_GET(S1G_NSS, rate);
2650 		if (STA_STATS_GET(SGI, rate))
2651 			rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
2652 		break;
2653 	}
2654 }
2655 
2656 static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo,
2657 				int link_id)
2658 {
2659 	u32 rate = READ_ONCE(sta_get_last_rx_stats(sta, link_id)->last_rate);
2660 
2661 	if (rate == STA_STATS_RATE_INVALID)
2662 		return -EINVAL;
2663 
2664 	sta_stats_decode_rate(sta->local, rate, rinfo);
2665 	return 0;
2666 }
2667 
2668 static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats,
2669 					int tid)
2670 {
2671 	unsigned int start;
2672 	u64 value;
2673 
2674 	do {
2675 		start = u64_stats_fetch_begin(&rxstats->syncp);
2676 		value = u64_stats_read(&rxstats->msdu[tid]);
2677 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2678 
2679 	return value;
2680 }
2681 
2682 static void sta_set_tidstats(struct sta_info *sta,
2683 			     struct cfg80211_tid_stats *tidstats,
2684 			     int tid, int link_id)
2685 {
2686 	struct ieee80211_local *local = sta->local;
2687 	struct link_sta_info *link_sta_info;
2688 	int cpu;
2689 
2690 	if (link_id < 0)
2691 		link_sta_info = &sta->deflink;
2692 	else
2693 		link_sta_info = wiphy_dereference(sta->local->hw.wiphy,
2694 						  sta->link[link_id]);
2695 
2696 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2697 		tidstats->rx_msdu +=
2698 			sta_get_tidstats_msdu(&link_sta_info->rx_stats,
2699 					      tid);
2700 
2701 		if (link_sta_info->pcpu_rx_stats) {
2702 			for_each_possible_cpu(cpu) {
2703 				struct ieee80211_sta_rx_stats *cpurxs;
2704 
2705 				cpurxs = per_cpu_ptr(link_sta_info->pcpu_rx_stats,
2706 						     cpu);
2707 				tidstats->rx_msdu +=
2708 					sta_get_tidstats_msdu(cpurxs, tid);
2709 			}
2710 		}
2711 
2712 		tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2713 	}
2714 
2715 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2716 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2717 		tidstats->tx_msdu = link_sta_info->tx_stats.msdu[tid];
2718 	}
2719 
2720 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2721 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2722 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2723 		tidstats->tx_msdu_retries =
2724 			link_sta_info->status_stats.msdu_retries[tid];
2725 	}
2726 
2727 	if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2728 	    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2729 		tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2730 		tidstats->tx_msdu_failed =
2731 			link_sta_info->status_stats.msdu_failed[tid];
2732 	}
2733 
2734 	if (link_id < 0 && tid < IEEE80211_NUM_TIDS) {
2735 		spin_lock_bh(&local->fq.lock);
2736 
2737 		tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS);
2738 		ieee80211_fill_txq_stats(&tidstats->txq_stats,
2739 					 to_txq_info(sta->sta.txq[tid]));
2740 
2741 		spin_unlock_bh(&local->fq.lock);
2742 	}
2743 }
2744 
2745 static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats)
2746 {
2747 	unsigned int start;
2748 	u64 value;
2749 
2750 	do {
2751 		start = u64_stats_fetch_begin(&rxstats->syncp);
2752 		value = u64_stats_read(&rxstats->bytes);
2753 	} while (u64_stats_fetch_retry(&rxstats->syncp, start));
2754 
2755 	return value;
2756 }
2757 
2758 #ifdef CONFIG_MAC80211_MESH
2759 static void sta_set_mesh_sinfo(struct sta_info *sta,
2760 			       struct station_info *sinfo)
2761 {
2762 	struct ieee80211_local *local = sta->sdata->local;
2763 
2764 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) |
2765 			 BIT_ULL(NL80211_STA_INFO_PLID) |
2766 			 BIT_ULL(NL80211_STA_INFO_PLINK_STATE) |
2767 			 BIT_ULL(NL80211_STA_INFO_LOCAL_PM) |
2768 			 BIT_ULL(NL80211_STA_INFO_PEER_PM) |
2769 			 BIT_ULL(NL80211_STA_INFO_NONPEER_PM) |
2770 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) |
2771 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS);
2772 
2773 	sinfo->llid = sta->mesh->llid;
2774 	sinfo->plid = sta->mesh->plid;
2775 	sinfo->plink_state = sta->mesh->plink_state;
2776 	if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2777 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET);
2778 		sinfo->t_offset = sta->mesh->t_offset;
2779 	}
2780 	sinfo->local_pm = sta->mesh->local_pm;
2781 	sinfo->peer_pm = sta->mesh->peer_pm;
2782 	sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2783 	sinfo->connected_to_gate = sta->mesh->connected_to_gate;
2784 	sinfo->connected_to_as = sta->mesh->connected_to_as;
2785 
2786 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC);
2787 	sinfo->airtime_link_metric = airtime_link_metric_get(local, sta);
2788 }
2789 #endif
2790 
2791 void sta_set_accumulated_removed_links_sinfo(struct sta_info *sta,
2792 					     struct station_info *sinfo)
2793 {
2794 	/* Accumulating the removed link statistics. */
2795 	sinfo->tx_packets = sta->rem_link_stats.tx_packets;
2796 	sinfo->rx_packets = sta->rem_link_stats.rx_packets;
2797 	sinfo->tx_bytes = sta->rem_link_stats.tx_bytes;
2798 	sinfo->rx_bytes = sta->rem_link_stats.rx_bytes;
2799 	sinfo->tx_retries = sta->rem_link_stats.tx_retries;
2800 	sinfo->tx_failed = sta->rem_link_stats.tx_failed;
2801 	sinfo->rx_dropped_misc = sta->rem_link_stats.rx_dropped_misc;
2802 	sinfo->beacon_loss_count = sta->rem_link_stats.beacon_loss_count;
2803 	sinfo->expected_throughput = sta->rem_link_stats.expected_throughput;
2804 
2805 	if (sinfo->pertid) {
2806 		sinfo->pertid->rx_msdu =
2807 			sta->rem_link_stats.pertid_stats.rx_msdu;
2808 		sinfo->pertid->tx_msdu =
2809 			sta->rem_link_stats.pertid_stats.tx_msdu;
2810 		sinfo->pertid->tx_msdu_retries =
2811 			sta->rem_link_stats.pertid_stats.tx_msdu_retries;
2812 		sinfo->pertid->tx_msdu_failed =
2813 			sta->rem_link_stats.pertid_stats.tx_msdu_failed;
2814 	}
2815 }
2816 
2817 static u32 sta_estimate_expected_throughput(struct sta_info *sta,
2818 					    struct rate_info *ri,
2819 					    struct ieee80211_bss_conf *bss_conf)
2820 {
2821 	struct ieee80211_hw *hw = &sta->sdata->local->hw;
2822 	struct ieee80211_chanctx_conf *conf;
2823 	u32 duration;
2824 	u8 band;
2825 
2826 	conf = sdata_dereference(bss_conf->chanctx_conf, sta->sdata);
2827 	if (!conf)
2828 		return 0;
2829 	band = conf->def.chan->band;
2830 
2831 	duration = ieee80211_rate_expected_tx_airtime(hw, NULL, ri, band, true, 1024);
2832 	duration += duration >> 4; /* add assumed packet error rate of ~6% */
2833 	if (!duration)
2834 		return 0;
2835 
2836 	return ((1024 * USEC_PER_SEC) / duration) * 8;
2837 }
2838 
2839 static void sta_set_link_sinfo(struct sta_info *sta,
2840 			       struct link_station_info *link_sinfo,
2841 			       struct ieee80211_link_data *link,
2842 			       bool tidstats)
2843 {
2844 	struct ieee80211_sub_if_data *sdata = sta->sdata;
2845 	struct ieee80211_sta_rx_stats *last_rxstats;
2846 	int i, ac, cpu, link_id = link->link_id;
2847 	struct link_sta_info *link_sta_info;
2848 	u32 thr = 0;
2849 
2850 	last_rxstats = sta_get_last_rx_stats(sta, link_id);
2851 
2852 	link_sta_info = wiphy_dereference(sta->local->hw.wiphy,
2853 					  sta->link[link_id]);
2854 
2855 	/* do before driver, so beacon filtering drivers have a
2856 	 * chance to e.g. just add the number of filtered beacons
2857 	 * (or just modify the value entirely, of course)
2858 	 */
2859 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
2860 		link_sinfo->rx_beacon = link->u.mgd.count_beacon_signal;
2861 
2862 	ether_addr_copy(link_sinfo->addr, link_sta_info->addr);
2863 
2864 	drv_link_sta_statistics(sta->local, sdata,
2865 				link_sta_info->pub,
2866 				link_sinfo);
2867 
2868 	link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
2869 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
2870 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
2871 
2872 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
2873 		link_sinfo->beacon_loss_count =
2874 			link->u.mgd.beacon_loss_count;
2875 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
2876 	}
2877 
2878 	link_sinfo->inactive_time =
2879 		jiffies_delta_to_msecs(jiffies -
2880 				       ieee80211_sta_last_active(sta,
2881 								 link_id));
2882 
2883 	if (!(link_sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
2884 				    BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
2885 		link_sinfo->tx_bytes = 0;
2886 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2887 			link_sinfo->tx_bytes +=
2888 				link_sta_info->tx_stats.bytes[ac];
2889 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
2890 	}
2891 
2892 	if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
2893 		link_sinfo->tx_packets = 0;
2894 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2895 			link_sinfo->tx_packets +=
2896 				link_sta_info->tx_stats.packets[ac];
2897 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
2898 	}
2899 
2900 	if (!(link_sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
2901 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
2902 		link_sinfo->rx_bytes +=
2903 			sta_get_stats_bytes(&link_sta_info->rx_stats);
2904 
2905 		if (link_sta_info->pcpu_rx_stats) {
2906 			for_each_possible_cpu(cpu) {
2907 				struct ieee80211_sta_rx_stats *cpurxs;
2908 
2909 				cpurxs = per_cpu_ptr(link_sta_info->pcpu_rx_stats,
2910 						     cpu);
2911 				link_sinfo->rx_bytes +=
2912 					sta_get_stats_bytes(cpurxs);
2913 			}
2914 		}
2915 
2916 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
2917 	}
2918 
2919 	if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
2920 		link_sinfo->rx_packets = link_sta_info->rx_stats.packets;
2921 		if (link_sta_info->pcpu_rx_stats) {
2922 			for_each_possible_cpu(cpu) {
2923 				struct ieee80211_sta_rx_stats *cpurxs;
2924 
2925 				cpurxs = per_cpu_ptr(link_sta_info->pcpu_rx_stats,
2926 						     cpu);
2927 				link_sinfo->rx_packets += cpurxs->packets;
2928 			}
2929 		}
2930 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
2931 	}
2932 
2933 	if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
2934 		link_sinfo->tx_retries =
2935 			link_sta_info->status_stats.retry_count;
2936 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
2937 	}
2938 
2939 	if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
2940 		link_sinfo->tx_failed =
2941 			link_sta_info->status_stats.retry_failed;
2942 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
2943 	}
2944 
2945 	if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
2946 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2947 			link_sinfo->rx_duration += sta->airtime[ac].rx_airtime;
2948 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
2949 	}
2950 
2951 	if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
2952 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2953 			link_sinfo->tx_duration += sta->airtime[ac].tx_airtime;
2954 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
2955 	}
2956 
2957 	if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
2958 		link_sinfo->airtime_weight = sta->airtime_weight;
2959 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
2960 	}
2961 
2962 	link_sinfo->rx_dropped_misc = link_sta_info->rx_stats.dropped;
2963 	if (link_sta_info->pcpu_rx_stats) {
2964 		for_each_possible_cpu(cpu) {
2965 			struct ieee80211_sta_rx_stats *cpurxs;
2966 
2967 			cpurxs = per_cpu_ptr(link_sta_info->pcpu_rx_stats,
2968 					     cpu);
2969 			link_sinfo->rx_dropped_misc += cpurxs->dropped;
2970 		}
2971 	}
2972 
2973 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
2974 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
2975 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
2976 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
2977 		link_sinfo->rx_beacon_signal_avg =
2978 			ieee80211_ave_rssi(&sdata->vif, -1);
2979 	}
2980 
2981 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2982 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2983 		if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
2984 			link_sinfo->signal = (s8)last_rxstats->last_signal;
2985 			link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2986 		}
2987 
2988 		if (!link_sta_info->pcpu_rx_stats &&
2989 		    !(link_sinfo->filled &
2990 		       BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
2991 			link_sinfo->signal_avg =
2992 				-ewma_signal_read(&link_sta_info->rx_stats_avg.signal);
2993 			link_sinfo->filled |=
2994 				BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
2995 		}
2996 	}
2997 
2998 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
2999 	 * the sta->rx_stats struct, so the check here is fine with and without
3000 	 * pcpu statistics
3001 	 */
3002 	if (last_rxstats->chains &&
3003 	    !(link_sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
3004 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
3005 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
3006 		if (!link_sta_info->pcpu_rx_stats)
3007 			link_sinfo->filled |=
3008 				BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
3009 
3010 		link_sinfo->chains = last_rxstats->chains;
3011 
3012 		for (i = 0; i < ARRAY_SIZE(link_sinfo->chain_signal); i++) {
3013 			link_sinfo->chain_signal[i] =
3014 				last_rxstats->chain_signal_last[i];
3015 			link_sinfo->chain_signal_avg[i] =
3016 				-ewma_signal_read(
3017 					&link_sta_info->rx_stats_avg.chain_signal[i]);
3018 		}
3019 	}
3020 
3021 	if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
3022 	    ieee80211_rate_valid(&link_sta_info->tx_stats.last_rate)) {
3023 		sta_set_rate_info_tx(sta, &link_sta_info->tx_stats.last_rate,
3024 				     &link_sinfo->txrate);
3025 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
3026 	}
3027 
3028 	if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) {
3029 		if (sta_set_rate_info_rx(sta, &link_sinfo->rxrate,
3030 					 link_id) == 0)
3031 			link_sinfo->filled |=
3032 				BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
3033 	}
3034 
3035 	if (tidstats && !cfg80211_link_sinfo_alloc_tid_stats(link_sinfo,
3036 							     GFP_KERNEL)) {
3037 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
3038 			sta_set_tidstats(sta, &link_sinfo->pertid[i], i,
3039 					 link_id);
3040 	}
3041 
3042 	link_sinfo->bss_param.flags = 0;
3043 	if (sdata->vif.bss_conf.use_cts_prot)
3044 		link_sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
3045 	if (sdata->vif.bss_conf.use_short_preamble)
3046 		link_sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
3047 	if (sdata->vif.bss_conf.use_short_slot)
3048 		link_sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
3049 	link_sinfo->bss_param.dtim_period = link->conf->dtim_period;
3050 	link_sinfo->bss_param.beacon_interval = link->conf->beacon_int;
3051 
3052 	thr = sta_get_expected_throughput(sta);
3053 	if (!thr && (link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)))
3054 		thr = sta_estimate_expected_throughput(sta,
3055 						      &link_sinfo->txrate,
3056 						      link->conf);
3057 
3058 	if (thr != 0) {
3059 		link_sinfo->filled |=
3060 			BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
3061 		link_sinfo->expected_throughput = thr;
3062 	}
3063 
3064 	if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
3065 	    link_sta_info->status_stats.ack_signal_filled) {
3066 		link_sinfo->ack_signal =
3067 			link_sta_info->status_stats.last_ack_signal;
3068 		link_sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
3069 	}
3070 
3071 	if (!(link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
3072 	    link_sta_info->status_stats.ack_signal_filled) {
3073 		link_sinfo->avg_ack_signal =
3074 			-(s8)ewma_avg_signal_read(
3075 				&link_sta_info->status_stats.avg_ack_signal);
3076 		link_sinfo->filled |=
3077 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
3078 	}
3079 }
3080 
3081 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo,
3082 		   bool tidstats)
3083 {
3084 	struct ieee80211_sub_if_data *sdata = sta->sdata;
3085 	struct ieee80211_local *local = sdata->local;
3086 	u32 thr = 0;
3087 	int i, ac, cpu;
3088 	struct ieee80211_sta_rx_stats *last_rxstats;
3089 
3090 	last_rxstats = sta_get_last_rx_stats(sta, -1);
3091 
3092 	sinfo->generation = sdata->local->sta_generation;
3093 
3094 	/* do before driver, so beacon filtering drivers have a
3095 	 * chance to e.g. just add the number of filtered beacons
3096 	 * (or just modify the value entirely, of course)
3097 	 */
3098 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
3099 		sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal;
3100 
3101 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
3102 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) |
3103 			 BIT_ULL(NL80211_STA_INFO_STA_FLAGS) |
3104 			 BIT_ULL(NL80211_STA_INFO_BSS_PARAM) |
3105 			 BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) |
3106 			 BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) |
3107 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC);
3108 
3109 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
3110 		sinfo->beacon_loss_count =
3111 			sdata->deflink.u.mgd.beacon_loss_count;
3112 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS);
3113 	}
3114 
3115 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
3116 	sinfo->assoc_at = sta->assoc_at;
3117 	sinfo->inactive_time =
3118 		jiffies_delta_to_msecs(jiffies -
3119 				       ieee80211_sta_last_active(sta, -1));
3120 
3121 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) |
3122 			       BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) {
3123 		sinfo->tx_bytes = 0;
3124 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
3125 			sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac];
3126 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64);
3127 	}
3128 
3129 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) {
3130 		sinfo->tx_packets = 0;
3131 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
3132 			sinfo->tx_packets += sta->deflink.tx_stats.packets[ac];
3133 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
3134 	}
3135 
3136 	if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) |
3137 			       BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) {
3138 		sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats);
3139 
3140 		if (sta->deflink.pcpu_rx_stats) {
3141 			for_each_possible_cpu(cpu) {
3142 				struct ieee80211_sta_rx_stats *cpurxs;
3143 
3144 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
3145 						     cpu);
3146 				sinfo->rx_bytes += sta_get_stats_bytes(cpurxs);
3147 			}
3148 		}
3149 
3150 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64);
3151 	}
3152 
3153 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) {
3154 		sinfo->rx_packets = sta->deflink.rx_stats.packets;
3155 		if (sta->deflink.pcpu_rx_stats) {
3156 			for_each_possible_cpu(cpu) {
3157 				struct ieee80211_sta_rx_stats *cpurxs;
3158 
3159 				cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats,
3160 						     cpu);
3161 				sinfo->rx_packets += cpurxs->packets;
3162 			}
3163 		}
3164 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
3165 	}
3166 
3167 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) {
3168 		sinfo->tx_retries = sta->deflink.status_stats.retry_count;
3169 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
3170 	}
3171 
3172 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) {
3173 		sinfo->tx_failed = sta->deflink.status_stats.retry_failed;
3174 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
3175 	}
3176 
3177 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) {
3178 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
3179 			sinfo->rx_duration += sta->airtime[ac].rx_airtime;
3180 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
3181 	}
3182 
3183 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) {
3184 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
3185 			sinfo->tx_duration += sta->airtime[ac].tx_airtime;
3186 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
3187 	}
3188 
3189 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) {
3190 		sinfo->airtime_weight = sta->airtime_weight;
3191 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT);
3192 	}
3193 
3194 	sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped;
3195 	if (sta->deflink.pcpu_rx_stats) {
3196 		for_each_possible_cpu(cpu) {
3197 			struct ieee80211_sta_rx_stats *cpurxs;
3198 
3199 			cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu);
3200 			sinfo->rx_dropped_misc += cpurxs->dropped;
3201 		}
3202 	}
3203 
3204 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
3205 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
3206 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) |
3207 				 BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
3208 		sinfo->rx_beacon_signal_avg =
3209 			ieee80211_ave_rssi(&sdata->vif, -1);
3210 	}
3211 
3212 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
3213 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
3214 		if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) {
3215 			sinfo->signal = (s8)last_rxstats->last_signal;
3216 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
3217 		}
3218 
3219 		if (!sta->deflink.pcpu_rx_stats &&
3220 		    !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) {
3221 			sinfo->signal_avg =
3222 				-ewma_signal_read(&sta->deflink.rx_stats_avg.signal);
3223 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
3224 		}
3225 	}
3226 
3227 	/* for the average - if pcpu_rx_stats isn't set - rxstats must point to
3228 	 * the sta->rx_stats struct, so the check here is fine with and without
3229 	 * pcpu statistics
3230 	 */
3231 	if (last_rxstats->chains &&
3232 	    !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) |
3233 			       BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
3234 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
3235 		if (!sta->deflink.pcpu_rx_stats)
3236 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
3237 
3238 		sinfo->chains = last_rxstats->chains;
3239 
3240 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
3241 			sinfo->chain_signal[i] =
3242 				last_rxstats->chain_signal_last[i];
3243 			sinfo->chain_signal_avg[i] =
3244 				-ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]);
3245 		}
3246 	}
3247 
3248 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) &&
3249 	    !sta->sta.valid_links &&
3250 	    ieee80211_rate_valid(&sta->deflink.tx_stats.last_rate)) {
3251 		sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate,
3252 				     &sinfo->txrate);
3253 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
3254 	}
3255 
3256 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) &&
3257 	    !sta->sta.valid_links) {
3258 		if (sta_set_rate_info_rx(sta, &sinfo->rxrate, -1) == 0)
3259 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
3260 	}
3261 
3262 	if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) {
3263 		for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++)
3264 			sta_set_tidstats(sta, &sinfo->pertid[i], i, -1);
3265 	}
3266 
3267 #ifdef CONFIG_MAC80211_MESH
3268 	if (ieee80211_vif_is_mesh(&sdata->vif))
3269 		sta_set_mesh_sinfo(sta, sinfo);
3270 #endif
3271 
3272 	sinfo->bss_param.flags = 0;
3273 	if (sdata->vif.bss_conf.use_cts_prot)
3274 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
3275 	if (sdata->vif.bss_conf.use_short_preamble)
3276 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
3277 	if (sdata->vif.bss_conf.use_short_slot)
3278 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
3279 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
3280 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
3281 
3282 	sinfo->sta_flags.set = 0;
3283 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3284 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3285 				BIT(NL80211_STA_FLAG_WME) |
3286 				BIT(NL80211_STA_FLAG_MFP) |
3287 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3288 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
3289 				BIT(NL80211_STA_FLAG_TDLS_PEER);
3290 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
3291 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
3292 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
3293 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
3294 	if (sta->sta.wme)
3295 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
3296 	if (test_sta_flag(sta, WLAN_STA_MFP))
3297 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
3298 	if (test_sta_flag(sta, WLAN_STA_AUTH))
3299 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
3300 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
3301 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
3302 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
3303 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
3304 
3305 	thr = sta_get_expected_throughput(sta);
3306 
3307 	if (thr != 0) {
3308 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
3309 		sinfo->expected_throughput = thr;
3310 	} else if (!sta->sta.valid_links &&
3311 		   (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
3312 		thr = sta_estimate_expected_throughput(sta, &sinfo->txrate,
3313 						      &sdata->vif.bss_conf);
3314 		if (thr) {
3315 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
3316 			sinfo->expected_throughput = thr;
3317 		}
3318 	}
3319 
3320 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) &&
3321 	    sta->deflink.status_stats.ack_signal_filled) {
3322 		sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal;
3323 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL);
3324 	}
3325 
3326 	if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) &&
3327 	    sta->deflink.status_stats.ack_signal_filled) {
3328 		sinfo->avg_ack_signal =
3329 			-(s8)ewma_avg_signal_read(
3330 				&sta->deflink.status_stats.avg_ack_signal);
3331 		sinfo->filled |=
3332 			BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG);
3333 	}
3334 
3335 	if (sta->sta.valid_links) {
3336 		struct ieee80211_link_data *link;
3337 		struct link_sta_info *link_sta;
3338 		u32 est_thr = 0;
3339 		int link_id;
3340 
3341 		sinfo->mlo_params_valid = true;
3342 		sinfo->assoc_link_id = sta->deflink.link_id;
3343 		if (sta->sta.mlo)
3344 			ether_addr_copy(sinfo->mld_addr, sta->addr);
3345 
3346 		/* assign valid links first for iteration */
3347 		sinfo->valid_links = sta->sta.valid_links;
3348 
3349 		for_each_valid_link(sinfo, link_id) {
3350 			struct link_station_info *link_sinfo = sinfo->links[link_id];
3351 
3352 			link_sta = wiphy_dereference(sta->local->hw.wiphy,
3353 						     sta->link[link_id]);
3354 			link = wiphy_dereference(sdata->local->hw.wiphy,
3355 						 sdata->link[link_id]);
3356 
3357 			if (!link_sta || !link_sinfo || !link) {
3358 				sinfo->valid_links &= ~BIT(link_id);
3359 				continue;
3360 			}
3361 			sta_set_link_sinfo(sta, link_sinfo, link, tidstats);
3362 			if (!thr &&
3363 			    (link_sinfo->filled & BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT)))
3364 				est_thr += link_sinfo->expected_throughput;
3365 		}
3366 		if (est_thr) {
3367 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
3368 			sinfo->expected_throughput = est_thr;
3369 		}
3370 	}
3371 }
3372 
3373 u32 sta_get_expected_throughput(struct sta_info *sta)
3374 {
3375 	struct ieee80211_sub_if_data *sdata = sta->sdata;
3376 	struct ieee80211_local *local = sdata->local;
3377 	struct rate_control_ref *ref = NULL;
3378 	u32 thr = 0;
3379 
3380 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
3381 		ref = local->rate_ctrl;
3382 
3383 	/* check if the driver has a SW RC implementation */
3384 	if (ref && ref->ops->get_expected_throughput)
3385 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
3386 	else
3387 		thr = drv_get_expected_throughput(local, sta);
3388 
3389 	return thr;
3390 }
3391 
3392 unsigned long ieee80211_sta_last_active(struct sta_info *sta, int link_id)
3393 {
3394 	struct ieee80211_sta_rx_stats *stats;
3395 	struct link_sta_info *link_sta_info;
3396 
3397 	stats = sta_get_last_rx_stats(sta, link_id);
3398 
3399 	if (link_id < 0)
3400 		link_sta_info = &sta->deflink;
3401 	else
3402 		link_sta_info = wiphy_dereference(sta->local->hw.wiphy,
3403 						  sta->link[link_id]);
3404 
3405 	if (!link_sta_info->status_stats.last_ack ||
3406 	    time_after(stats->last_rx, link_sta_info->status_stats.last_ack))
3407 		return stats->last_rx;
3408 
3409 	return link_sta_info->status_stats.last_ack;
3410 }
3411 
3412 int ieee80211_sta_allocate_link(struct sta_info *sta, unsigned int link_id)
3413 {
3414 	struct ieee80211_sub_if_data *sdata = sta->sdata;
3415 	struct sta_link_alloc *alloc;
3416 	int ret;
3417 
3418 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
3419 
3420 	WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED));
3421 
3422 	/* must represent an MLD from the start */
3423 	if (WARN_ON(!sta->sta.valid_links))
3424 		return -EINVAL;
3425 
3426 	if (WARN_ON(sta->sta.valid_links & BIT(link_id) ||
3427 		    sta->link[link_id]))
3428 		return -EBUSY;
3429 
3430 	alloc = kzalloc_obj(*alloc);
3431 	if (!alloc)
3432 		return -ENOMEM;
3433 
3434 	ret = sta_info_alloc_link(sdata->local, &alloc->info, GFP_KERNEL);
3435 	if (ret) {
3436 		kfree(alloc);
3437 		return ret;
3438 	}
3439 
3440 	sta_info_add_link(sta, link_id, &alloc->info, &alloc->sta);
3441 
3442 	ieee80211_link_sta_debugfs_add(&alloc->info);
3443 
3444 	return 0;
3445 }
3446 
3447 void ieee80211_sta_free_link(struct sta_info *sta, unsigned int link_id)
3448 {
3449 	lockdep_assert_wiphy(sta->sdata->local->hw.wiphy);
3450 
3451 	WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED));
3452 
3453 	sta_remove_link(sta, link_id, false);
3454 }
3455 
3456 int ieee80211_sta_activate_link(struct sta_info *sta, unsigned int link_id)
3457 {
3458 	struct ieee80211_sub_if_data *sdata = sta->sdata;
3459 	struct link_sta_info *link_sta;
3460 	u16 old_links = sta->sta.valid_links;
3461 	u16 new_links = old_links | BIT(link_id);
3462 	int ret;
3463 
3464 	link_sta = rcu_dereference_protected(sta->link[link_id],
3465 					     lockdep_is_held(&sdata->local->hw.wiphy->mtx));
3466 
3467 	if (WARN_ON(old_links == new_links || !link_sta))
3468 		return -EINVAL;
3469 
3470 	rcu_read_lock();
3471 	if (link_sta_info_hash_lookup(sdata->local, link_sta->addr)) {
3472 		rcu_read_unlock();
3473 		return -EALREADY;
3474 	}
3475 	/* we only modify under the mutex so this is fine */
3476 	rcu_read_unlock();
3477 
3478 	sta->sta.valid_links = new_links;
3479 
3480 	if (WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED)))
3481 		goto hash;
3482 
3483 	ieee80211_recalc_min_chandef(sdata, link_id);
3484 
3485 	/* Ensure the values are updated for the driver,
3486 	 * redone by sta_remove_link on failure.
3487 	 */
3488 	ieee80211_sta_recalc_aggregates(&sta->sta);
3489 
3490 	ret = drv_change_sta_links(sdata->local, sdata, &sta->sta,
3491 				   old_links, new_links);
3492 	if (ret) {
3493 		sta->sta.valid_links = old_links;
3494 		sta_remove_link(sta, link_id, false);
3495 		return ret;
3496 	}
3497 
3498 hash:
3499 	ret = link_sta_info_hash_add(sdata->local, link_sta);
3500 	WARN_ON(ret);
3501 	return 0;
3502 }
3503 
3504 void ieee80211_sta_remove_link(struct sta_info *sta, unsigned int link_id)
3505 {
3506 	struct ieee80211_sub_if_data *sdata = sta->sdata;
3507 	u16 old_links = sta->sta.valid_links;
3508 
3509 	lockdep_assert_wiphy(sdata->local->hw.wiphy);
3510 
3511 	sta->sta.valid_links &= ~BIT(link_id);
3512 
3513 	if (!WARN_ON(!test_sta_flag(sta, WLAN_STA_INSERTED)))
3514 		drv_change_sta_links(sdata->local, sdata, &sta->sta,
3515 				     old_links, sta->sta.valid_links);
3516 
3517 	sta_remove_link(sta, link_id, true);
3518 }
3519 
3520 static u8 ieee80211_sta_nss_capability(struct link_sta_info *link_sta)
3521 {
3522 	u8 ht_rx_nss = 0, vht_rx_nss = 0, he_rx_nss = 0, eht_rx_nss = 0, rx_nss;
3523 	bool support_160;
3524 
3525 	if (link_sta->pub->eht_cap.has_eht) {
3526 		int i;
3527 		const u8 *rx_nss_mcs = (void *)&link_sta->pub->eht_cap.eht_mcs_nss_supp;
3528 
3529 		/* get the max nss for EHT over all possible bandwidths and mcs */
3530 		for (i = 0; i < sizeof(struct ieee80211_eht_mcs_nss_supp); i++)
3531 			eht_rx_nss = max_t(u8, eht_rx_nss,
3532 					   u8_get_bits(rx_nss_mcs[i],
3533 						       IEEE80211_EHT_MCS_NSS_RX));
3534 	}
3535 
3536 	if (link_sta->pub->he_cap.has_he) {
3537 		int i;
3538 		u8 rx_mcs_80 = 0, rx_mcs_160 = 0;
3539 		const struct ieee80211_sta_he_cap *he_cap = &link_sta->pub->he_cap;
3540 		u16 mcs_160_map =
3541 			le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
3542 		u16 mcs_80_map = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
3543 
3544 		for (i = 7; i >= 0; i--) {
3545 			u8 mcs_160 = (mcs_160_map >> (2 * i)) & 3;
3546 
3547 			if (mcs_160 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
3548 				rx_mcs_160 = i + 1;
3549 				break;
3550 			}
3551 		}
3552 		for (i = 7; i >= 0; i--) {
3553 			u8 mcs_80 = (mcs_80_map >> (2 * i)) & 3;
3554 
3555 			if (mcs_80 != IEEE80211_HE_MCS_NOT_SUPPORTED) {
3556 				rx_mcs_80 = i + 1;
3557 				break;
3558 			}
3559 		}
3560 
3561 		support_160 = he_cap->he_cap_elem.phy_cap_info[0] &
3562 			      IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G;
3563 
3564 		if (support_160)
3565 			he_rx_nss = min(rx_mcs_80, rx_mcs_160);
3566 		else
3567 			he_rx_nss = rx_mcs_80;
3568 	}
3569 
3570 	if (link_sta->pub->ht_cap.ht_supported) {
3571 		if (link_sta->pub->ht_cap.mcs.rx_mask[0])
3572 			ht_rx_nss++;
3573 		if (link_sta->pub->ht_cap.mcs.rx_mask[1])
3574 			ht_rx_nss++;
3575 		if (link_sta->pub->ht_cap.mcs.rx_mask[2])
3576 			ht_rx_nss++;
3577 		if (link_sta->pub->ht_cap.mcs.rx_mask[3])
3578 			ht_rx_nss++;
3579 		/* FIXME: consider rx_highest? */
3580 	}
3581 
3582 	if (link_sta->pub->vht_cap.vht_supported) {
3583 		int i;
3584 		u16 rx_mcs_map;
3585 
3586 		rx_mcs_map = le16_to_cpu(link_sta->pub->vht_cap.vht_mcs.rx_mcs_map);
3587 
3588 		for (i = 7; i >= 0; i--) {
3589 			u8 mcs = (rx_mcs_map >> (2 * i)) & 3;
3590 
3591 			if (mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
3592 				vht_rx_nss = i + 1;
3593 				break;
3594 			}
3595 		}
3596 		/* FIXME: consider rx_highest? */
3597 	}
3598 
3599 	rx_nss = max(vht_rx_nss, ht_rx_nss);
3600 	rx_nss = max(he_rx_nss, rx_nss);
3601 	rx_nss = max(eht_rx_nss, rx_nss);
3602 	rx_nss = max_t(u8, 1, rx_nss);
3603 
3604 	return rx_nss;
3605 }
3606 
3607 void ieee80211_sta_init_nss_bw_capa(struct link_sta_info *link_sta,
3608 				    struct cfg80211_chan_def *chandef)
3609 {
3610 	/*
3611 	 * TODO: The entirety of the STA Tx/Rx bandwidth handling
3612 	 * assumes 20MHz based widths, so for now don't initialise
3613 	 * pubsta->bandwidth for S1G bands. Since enum
3614 	 * ieee80211_sta_rx_bandwidth is ordered, we will probably
3615 	 * need to introduce ieee80211_s1g_sta_rx_bandwidth with
3616 	 * S1G widths and associated S1G specific code. Additionally,
3617 	 * existing S1G hardware is all 1SS, in the future if hardware
3618 	 * starts supporting >1SS this should be implemented in
3619 	 * ieee80211_sta_nss_capability().
3620 	 */
3621 	if (cfg80211_chandef_is_s1g(chandef)) {
3622 		link_sta->capa_nss = 1;
3623 		link_sta->pub->rx_nss = 1;
3624 		return;
3625 	}
3626 
3627 	link_sta->capa_nss = ieee80211_sta_nss_capability(link_sta);
3628 	link_sta->pub->rx_nss = link_sta->capa_nss;
3629 
3630 	link_sta->pub->bandwidth =
3631 		ieee80211_sta_current_bw(link_sta, chandef,
3632 					 IEEE80211_STA_BW_TX_TO_STA);
3633 }
3634 
3635 void ieee80211_sta_set_max_amsdu_subframes(struct sta_info *sta,
3636 					   const u8 *ext_capab,
3637 					   unsigned int ext_capab_len)
3638 {
3639 	u8 val;
3640 
3641 	sta->sta.max_amsdu_subframes = 0;
3642 
3643 	if (ext_capab_len < 8)
3644 		return;
3645 
3646 	/* The sender might not have sent the last bit, consider it to be 0 */
3647 	val = u8_get_bits(ext_capab[7], WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB);
3648 
3649 	/* we did get all the bits, take the MSB as well */
3650 	if (ext_capab_len >= 9)
3651 		val |= u8_get_bits(ext_capab[8],
3652 				   WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB) << 1;
3653 
3654 	if (val)
3655 		sta->sta.max_amsdu_subframes = 4 << (4 - val);
3656 }
3657 
3658 #ifdef CONFIG_LOCKDEP
3659 bool lockdep_sta_mutex_held(struct ieee80211_sta *pubsta)
3660 {
3661 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
3662 
3663 	return lockdep_is_held(&sta->local->hw.wiphy->mtx);
3664 }
3665 EXPORT_SYMBOL(lockdep_sta_mutex_held);
3666 #endif
3667 
3668 /**
3669  * ieee80211_sta_bw_capability - get STA's bandwidth capability
3670  * @link_sta: the (link) STA to get the capability for
3671  * @band: the band to get the capability on
3672  *
3673  * Return: the maximum bandwidth supported by the STA
3674  */
3675 static enum ieee80211_sta_rx_bandwidth
3676 ieee80211_sta_bw_capability(struct link_sta_info *link_sta,
3677 			    enum nl80211_band band)
3678 {
3679 	struct ieee80211_sta_vht_cap *vht_cap = &link_sta->pub->vht_cap;
3680 	struct ieee80211_sta_he_cap *he_cap = &link_sta->pub->he_cap;
3681 	struct ieee80211_sta_eht_cap *eht_cap = &link_sta->pub->eht_cap;
3682 	u32 cap_width;
3683 
3684 	if (he_cap->has_he) {
3685 		u8 info;
3686 
3687 		if (eht_cap->has_eht && band == NL80211_BAND_6GHZ) {
3688 			info = eht_cap->eht_cap_elem.phy_cap_info[0];
3689 
3690 			if (info & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)
3691 				return IEEE80211_STA_RX_BW_320;
3692 		}
3693 
3694 		info = he_cap->he_cap_elem.phy_cap_info[0];
3695 
3696 		if (band == NL80211_BAND_2GHZ) {
3697 			if (info & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G)
3698 				return IEEE80211_STA_RX_BW_40;
3699 			return IEEE80211_STA_RX_BW_20;
3700 		}
3701 
3702 		if (info & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G ||
3703 		    info & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
3704 			return IEEE80211_STA_RX_BW_160;
3705 
3706 		if (info & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)
3707 			return IEEE80211_STA_RX_BW_80;
3708 
3709 		return IEEE80211_STA_RX_BW_20;
3710 	}
3711 
3712 	if (!vht_cap->vht_supported)
3713 		return link_sta->pub->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 ?
3714 				IEEE80211_STA_RX_BW_40 :
3715 				IEEE80211_STA_RX_BW_20;
3716 
3717 	cap_width = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
3718 
3719 	if (cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ ||
3720 	    cap_width == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
3721 		return IEEE80211_STA_RX_BW_160;
3722 
3723 	/*
3724 	 * If this is non-zero, then it does support 160 MHz after all,
3725 	 * in one form or the other. We don't distinguish here (or even
3726 	 * above) between 160 and 80+80 yet.
3727 	 */
3728 	if (vht_cap->cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK)
3729 		return IEEE80211_STA_RX_BW_160;
3730 
3731 	return IEEE80211_STA_RX_BW_80;
3732 }
3733 
3734 /**
3735  * ieee80211_sta_usable_bw - get STA's usable bandwidth capability
3736  * @link_sta: the (link) STA to get the capability for
3737  * @band: the band to get the capability on
3738  *
3739  * If the STA is on an AP interface, take into account the AP's
3740  * bandwidth corresponding to this station's PHY capability
3741  *
3742  * Return: the maximum bandwidth supported by the STA on the
3743  *	connection to the interface it's connected to
3744  */
3745 static enum ieee80211_sta_rx_bandwidth
3746 ieee80211_sta_usable_bw(struct link_sta_info *link_sta,
3747 			enum nl80211_band band)
3748 {
3749 	struct ieee80211_sub_if_data *sdata = link_sta->sta->sdata;
3750 	enum ieee80211_sta_rx_bandwidth bw;
3751 	struct ieee80211_link_data *link;
3752 
3753 	bw = ieee80211_sta_bw_capability(link_sta, band);
3754 
3755 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
3756 		sdata = get_bss_sdata(sdata);
3757 
3758 		/* for a STA to exist on VLAN, it must have AP */
3759 		if (WARN_ON(!sdata))
3760 			return IEEE80211_STA_RX_BW_20;
3761 	}
3762 
3763 	if (sdata->vif.type != NL80211_IFTYPE_AP)
3764 		return bw;
3765 
3766 	/* for a link STA to exist, vif must have the link */
3767 	link = sdata_dereference(sdata->link[link_sta->link_id], sdata);
3768 	if (WARN_ON(!link))
3769 		return IEEE80211_STA_RX_BW_20;
3770 
3771 	if (!link_sta->pub->eht_cap.has_eht)
3772 		return min(bw, link->bss_bw.he_and_lower);
3773 
3774 	if (!link_sta->pub->uhr_cap.has_uhr ||
3775 	    !link_sta->uhr_dbe_enabled)
3776 		return min(bw, link->bss_bw.eht);
3777 
3778 	return bw;
3779 }
3780 
3781 static enum ieee80211_sta_rx_bandwidth
3782 ieee80211_sta_current_bw_rx_from_sta(struct link_sta_info *link_sta,
3783 				     struct cfg80211_chan_def *chandef)
3784 {
3785 	/*
3786 	 * Take RX OMI into account. The value "rx_omi_bw_rx" is what
3787 	 * we've indicated to the STA we can currently receive.
3788 	 *
3789 	 * This is needed since the RX OMI is done by us to save power,
3790 	 * requiring changing both our TX (rate control) and RX (chanctx),
3791 	 * which in turn needs to be done in the right order (stop TX
3792 	 * at a higher bandwidth first while reducing bandwidth, and
3793 	 * change the chanctx only after the peer accepts, etc.)
3794 	 */
3795 	return min(ieee80211_sta_usable_bw(link_sta, chandef->chan->band),
3796 		   link_sta->rx_omi_bw_rx);
3797 }
3798 
3799 static enum ieee80211_sta_rx_bandwidth
3800 ieee80211_sta_current_bw_tx_to_sta(struct link_sta_info *link_sta,
3801 				   struct cfg80211_chan_def *chandef)
3802 {
3803 	struct sta_info *sta = link_sta->sta;
3804 	enum nl80211_chan_width bss_width;
3805 	enum ieee80211_sta_rx_bandwidth bw;
3806 	enum nl80211_band band;
3807 
3808 	bss_width = chandef->width;
3809 	band = chandef->chan->band;
3810 
3811 	bw = ieee80211_sta_usable_bw(link_sta, band);
3812 	bw = min(bw, link_sta->op_mode_bw);
3813 	/* also limit to RX OMI bandwidth we TX to the STA */
3814 	bw = min(bw, link_sta->rx_omi_bw_tx);
3815 	/* and UHR DBE transition limits */
3816 	bw = min(bw, link_sta->uhr_usable_tx_width);
3817 
3818 	/* Don't consider AP's bandwidth for TDLS peers, section 11.23.1 of
3819 	 * IEEE80211-2016 specification makes higher bandwidth operation
3820 	 * possible on the TDLS link if the peers have wider bandwidth
3821 	 * capability.
3822 	 *
3823 	 * However, in this case, and only if the TDLS peer is authorized,
3824 	 * limit to the tdls_chandef so that the configuration here isn't
3825 	 * wider than what's actually requested on the channel context.
3826 	 */
3827 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER) &&
3828 	    test_sta_flag(sta, WLAN_STA_TDLS_WIDER_BW) &&
3829 	    test_sta_flag(sta, WLAN_STA_AUTHORIZED) &&
3830 	    sta->tdls_chandef.chan)
3831 		bw = min(bw, ieee80211_chan_width_to_rx_bw(sta->tdls_chandef.width));
3832 	else
3833 		bw = min(bw, ieee80211_chan_width_to_rx_bw(bss_width));
3834 
3835 	return bw;
3836 }
3837 
3838 /**
3839  * ieee80211_sta_current_bw - get STA's current usable bandwidth
3840  * @link_sta: the (link) STA to get the bandwidth for
3841  * @chandef: the chandef for the channel the STA is on
3842  * @direction: the direction (to or from STA)
3843  *
3844  * Return: the maximum bandwidth that the station can/may
3845  *	(currently) use in the given direction
3846  */
3847 enum ieee80211_sta_rx_bandwidth
3848 ieee80211_sta_current_bw(struct link_sta_info *link_sta,
3849 			 struct cfg80211_chan_def *chandef,
3850 			 enum ieee80211_sta_bw_direction direction)
3851 {
3852 	if (WARN_ON(!chandef))
3853 		return IEEE80211_STA_RX_BW_20;
3854 
3855 	switch (direction) {
3856 	case IEEE80211_STA_BW_RX_FROM_STA:
3857 		return ieee80211_sta_current_bw_rx_from_sta(link_sta, chandef);
3858 	case IEEE80211_STA_BW_TX_TO_STA:
3859 		return ieee80211_sta_current_bw_tx_to_sta(link_sta, chandef);
3860 	}
3861 
3862 	/* unreachable */
3863 	return IEEE80211_STA_RX_BW_20;
3864 }
3865 
3866 bool ieee80211_link_sta_update_rc_bw(struct ieee80211_link_data *link,
3867 				     struct link_sta_info *link_sta)
3868 {
3869 	struct ieee80211_sub_if_data *sdata = link->sdata;
3870 	struct ieee80211_supported_band *sband;
3871 	enum ieee80211_sta_rx_bandwidth new_bw;
3872 	enum nl80211_band band;
3873 
3874 	band = link->conf->chanreq.oper.chan->band;
3875 	sband = sdata->local->hw.wiphy->bands[band];
3876 
3877 	new_bw = ieee80211_sta_current_bw(link_sta, &link->conf->chanreq.oper,
3878 					  IEEE80211_STA_BW_TX_TO_STA);
3879 	if (link_sta->pub->bandwidth == new_bw)
3880 		return false;
3881 
3882 	link_sta->pub->bandwidth = new_bw;
3883 	rate_control_rate_update(sdata->local, sband, link_sta,
3884 				 IEEE80211_RC_BW_CHANGED);
3885 
3886 	return true;
3887 }
3888