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