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