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