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