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