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