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