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