xref: /openbmc/linux/net/mac80211/sta_info.c (revision 0ead2510f8cec11ce96308d79a1b4ee272fb5238)
1 /*
2  * Copyright 2002-2005, Instant802 Networks, Inc.
3  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
4  * Copyright 2013-2014  Intel Mobile Communications GmbH
5  * Copyright (C) 2015 Intel Deutschland GmbH
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/etherdevice.h>
15 #include <linux/netdevice.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/if_arp.h>
20 #include <linux/timer.h>
21 #include <linux/rtnetlink.h>
22 
23 #include <net/mac80211.h>
24 #include "ieee80211_i.h"
25 #include "driver-ops.h"
26 #include "rate.h"
27 #include "sta_info.h"
28 #include "debugfs_sta.h"
29 #include "mesh.h"
30 #include "wme.h"
31 
32 /**
33  * DOC: STA information lifetime rules
34  *
35  * STA info structures (&struct sta_info) are managed in a hash table
36  * for faster lookup and a list for iteration. They are managed using
37  * RCU, i.e. access to the list and hash table is protected by RCU.
38  *
39  * Upon allocating a STA info structure with sta_info_alloc(), the caller
40  * owns that structure. It must then insert it into the hash table using
41  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
42  * case (which acquires an rcu read section but must not be called from
43  * within one) will the pointer still be valid after the call. Note that
44  * the caller may not do much with the STA info before inserting it, in
45  * particular, it may not start any mesh peer link management or add
46  * encryption keys.
47  *
48  * When the insertion fails (sta_info_insert()) returns non-zero), the
49  * structure will have been freed by sta_info_insert()!
50  *
51  * Station entries are added by mac80211 when you establish a link with a
52  * peer. This means different things for the different type of interfaces
53  * we support. For a regular station this mean we add the AP sta when we
54  * receive an association response from the AP. For IBSS this occurs when
55  * get to know about a peer on the same IBSS. For WDS we add the sta for
56  * the peer immediately upon device open. When using AP mode we add stations
57  * for each respective station upon request from userspace through nl80211.
58  *
59  * In order to remove a STA info structure, various sta_info_destroy_*()
60  * calls are available.
61  *
62  * There is no concept of ownership on a STA entry, each structure is
63  * owned by the global hash table/list until it is removed. All users of
64  * the structure need to be RCU protected so that the structure won't be
65  * freed before they are done using it.
66  */
67 
68 static const struct rhashtable_params sta_rht_params = {
69 	.nelem_hint = 3, /* start small */
70 	.automatic_shrinking = true,
71 	.head_offset = offsetof(struct sta_info, hash_node),
72 	.key_offset = offsetof(struct sta_info, addr),
73 	.key_len = ETH_ALEN,
74 	.hashfn = sta_addr_hash,
75 	.max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE,
76 };
77 
78 /* Caller must hold local->sta_mtx */
79 static int sta_info_hash_del(struct ieee80211_local *local,
80 			     struct sta_info *sta)
81 {
82 	return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
83 				      sta_rht_params);
84 }
85 
86 static void __cleanup_single_sta(struct sta_info *sta)
87 {
88 	int ac, i;
89 	struct tid_ampdu_tx *tid_tx;
90 	struct ieee80211_sub_if_data *sdata = sta->sdata;
91 	struct ieee80211_local *local = sdata->local;
92 	struct ps_data *ps;
93 
94 	if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
95 	    test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
96 	    test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
97 		if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
98 		    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
99 			ps = &sdata->bss->ps;
100 		else if (ieee80211_vif_is_mesh(&sdata->vif))
101 			ps = &sdata->u.mesh.ps;
102 		else
103 			return;
104 
105 		clear_sta_flag(sta, WLAN_STA_PS_STA);
106 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
107 		clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
108 
109 		atomic_dec(&ps->num_sta_ps);
110 	}
111 
112 	if (sta->sta.txq[0]) {
113 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
114 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
115 			int n = skb_queue_len(&txqi->queue);
116 
117 			ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
118 			atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
119 		}
120 	}
121 
122 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
123 		local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
124 		ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
125 		ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
126 	}
127 
128 	if (ieee80211_vif_is_mesh(&sdata->vif))
129 		mesh_sta_cleanup(sta);
130 
131 	cancel_work_sync(&sta->drv_deliver_wk);
132 
133 	/*
134 	 * Destroy aggregation state here. It would be nice to wait for the
135 	 * driver to finish aggregation stop and then clean up, but for now
136 	 * drivers have to handle aggregation stop being requested, followed
137 	 * directly by station destruction.
138 	 */
139 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
140 		kfree(sta->ampdu_mlme.tid_start_tx[i]);
141 		tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
142 		if (!tid_tx)
143 			continue;
144 		ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
145 		kfree(tid_tx);
146 	}
147 }
148 
149 static void cleanup_single_sta(struct sta_info *sta)
150 {
151 	struct ieee80211_sub_if_data *sdata = sta->sdata;
152 	struct ieee80211_local *local = sdata->local;
153 
154 	__cleanup_single_sta(sta);
155 	sta_info_free(local, sta);
156 }
157 
158 /* protected by RCU */
159 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
160 			      const u8 *addr)
161 {
162 	struct ieee80211_local *local = sdata->local;
163 	struct sta_info *sta;
164 	struct rhash_head *tmp;
165 	const struct bucket_table *tbl;
166 
167 	rcu_read_lock();
168 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
169 
170 	for_each_sta_info(local, tbl, addr, sta, tmp) {
171 		if (sta->sdata == sdata) {
172 			rcu_read_unlock();
173 			/* this is safe as the caller must already hold
174 			 * another rcu read section or the mutex
175 			 */
176 			return sta;
177 		}
178 	}
179 	rcu_read_unlock();
180 	return NULL;
181 }
182 
183 /*
184  * Get sta info either from the specified interface
185  * or from one of its vlans
186  */
187 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
188 				  const u8 *addr)
189 {
190 	struct ieee80211_local *local = sdata->local;
191 	struct sta_info *sta;
192 	struct rhash_head *tmp;
193 	const struct bucket_table *tbl;
194 
195 	rcu_read_lock();
196 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
197 
198 	for_each_sta_info(local, tbl, addr, sta, tmp) {
199 		if (sta->sdata == sdata ||
200 		    (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
201 			rcu_read_unlock();
202 			/* this is safe as the caller must already hold
203 			 * another rcu read section or the mutex
204 			 */
205 			return sta;
206 		}
207 	}
208 	rcu_read_unlock();
209 	return NULL;
210 }
211 
212 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
213 				     int idx)
214 {
215 	struct ieee80211_local *local = sdata->local;
216 	struct sta_info *sta;
217 	int i = 0;
218 
219 	list_for_each_entry_rcu(sta, &local->sta_list, list) {
220 		if (sdata != sta->sdata)
221 			continue;
222 		if (i < idx) {
223 			++i;
224 			continue;
225 		}
226 		return sta;
227 	}
228 
229 	return NULL;
230 }
231 
232 /**
233  * sta_info_free - free STA
234  *
235  * @local: pointer to the global information
236  * @sta: STA info to free
237  *
238  * This function must undo everything done by sta_info_alloc()
239  * that may happen before sta_info_insert(). It may only be
240  * called when sta_info_insert() has not been attempted (and
241  * if that fails, the station is freed anyway.)
242  */
243 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
244 {
245 	if (sta->rate_ctrl)
246 		rate_control_free_sta(sta);
247 
248 	sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
249 
250 	if (sta->sta.txq[0])
251 		kfree(to_txq_info(sta->sta.txq[0]));
252 	kfree(rcu_dereference_raw(sta->sta.rates));
253 #ifdef CONFIG_MAC80211_MESH
254 	kfree(sta->mesh);
255 #endif
256 	kfree(sta);
257 }
258 
259 /* Caller must hold local->sta_mtx */
260 static void sta_info_hash_add(struct ieee80211_local *local,
261 			      struct sta_info *sta)
262 {
263 	rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
264 			       sta_rht_params);
265 }
266 
267 static void sta_deliver_ps_frames(struct work_struct *wk)
268 {
269 	struct sta_info *sta;
270 
271 	sta = container_of(wk, struct sta_info, drv_deliver_wk);
272 
273 	if (sta->dead)
274 		return;
275 
276 	local_bh_disable();
277 	if (!test_sta_flag(sta, WLAN_STA_PS_STA))
278 		ieee80211_sta_ps_deliver_wakeup(sta);
279 	else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL))
280 		ieee80211_sta_ps_deliver_poll_response(sta);
281 	else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD))
282 		ieee80211_sta_ps_deliver_uapsd(sta);
283 	local_bh_enable();
284 }
285 
286 static int sta_prepare_rate_control(struct ieee80211_local *local,
287 				    struct sta_info *sta, gfp_t gfp)
288 {
289 	if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL))
290 		return 0;
291 
292 	sta->rate_ctrl = local->rate_ctrl;
293 	sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
294 						     sta, gfp);
295 	if (!sta->rate_ctrl_priv)
296 		return -ENOMEM;
297 
298 	return 0;
299 }
300 
301 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
302 				const u8 *addr, gfp_t gfp)
303 {
304 	struct ieee80211_local *local = sdata->local;
305 	struct ieee80211_hw *hw = &local->hw;
306 	struct sta_info *sta;
307 	int i;
308 
309 	sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
310 	if (!sta)
311 		return NULL;
312 
313 	spin_lock_init(&sta->lock);
314 	spin_lock_init(&sta->ps_lock);
315 	INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
316 	INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
317 	mutex_init(&sta->ampdu_mlme.mtx);
318 #ifdef CONFIG_MAC80211_MESH
319 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
320 		sta->mesh = kzalloc(sizeof(*sta->mesh), gfp);
321 		if (!sta->mesh)
322 			goto free;
323 		spin_lock_init(&sta->mesh->plink_lock);
324 		if (ieee80211_vif_is_mesh(&sdata->vif) &&
325 		    !sdata->u.mesh.user_mpm)
326 			init_timer(&sta->mesh->plink_timer);
327 		sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
328 	}
329 #endif
330 
331 	memcpy(sta->addr, addr, ETH_ALEN);
332 	memcpy(sta->sta.addr, addr, ETH_ALEN);
333 	sta->local = local;
334 	sta->sdata = sdata;
335 	sta->rx_stats.last_rx = jiffies;
336 
337 	sta->sta_state = IEEE80211_STA_NONE;
338 
339 	/* Mark TID as unreserved */
340 	sta->reserved_tid = IEEE80211_TID_UNRESERVED;
341 
342 	sta->last_connected = ktime_get_seconds();
343 	ewma_signal_init(&sta->rx_stats.avg_signal);
344 	for (i = 0; i < ARRAY_SIZE(sta->rx_stats.chain_signal_avg); i++)
345 		ewma_signal_init(&sta->rx_stats.chain_signal_avg[i]);
346 
347 	if (local->ops->wake_tx_queue) {
348 		void *txq_data;
349 		int size = sizeof(struct txq_info) +
350 			   ALIGN(hw->txq_data_size, sizeof(void *));
351 
352 		txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
353 		if (!txq_data)
354 			goto free;
355 
356 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
357 			struct txq_info *txq = txq_data + i * size;
358 
359 			ieee80211_init_tx_queue(sdata, sta, txq, i);
360 		}
361 	}
362 
363 	if (sta_prepare_rate_control(local, sta, gfp))
364 		goto free_txq;
365 
366 	for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
367 		/*
368 		 * timer_to_tid must be initialized with identity mapping
369 		 * to enable session_timer's data differentiation. See
370 		 * sta_rx_agg_session_timer_expired for usage.
371 		 */
372 		sta->timer_to_tid[i] = i;
373 	}
374 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
375 		skb_queue_head_init(&sta->ps_tx_buf[i]);
376 		skb_queue_head_init(&sta->tx_filtered[i]);
377 	}
378 
379 	for (i = 0; i < IEEE80211_NUM_TIDS; i++)
380 		sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
381 
382 	sta->sta.smps_mode = IEEE80211_SMPS_OFF;
383 	if (sdata->vif.type == NL80211_IFTYPE_AP ||
384 	    sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
385 		struct ieee80211_supported_band *sband =
386 			hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
387 		u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
388 				IEEE80211_HT_CAP_SM_PS_SHIFT;
389 		/*
390 		 * Assume that hostapd advertises our caps in the beacon and
391 		 * this is the known_smps_mode for a station that just assciated
392 		 */
393 		switch (smps) {
394 		case WLAN_HT_SMPS_CONTROL_DISABLED:
395 			sta->known_smps_mode = IEEE80211_SMPS_OFF;
396 			break;
397 		case WLAN_HT_SMPS_CONTROL_STATIC:
398 			sta->known_smps_mode = IEEE80211_SMPS_STATIC;
399 			break;
400 		case WLAN_HT_SMPS_CONTROL_DYNAMIC:
401 			sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
402 			break;
403 		default:
404 			WARN_ON(1);
405 		}
406 	}
407 
408 	sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
409 
410 	return sta;
411 
412 free_txq:
413 	if (sta->sta.txq[0])
414 		kfree(to_txq_info(sta->sta.txq[0]));
415 free:
416 #ifdef CONFIG_MAC80211_MESH
417 	kfree(sta->mesh);
418 #endif
419 	kfree(sta);
420 	return NULL;
421 }
422 
423 static int sta_info_insert_check(struct sta_info *sta)
424 {
425 	struct ieee80211_sub_if_data *sdata = sta->sdata;
426 
427 	/*
428 	 * Can't be a WARN_ON because it can be triggered through a race:
429 	 * something inserts a STA (on one CPU) without holding the RTNL
430 	 * and another CPU turns off the net device.
431 	 */
432 	if (unlikely(!ieee80211_sdata_running(sdata)))
433 		return -ENETDOWN;
434 
435 	if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
436 		    is_multicast_ether_addr(sta->sta.addr)))
437 		return -EINVAL;
438 
439 	/* Strictly speaking this isn't necessary as we hold the mutex, but
440 	 * the rhashtable code can't really deal with that distinction. We
441 	 * do require the mutex for correctness though.
442 	 */
443 	rcu_read_lock();
444 	lockdep_assert_held(&sdata->local->sta_mtx);
445 	if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) &&
446 	    ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) {
447 		rcu_read_unlock();
448 		return -ENOTUNIQ;
449 	}
450 	rcu_read_unlock();
451 
452 	return 0;
453 }
454 
455 static int sta_info_insert_drv_state(struct ieee80211_local *local,
456 				     struct ieee80211_sub_if_data *sdata,
457 				     struct sta_info *sta)
458 {
459 	enum ieee80211_sta_state state;
460 	int err = 0;
461 
462 	for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
463 		err = drv_sta_state(local, sdata, sta, state, state + 1);
464 		if (err)
465 			break;
466 	}
467 
468 	if (!err) {
469 		/*
470 		 * Drivers using legacy sta_add/sta_remove callbacks only
471 		 * get uploaded set to true after sta_add is called.
472 		 */
473 		if (!local->ops->sta_add)
474 			sta->uploaded = true;
475 		return 0;
476 	}
477 
478 	if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
479 		sdata_info(sdata,
480 			   "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
481 			   sta->sta.addr, state + 1, err);
482 		err = 0;
483 	}
484 
485 	/* unwind on error */
486 	for (; state > IEEE80211_STA_NOTEXIST; state--)
487 		WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
488 
489 	return err;
490 }
491 
492 /*
493  * should be called with sta_mtx locked
494  * this function replaces the mutex lock
495  * with a RCU lock
496  */
497 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
498 {
499 	struct ieee80211_local *local = sta->local;
500 	struct ieee80211_sub_if_data *sdata = sta->sdata;
501 	struct station_info sinfo;
502 	int err = 0;
503 
504 	lockdep_assert_held(&local->sta_mtx);
505 
506 	/* check if STA exists already */
507 	if (sta_info_get_bss(sdata, sta->sta.addr)) {
508 		err = -EEXIST;
509 		goto out_err;
510 	}
511 
512 	local->num_sta++;
513 	local->sta_generation++;
514 	smp_mb();
515 
516 	/* simplify things and don't accept BA sessions yet */
517 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
518 
519 	/* make the station visible */
520 	sta_info_hash_add(local, sta);
521 
522 	list_add_tail_rcu(&sta->list, &local->sta_list);
523 
524 	/* notify driver */
525 	err = sta_info_insert_drv_state(local, sdata, sta);
526 	if (err)
527 		goto out_remove;
528 
529 	set_sta_flag(sta, WLAN_STA_INSERTED);
530 	/* accept BA sessions now */
531 	clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
532 
533 	ieee80211_recalc_min_chandef(sdata);
534 	ieee80211_sta_debugfs_add(sta);
535 	rate_control_add_sta_debugfs(sta);
536 
537 	memset(&sinfo, 0, sizeof(sinfo));
538 	sinfo.filled = 0;
539 	sinfo.generation = local->sta_generation;
540 	cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
541 
542 	sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
543 
544 	/* move reference to rcu-protected */
545 	rcu_read_lock();
546 	mutex_unlock(&local->sta_mtx);
547 
548 	if (ieee80211_vif_is_mesh(&sdata->vif))
549 		mesh_accept_plinks_update(sdata);
550 
551 	return 0;
552  out_remove:
553 	sta_info_hash_del(local, sta);
554 	list_del_rcu(&sta->list);
555 	local->num_sta--;
556 	synchronize_net();
557 	__cleanup_single_sta(sta);
558  out_err:
559 	mutex_unlock(&local->sta_mtx);
560 	rcu_read_lock();
561 	return err;
562 }
563 
564 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
565 {
566 	struct ieee80211_local *local = sta->local;
567 	int err;
568 
569 	might_sleep();
570 
571 	mutex_lock(&local->sta_mtx);
572 
573 	err = sta_info_insert_check(sta);
574 	if (err) {
575 		mutex_unlock(&local->sta_mtx);
576 		rcu_read_lock();
577 		goto out_free;
578 	}
579 
580 	err = sta_info_insert_finish(sta);
581 	if (err)
582 		goto out_free;
583 
584 	return 0;
585  out_free:
586 	sta_info_free(local, sta);
587 	return err;
588 }
589 
590 int sta_info_insert(struct sta_info *sta)
591 {
592 	int err = sta_info_insert_rcu(sta);
593 
594 	rcu_read_unlock();
595 
596 	return err;
597 }
598 
599 static inline void __bss_tim_set(u8 *tim, u16 id)
600 {
601 	/*
602 	 * This format has been mandated by the IEEE specifications,
603 	 * so this line may not be changed to use the __set_bit() format.
604 	 */
605 	tim[id / 8] |= (1 << (id % 8));
606 }
607 
608 static inline void __bss_tim_clear(u8 *tim, u16 id)
609 {
610 	/*
611 	 * This format has been mandated by the IEEE specifications,
612 	 * so this line may not be changed to use the __clear_bit() format.
613 	 */
614 	tim[id / 8] &= ~(1 << (id % 8));
615 }
616 
617 static inline bool __bss_tim_get(u8 *tim, u16 id)
618 {
619 	/*
620 	 * This format has been mandated by the IEEE specifications,
621 	 * so this line may not be changed to use the test_bit() format.
622 	 */
623 	return tim[id / 8] & (1 << (id % 8));
624 }
625 
626 static unsigned long ieee80211_tids_for_ac(int ac)
627 {
628 	/* If we ever support TIDs > 7, this obviously needs to be adjusted */
629 	switch (ac) {
630 	case IEEE80211_AC_VO:
631 		return BIT(6) | BIT(7);
632 	case IEEE80211_AC_VI:
633 		return BIT(4) | BIT(5);
634 	case IEEE80211_AC_BE:
635 		return BIT(0) | BIT(3);
636 	case IEEE80211_AC_BK:
637 		return BIT(1) | BIT(2);
638 	default:
639 		WARN_ON(1);
640 		return 0;
641 	}
642 }
643 
644 static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
645 {
646 	struct ieee80211_local *local = sta->local;
647 	struct ps_data *ps;
648 	bool indicate_tim = false;
649 	u8 ignore_for_tim = sta->sta.uapsd_queues;
650 	int ac;
651 	u16 id = sta->sta.aid;
652 
653 	if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
654 	    sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
655 		if (WARN_ON_ONCE(!sta->sdata->bss))
656 			return;
657 
658 		ps = &sta->sdata->bss->ps;
659 #ifdef CONFIG_MAC80211_MESH
660 	} else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
661 		ps = &sta->sdata->u.mesh.ps;
662 #endif
663 	} else {
664 		return;
665 	}
666 
667 	/* No need to do anything if the driver does all */
668 	if (ieee80211_hw_check(&local->hw, AP_LINK_PS))
669 		return;
670 
671 	if (sta->dead)
672 		goto done;
673 
674 	/*
675 	 * If all ACs are delivery-enabled then we should build
676 	 * the TIM bit for all ACs anyway; if only some are then
677 	 * we ignore those and build the TIM bit using only the
678 	 * non-enabled ones.
679 	 */
680 	if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
681 		ignore_for_tim = 0;
682 
683 	if (ignore_pending)
684 		ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1;
685 
686 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
687 		unsigned long tids;
688 
689 		if (ignore_for_tim & BIT(ac))
690 			continue;
691 
692 		indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
693 				!skb_queue_empty(&sta->ps_tx_buf[ac]);
694 		if (indicate_tim)
695 			break;
696 
697 		tids = ieee80211_tids_for_ac(ac);
698 
699 		indicate_tim |=
700 			sta->driver_buffered_tids & tids;
701 		indicate_tim |=
702 			sta->txq_buffered_tids & tids;
703 	}
704 
705  done:
706 	spin_lock_bh(&local->tim_lock);
707 
708 	if (indicate_tim == __bss_tim_get(ps->tim, id))
709 		goto out_unlock;
710 
711 	if (indicate_tim)
712 		__bss_tim_set(ps->tim, id);
713 	else
714 		__bss_tim_clear(ps->tim, id);
715 
716 	if (local->ops->set_tim && !WARN_ON(sta->dead)) {
717 		local->tim_in_locked_section = true;
718 		drv_set_tim(local, &sta->sta, indicate_tim);
719 		local->tim_in_locked_section = false;
720 	}
721 
722 out_unlock:
723 	spin_unlock_bh(&local->tim_lock);
724 }
725 
726 void sta_info_recalc_tim(struct sta_info *sta)
727 {
728 	__sta_info_recalc_tim(sta, false);
729 }
730 
731 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
732 {
733 	struct ieee80211_tx_info *info;
734 	int timeout;
735 
736 	if (!skb)
737 		return false;
738 
739 	info = IEEE80211_SKB_CB(skb);
740 
741 	/* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
742 	timeout = (sta->listen_interval *
743 		   sta->sdata->vif.bss_conf.beacon_int *
744 		   32 / 15625) * HZ;
745 	if (timeout < STA_TX_BUFFER_EXPIRE)
746 		timeout = STA_TX_BUFFER_EXPIRE;
747 	return time_after(jiffies, info->control.jiffies + timeout);
748 }
749 
750 
751 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
752 						struct sta_info *sta, int ac)
753 {
754 	unsigned long flags;
755 	struct sk_buff *skb;
756 
757 	/*
758 	 * First check for frames that should expire on the filtered
759 	 * queue. Frames here were rejected by the driver and are on
760 	 * a separate queue to avoid reordering with normal PS-buffered
761 	 * frames. They also aren't accounted for right now in the
762 	 * total_ps_buffered counter.
763 	 */
764 	for (;;) {
765 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
766 		skb = skb_peek(&sta->tx_filtered[ac]);
767 		if (sta_info_buffer_expired(sta, skb))
768 			skb = __skb_dequeue(&sta->tx_filtered[ac]);
769 		else
770 			skb = NULL;
771 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
772 
773 		/*
774 		 * Frames are queued in order, so if this one
775 		 * hasn't expired yet we can stop testing. If
776 		 * we actually reached the end of the queue we
777 		 * also need to stop, of course.
778 		 */
779 		if (!skb)
780 			break;
781 		ieee80211_free_txskb(&local->hw, skb);
782 	}
783 
784 	/*
785 	 * Now also check the normal PS-buffered queue, this will
786 	 * only find something if the filtered queue was emptied
787 	 * since the filtered frames are all before the normal PS
788 	 * buffered frames.
789 	 */
790 	for (;;) {
791 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
792 		skb = skb_peek(&sta->ps_tx_buf[ac]);
793 		if (sta_info_buffer_expired(sta, skb))
794 			skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
795 		else
796 			skb = NULL;
797 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
798 
799 		/*
800 		 * frames are queued in order, so if this one
801 		 * hasn't expired yet (or we reached the end of
802 		 * the queue) we can stop testing
803 		 */
804 		if (!skb)
805 			break;
806 
807 		local->total_ps_buffered--;
808 		ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
809 		       sta->sta.addr);
810 		ieee80211_free_txskb(&local->hw, skb);
811 	}
812 
813 	/*
814 	 * Finally, recalculate the TIM bit for this station -- it might
815 	 * now be clear because the station was too slow to retrieve its
816 	 * frames.
817 	 */
818 	sta_info_recalc_tim(sta);
819 
820 	/*
821 	 * Return whether there are any frames still buffered, this is
822 	 * used to check whether the cleanup timer still needs to run,
823 	 * if there are no frames we don't need to rearm the timer.
824 	 */
825 	return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
826 		 skb_queue_empty(&sta->tx_filtered[ac]));
827 }
828 
829 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
830 					     struct sta_info *sta)
831 {
832 	bool have_buffered = false;
833 	int ac;
834 
835 	/* This is only necessary for stations on BSS/MBSS interfaces */
836 	if (!sta->sdata->bss &&
837 	    !ieee80211_vif_is_mesh(&sta->sdata->vif))
838 		return false;
839 
840 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
841 		have_buffered |=
842 			sta_info_cleanup_expire_buffered_ac(local, sta, ac);
843 
844 	return have_buffered;
845 }
846 
847 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
848 {
849 	struct ieee80211_local *local;
850 	struct ieee80211_sub_if_data *sdata;
851 	int ret;
852 
853 	might_sleep();
854 
855 	if (!sta)
856 		return -ENOENT;
857 
858 	local = sta->local;
859 	sdata = sta->sdata;
860 
861 	lockdep_assert_held(&local->sta_mtx);
862 
863 	/*
864 	 * Before removing the station from the driver and
865 	 * rate control, it might still start new aggregation
866 	 * sessions -- block that to make sure the tear-down
867 	 * will be sufficient.
868 	 */
869 	set_sta_flag(sta, WLAN_STA_BLOCK_BA);
870 	ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
871 
872 	ret = sta_info_hash_del(local, sta);
873 	if (WARN_ON(ret))
874 		return ret;
875 
876 	/*
877 	 * for TDLS peers, make sure to return to the base channel before
878 	 * removal.
879 	 */
880 	if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) {
881 		drv_tdls_cancel_channel_switch(local, sdata, &sta->sta);
882 		clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL);
883 	}
884 
885 	list_del_rcu(&sta->list);
886 
887 	drv_sta_pre_rcu_remove(local, sta->sdata, sta);
888 
889 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
890 	    rcu_access_pointer(sdata->u.vlan.sta) == sta)
891 		RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
892 
893 	return 0;
894 }
895 
896 static void __sta_info_destroy_part2(struct sta_info *sta)
897 {
898 	struct ieee80211_local *local = sta->local;
899 	struct ieee80211_sub_if_data *sdata = sta->sdata;
900 	struct station_info sinfo = {};
901 	int ret;
902 
903 	/*
904 	 * NOTE: This assumes at least synchronize_net() was done
905 	 *	 after _part1 and before _part2!
906 	 */
907 
908 	might_sleep();
909 	lockdep_assert_held(&local->sta_mtx);
910 
911 	/* now keys can no longer be reached */
912 	ieee80211_free_sta_keys(local, sta);
913 
914 	/* disable TIM bit - last chance to tell driver */
915 	__sta_info_recalc_tim(sta, true);
916 
917 	sta->dead = true;
918 
919 	local->num_sta--;
920 	local->sta_generation++;
921 
922 	while (sta->sta_state > IEEE80211_STA_NONE) {
923 		ret = sta_info_move_state(sta, sta->sta_state - 1);
924 		if (ret) {
925 			WARN_ON_ONCE(1);
926 			break;
927 		}
928 	}
929 
930 	if (sta->uploaded) {
931 		ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
932 				    IEEE80211_STA_NOTEXIST);
933 		WARN_ON_ONCE(ret != 0);
934 	}
935 
936 	sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
937 
938 	sta_set_sinfo(sta, &sinfo);
939 	cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL);
940 
941 	rate_control_remove_sta_debugfs(sta);
942 	ieee80211_sta_debugfs_remove(sta);
943 	ieee80211_recalc_min_chandef(sdata);
944 
945 	cleanup_single_sta(sta);
946 }
947 
948 int __must_check __sta_info_destroy(struct sta_info *sta)
949 {
950 	int err = __sta_info_destroy_part1(sta);
951 
952 	if (err)
953 		return err;
954 
955 	synchronize_net();
956 
957 	__sta_info_destroy_part2(sta);
958 
959 	return 0;
960 }
961 
962 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
963 {
964 	struct sta_info *sta;
965 	int ret;
966 
967 	mutex_lock(&sdata->local->sta_mtx);
968 	sta = sta_info_get(sdata, addr);
969 	ret = __sta_info_destroy(sta);
970 	mutex_unlock(&sdata->local->sta_mtx);
971 
972 	return ret;
973 }
974 
975 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
976 			      const u8 *addr)
977 {
978 	struct sta_info *sta;
979 	int ret;
980 
981 	mutex_lock(&sdata->local->sta_mtx);
982 	sta = sta_info_get_bss(sdata, addr);
983 	ret = __sta_info_destroy(sta);
984 	mutex_unlock(&sdata->local->sta_mtx);
985 
986 	return ret;
987 }
988 
989 static void sta_info_cleanup(unsigned long data)
990 {
991 	struct ieee80211_local *local = (struct ieee80211_local *) data;
992 	struct sta_info *sta;
993 	bool timer_needed = false;
994 
995 	rcu_read_lock();
996 	list_for_each_entry_rcu(sta, &local->sta_list, list)
997 		if (sta_info_cleanup_expire_buffered(local, sta))
998 			timer_needed = true;
999 	rcu_read_unlock();
1000 
1001 	if (local->quiescing)
1002 		return;
1003 
1004 	if (!timer_needed)
1005 		return;
1006 
1007 	mod_timer(&local->sta_cleanup,
1008 		  round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
1009 }
1010 
1011 u32 sta_addr_hash(const void *key, u32 length, u32 seed)
1012 {
1013 	return jhash(key, ETH_ALEN, seed);
1014 }
1015 
1016 int sta_info_init(struct ieee80211_local *local)
1017 {
1018 	int err;
1019 
1020 	err = rhashtable_init(&local->sta_hash, &sta_rht_params);
1021 	if (err)
1022 		return err;
1023 
1024 	spin_lock_init(&local->tim_lock);
1025 	mutex_init(&local->sta_mtx);
1026 	INIT_LIST_HEAD(&local->sta_list);
1027 
1028 	setup_timer(&local->sta_cleanup, sta_info_cleanup,
1029 		    (unsigned long)local);
1030 	return 0;
1031 }
1032 
1033 void sta_info_stop(struct ieee80211_local *local)
1034 {
1035 	del_timer_sync(&local->sta_cleanup);
1036 	rhashtable_destroy(&local->sta_hash);
1037 }
1038 
1039 
1040 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1041 {
1042 	struct ieee80211_local *local = sdata->local;
1043 	struct sta_info *sta, *tmp;
1044 	LIST_HEAD(free_list);
1045 	int ret = 0;
1046 
1047 	might_sleep();
1048 
1049 	WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1050 	WARN_ON(vlans && !sdata->bss);
1051 
1052 	mutex_lock(&local->sta_mtx);
1053 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1054 		if (sdata == sta->sdata ||
1055 		    (vlans && sdata->bss == sta->sdata->bss)) {
1056 			if (!WARN_ON(__sta_info_destroy_part1(sta)))
1057 				list_add(&sta->free_list, &free_list);
1058 			ret++;
1059 		}
1060 	}
1061 
1062 	if (!list_empty(&free_list)) {
1063 		synchronize_net();
1064 		list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1065 			__sta_info_destroy_part2(sta);
1066 	}
1067 	mutex_unlock(&local->sta_mtx);
1068 
1069 	return ret;
1070 }
1071 
1072 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1073 			  unsigned long exp_time)
1074 {
1075 	struct ieee80211_local *local = sdata->local;
1076 	struct sta_info *sta, *tmp;
1077 
1078 	mutex_lock(&local->sta_mtx);
1079 
1080 	list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1081 		if (sdata != sta->sdata)
1082 			continue;
1083 
1084 		if (time_after(jiffies, sta->rx_stats.last_rx + exp_time)) {
1085 			sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1086 				sta->sta.addr);
1087 
1088 			if (ieee80211_vif_is_mesh(&sdata->vif) &&
1089 			    test_sta_flag(sta, WLAN_STA_PS_STA))
1090 				atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1091 
1092 			WARN_ON(__sta_info_destroy(sta));
1093 		}
1094 	}
1095 
1096 	mutex_unlock(&local->sta_mtx);
1097 }
1098 
1099 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1100 						   const u8 *addr,
1101 						   const u8 *localaddr)
1102 {
1103 	struct ieee80211_local *local = hw_to_local(hw);
1104 	struct sta_info *sta;
1105 	struct rhash_head *tmp;
1106 	const struct bucket_table *tbl;
1107 
1108 	tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
1109 
1110 	/*
1111 	 * Just return a random station if localaddr is NULL
1112 	 * ... first in list.
1113 	 */
1114 	for_each_sta_info(local, tbl, addr, sta, tmp) {
1115 		if (localaddr &&
1116 		    !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1117 			continue;
1118 		if (!sta->uploaded)
1119 			return NULL;
1120 		return &sta->sta;
1121 	}
1122 
1123 	return NULL;
1124 }
1125 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1126 
1127 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1128 					 const u8 *addr)
1129 {
1130 	struct sta_info *sta;
1131 
1132 	if (!vif)
1133 		return NULL;
1134 
1135 	sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1136 	if (!sta)
1137 		return NULL;
1138 
1139 	if (!sta->uploaded)
1140 		return NULL;
1141 
1142 	return &sta->sta;
1143 }
1144 EXPORT_SYMBOL(ieee80211_find_sta);
1145 
1146 /* powersave support code */
1147 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1148 {
1149 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1150 	struct ieee80211_local *local = sdata->local;
1151 	struct sk_buff_head pending;
1152 	int filtered = 0, buffered = 0, ac, i;
1153 	unsigned long flags;
1154 	struct ps_data *ps;
1155 
1156 	if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1157 		sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1158 				     u.ap);
1159 
1160 	if (sdata->vif.type == NL80211_IFTYPE_AP)
1161 		ps = &sdata->bss->ps;
1162 	else if (ieee80211_vif_is_mesh(&sdata->vif))
1163 		ps = &sdata->u.mesh.ps;
1164 	else
1165 		return;
1166 
1167 	clear_sta_flag(sta, WLAN_STA_SP);
1168 
1169 	BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1170 	sta->driver_buffered_tids = 0;
1171 	sta->txq_buffered_tids = 0;
1172 
1173 	if (!ieee80211_hw_check(&local->hw, AP_LINK_PS))
1174 		drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1175 
1176 	if (sta->sta.txq[0]) {
1177 		for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1178 			struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1179 
1180 			if (!skb_queue_len(&txqi->queue))
1181 				continue;
1182 
1183 			drv_wake_tx_queue(local, txqi);
1184 		}
1185 	}
1186 
1187 	skb_queue_head_init(&pending);
1188 
1189 	/* sync with ieee80211_tx_h_unicast_ps_buf */
1190 	spin_lock(&sta->ps_lock);
1191 	/* Send all buffered frames to the station */
1192 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1193 		int count = skb_queue_len(&pending), tmp;
1194 
1195 		spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1196 		skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1197 		spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1198 		tmp = skb_queue_len(&pending);
1199 		filtered += tmp - count;
1200 		count = tmp;
1201 
1202 		spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1203 		skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1204 		spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1205 		tmp = skb_queue_len(&pending);
1206 		buffered += tmp - count;
1207 	}
1208 
1209 	ieee80211_add_pending_skbs(local, &pending);
1210 
1211 	/* now we're no longer in the deliver code */
1212 	clear_sta_flag(sta, WLAN_STA_PS_DELIVER);
1213 
1214 	/* The station might have polled and then woken up before we responded,
1215 	 * so clear these flags now to avoid them sticking around.
1216 	 */
1217 	clear_sta_flag(sta, WLAN_STA_PSPOLL);
1218 	clear_sta_flag(sta, WLAN_STA_UAPSD);
1219 	spin_unlock(&sta->ps_lock);
1220 
1221 	atomic_dec(&ps->num_sta_ps);
1222 
1223 	/* This station just woke up and isn't aware of our SMPS state */
1224 	if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1225 	    !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1226 					   sdata->smps_mode) &&
1227 	    sta->known_smps_mode != sdata->bss->req_smps &&
1228 	    sta_info_tx_streams(sta) != 1) {
1229 		ht_dbg(sdata,
1230 		       "%pM just woke up and MIMO capable - update SMPS\n",
1231 		       sta->sta.addr);
1232 		ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1233 					   sta->sta.addr,
1234 					   sdata->vif.bss_conf.bssid);
1235 	}
1236 
1237 	local->total_ps_buffered -= buffered;
1238 
1239 	sta_info_recalc_tim(sta);
1240 
1241 	ps_dbg(sdata,
1242 	       "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1243 	       sta->sta.addr, sta->sta.aid, filtered, buffered);
1244 
1245 	ieee80211_check_fast_xmit(sta);
1246 }
1247 
1248 static void ieee80211_send_null_response(struct sta_info *sta, int tid,
1249 					 enum ieee80211_frame_release_type reason,
1250 					 bool call_driver, bool more_data)
1251 {
1252 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1253 	struct ieee80211_local *local = sdata->local;
1254 	struct ieee80211_qos_hdr *nullfunc;
1255 	struct sk_buff *skb;
1256 	int size = sizeof(*nullfunc);
1257 	__le16 fc;
1258 	bool qos = sta->sta.wme;
1259 	struct ieee80211_tx_info *info;
1260 	struct ieee80211_chanctx_conf *chanctx_conf;
1261 
1262 	if (qos) {
1263 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1264 				 IEEE80211_STYPE_QOS_NULLFUNC |
1265 				 IEEE80211_FCTL_FROMDS);
1266 	} else {
1267 		size -= 2;
1268 		fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1269 				 IEEE80211_STYPE_NULLFUNC |
1270 				 IEEE80211_FCTL_FROMDS);
1271 	}
1272 
1273 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1274 	if (!skb)
1275 		return;
1276 
1277 	skb_reserve(skb, local->hw.extra_tx_headroom);
1278 
1279 	nullfunc = (void *) skb_put(skb, size);
1280 	nullfunc->frame_control = fc;
1281 	nullfunc->duration_id = 0;
1282 	memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1283 	memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1284 	memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1285 	nullfunc->seq_ctrl = 0;
1286 
1287 	skb->priority = tid;
1288 	skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1289 	if (qos) {
1290 		nullfunc->qos_ctrl = cpu_to_le16(tid);
1291 
1292 		if (reason == IEEE80211_FRAME_RELEASE_UAPSD) {
1293 			nullfunc->qos_ctrl |=
1294 				cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1295 			if (more_data)
1296 				nullfunc->frame_control |=
1297 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1298 		}
1299 	}
1300 
1301 	info = IEEE80211_SKB_CB(skb);
1302 
1303 	/*
1304 	 * Tell TX path to send this frame even though the
1305 	 * STA may still remain is PS mode after this frame
1306 	 * exchange. Also set EOSP to indicate this packet
1307 	 * ends the poll/service period.
1308 	 */
1309 	info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1310 		       IEEE80211_TX_STATUS_EOSP |
1311 		       IEEE80211_TX_CTL_REQ_TX_STATUS;
1312 
1313 	info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1314 
1315 	if (call_driver)
1316 		drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1317 					  reason, false);
1318 
1319 	skb->dev = sdata->dev;
1320 
1321 	rcu_read_lock();
1322 	chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1323 	if (WARN_ON(!chanctx_conf)) {
1324 		rcu_read_unlock();
1325 		kfree_skb(skb);
1326 		return;
1327 	}
1328 
1329 	info->band = chanctx_conf->def.chan->band;
1330 	ieee80211_xmit(sdata, sta, skb);
1331 	rcu_read_unlock();
1332 }
1333 
1334 static int find_highest_prio_tid(unsigned long tids)
1335 {
1336 	/* lower 3 TIDs aren't ordered perfectly */
1337 	if (tids & 0xF8)
1338 		return fls(tids) - 1;
1339 	/* TID 0 is BE just like TID 3 */
1340 	if (tids & BIT(0))
1341 		return 0;
1342 	return fls(tids) - 1;
1343 }
1344 
1345 /* Indicates if the MORE_DATA bit should be set in the last
1346  * frame obtained by ieee80211_sta_ps_get_frames.
1347  * Note that driver_release_tids is relevant only if
1348  * reason = IEEE80211_FRAME_RELEASE_PSPOLL
1349  */
1350 static bool
1351 ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs,
1352 			   enum ieee80211_frame_release_type reason,
1353 			   unsigned long driver_release_tids)
1354 {
1355 	int ac;
1356 
1357 	/* If the driver has data on more than one TID then
1358 	 * certainly there's more data if we release just a
1359 	 * single frame now (from a single TID). This will
1360 	 * only happen for PS-Poll.
1361 	 */
1362 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1363 	    hweight16(driver_release_tids) > 1)
1364 		return true;
1365 
1366 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1367 		if (ignored_acs & BIT(ac))
1368 			continue;
1369 
1370 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1371 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1372 			return true;
1373 	}
1374 
1375 	return false;
1376 }
1377 
1378 static void
1379 ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs,
1380 			    enum ieee80211_frame_release_type reason,
1381 			    struct sk_buff_head *frames,
1382 			    unsigned long *driver_release_tids)
1383 {
1384 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1385 	struct ieee80211_local *local = sdata->local;
1386 	int ac;
1387 
1388 	/* Get response frame(s) and more data bit for the last one. */
1389 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1390 		unsigned long tids;
1391 
1392 		if (ignored_acs & BIT(ac))
1393 			continue;
1394 
1395 		tids = ieee80211_tids_for_ac(ac);
1396 
1397 		/* if we already have frames from software, then we can't also
1398 		 * release from hardware queues
1399 		 */
1400 		if (skb_queue_empty(frames)) {
1401 			*driver_release_tids |=
1402 				sta->driver_buffered_tids & tids;
1403 			*driver_release_tids |= sta->txq_buffered_tids & tids;
1404 		}
1405 
1406 		if (!*driver_release_tids) {
1407 			struct sk_buff *skb;
1408 
1409 			while (n_frames > 0) {
1410 				skb = skb_dequeue(&sta->tx_filtered[ac]);
1411 				if (!skb) {
1412 					skb = skb_dequeue(
1413 						&sta->ps_tx_buf[ac]);
1414 					if (skb)
1415 						local->total_ps_buffered--;
1416 				}
1417 				if (!skb)
1418 					break;
1419 				n_frames--;
1420 				__skb_queue_tail(frames, skb);
1421 			}
1422 		}
1423 
1424 		/* If we have more frames buffered on this AC, then abort the
1425 		 * loop since we can't send more data from other ACs before
1426 		 * the buffered frames from this.
1427 		 */
1428 		if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1429 		    !skb_queue_empty(&sta->ps_tx_buf[ac]))
1430 			break;
1431 	}
1432 }
1433 
1434 static void
1435 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1436 				  int n_frames, u8 ignored_acs,
1437 				  enum ieee80211_frame_release_type reason)
1438 {
1439 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1440 	struct ieee80211_local *local = sdata->local;
1441 	unsigned long driver_release_tids = 0;
1442 	struct sk_buff_head frames;
1443 	bool more_data;
1444 
1445 	/* Service or PS-Poll period starts */
1446 	set_sta_flag(sta, WLAN_STA_SP);
1447 
1448 	__skb_queue_head_init(&frames);
1449 
1450 	ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason,
1451 				    &frames, &driver_release_tids);
1452 
1453 	more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids);
1454 
1455 	if (reason == IEEE80211_FRAME_RELEASE_PSPOLL)
1456 		driver_release_tids =
1457 			BIT(find_highest_prio_tid(driver_release_tids));
1458 
1459 	if (skb_queue_empty(&frames) && !driver_release_tids) {
1460 		int tid;
1461 
1462 		/*
1463 		 * For PS-Poll, this can only happen due to a race condition
1464 		 * when we set the TIM bit and the station notices it, but
1465 		 * before it can poll for the frame we expire it.
1466 		 *
1467 		 * For uAPSD, this is said in the standard (11.2.1.5 h):
1468 		 *	At each unscheduled SP for a non-AP STA, the AP shall
1469 		 *	attempt to transmit at least one MSDU or MMPDU, but no
1470 		 *	more than the value specified in the Max SP Length field
1471 		 *	in the QoS Capability element from delivery-enabled ACs,
1472 		 *	that are destined for the non-AP STA.
1473 		 *
1474 		 * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1475 		 */
1476 
1477 		/* This will evaluate to 1, 3, 5 or 7. */
1478 		tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1479 
1480 		ieee80211_send_null_response(sta, tid, reason, true, false);
1481 	} else if (!driver_release_tids) {
1482 		struct sk_buff_head pending;
1483 		struct sk_buff *skb;
1484 		int num = 0;
1485 		u16 tids = 0;
1486 		bool need_null = false;
1487 
1488 		skb_queue_head_init(&pending);
1489 
1490 		while ((skb = __skb_dequeue(&frames))) {
1491 			struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1492 			struct ieee80211_hdr *hdr = (void *) skb->data;
1493 			u8 *qoshdr = NULL;
1494 
1495 			num++;
1496 
1497 			/*
1498 			 * Tell TX path to send this frame even though the
1499 			 * STA may still remain is PS mode after this frame
1500 			 * exchange.
1501 			 */
1502 			info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
1503 			info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE;
1504 
1505 			/*
1506 			 * Use MoreData flag to indicate whether there are
1507 			 * more buffered frames for this STA
1508 			 */
1509 			if (more_data || !skb_queue_empty(&frames))
1510 				hdr->frame_control |=
1511 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1512 			else
1513 				hdr->frame_control &=
1514 					cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1515 
1516 			if (ieee80211_is_data_qos(hdr->frame_control) ||
1517 			    ieee80211_is_qos_nullfunc(hdr->frame_control))
1518 				qoshdr = ieee80211_get_qos_ctl(hdr);
1519 
1520 			tids |= BIT(skb->priority);
1521 
1522 			__skb_queue_tail(&pending, skb);
1523 
1524 			/* end service period after last frame or add one */
1525 			if (!skb_queue_empty(&frames))
1526 				continue;
1527 
1528 			if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1529 				/* for PS-Poll, there's only one frame */
1530 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1531 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1532 				break;
1533 			}
1534 
1535 			/* For uAPSD, things are a bit more complicated. If the
1536 			 * last frame has a QoS header (i.e. is a QoS-data or
1537 			 * QoS-nulldata frame) then just set the EOSP bit there
1538 			 * and be done.
1539 			 * If the frame doesn't have a QoS header (which means
1540 			 * it should be a bufferable MMPDU) then we can't set
1541 			 * the EOSP bit in the QoS header; add a QoS-nulldata
1542 			 * frame to the list to send it after the MMPDU.
1543 			 *
1544 			 * Note that this code is only in the mac80211-release
1545 			 * code path, we assume that the driver will not buffer
1546 			 * anything but QoS-data frames, or if it does, will
1547 			 * create the QoS-nulldata frame by itself if needed.
1548 			 *
1549 			 * Cf. 802.11-2012 10.2.1.10 (c).
1550 			 */
1551 			if (qoshdr) {
1552 				*qoshdr |= IEEE80211_QOS_CTL_EOSP;
1553 
1554 				info->flags |= IEEE80211_TX_STATUS_EOSP |
1555 					       IEEE80211_TX_CTL_REQ_TX_STATUS;
1556 			} else {
1557 				/* The standard isn't completely clear on this
1558 				 * as it says the more-data bit should be set
1559 				 * if there are more BUs. The QoS-Null frame
1560 				 * we're about to send isn't buffered yet, we
1561 				 * only create it below, but let's pretend it
1562 				 * was buffered just in case some clients only
1563 				 * expect more-data=0 when eosp=1.
1564 				 */
1565 				hdr->frame_control |=
1566 					cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1567 				need_null = true;
1568 				num++;
1569 			}
1570 			break;
1571 		}
1572 
1573 		drv_allow_buffered_frames(local, sta, tids, num,
1574 					  reason, more_data);
1575 
1576 		ieee80211_add_pending_skbs(local, &pending);
1577 
1578 		if (need_null)
1579 			ieee80211_send_null_response(
1580 				sta, find_highest_prio_tid(tids),
1581 				reason, false, false);
1582 
1583 		sta_info_recalc_tim(sta);
1584 	} else {
1585 		unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1586 		int tid;
1587 
1588 		/*
1589 		 * We need to release a frame that is buffered somewhere in the
1590 		 * driver ... it'll have to handle that.
1591 		 * Note that the driver also has to check the number of frames
1592 		 * on the TIDs we're releasing from - if there are more than
1593 		 * n_frames it has to set the more-data bit (if we didn't ask
1594 		 * it to set it anyway due to other buffered frames); if there
1595 		 * are fewer than n_frames it has to make sure to adjust that
1596 		 * to allow the service period to end properly.
1597 		 */
1598 		drv_release_buffered_frames(local, sta, driver_release_tids,
1599 					    n_frames, reason, more_data);
1600 
1601 		/*
1602 		 * Note that we don't recalculate the TIM bit here as it would
1603 		 * most likely have no effect at all unless the driver told us
1604 		 * that the TID(s) became empty before returning here from the
1605 		 * release function.
1606 		 * Either way, however, when the driver tells us that the TID(s)
1607 		 * became empty or we find that a txq became empty, we'll do the
1608 		 * TIM recalculation.
1609 		 */
1610 
1611 		if (!sta->sta.txq[0])
1612 			return;
1613 
1614 		for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1615 			struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1616 
1617 			if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1618 				continue;
1619 
1620 			sta_info_recalc_tim(sta);
1621 			break;
1622 		}
1623 	}
1624 }
1625 
1626 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1627 {
1628 	u8 ignore_for_response = sta->sta.uapsd_queues;
1629 
1630 	/*
1631 	 * If all ACs are delivery-enabled then we should reply
1632 	 * from any of them, if only some are enabled we reply
1633 	 * only from the non-enabled ones.
1634 	 */
1635 	if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1636 		ignore_for_response = 0;
1637 
1638 	ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1639 					  IEEE80211_FRAME_RELEASE_PSPOLL);
1640 }
1641 
1642 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1643 {
1644 	int n_frames = sta->sta.max_sp;
1645 	u8 delivery_enabled = sta->sta.uapsd_queues;
1646 
1647 	/*
1648 	 * If we ever grow support for TSPEC this might happen if
1649 	 * the TSPEC update from hostapd comes in between a trigger
1650 	 * frame setting WLAN_STA_UAPSD in the RX path and this
1651 	 * actually getting called.
1652 	 */
1653 	if (!delivery_enabled)
1654 		return;
1655 
1656 	switch (sta->sta.max_sp) {
1657 	case 1:
1658 		n_frames = 2;
1659 		break;
1660 	case 2:
1661 		n_frames = 4;
1662 		break;
1663 	case 3:
1664 		n_frames = 6;
1665 		break;
1666 	case 0:
1667 		/* XXX: what is a good value? */
1668 		n_frames = 128;
1669 		break;
1670 	}
1671 
1672 	ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1673 					  IEEE80211_FRAME_RELEASE_UAPSD);
1674 }
1675 
1676 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1677 			       struct ieee80211_sta *pubsta, bool block)
1678 {
1679 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1680 
1681 	trace_api_sta_block_awake(sta->local, pubsta, block);
1682 
1683 	if (block) {
1684 		set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1685 		ieee80211_clear_fast_xmit(sta);
1686 		return;
1687 	}
1688 
1689 	if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1690 		return;
1691 
1692 	if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
1693 		set_sta_flag(sta, WLAN_STA_PS_DELIVER);
1694 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1695 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1696 	} else if (test_sta_flag(sta, WLAN_STA_PSPOLL) ||
1697 		   test_sta_flag(sta, WLAN_STA_UAPSD)) {
1698 		/* must be asleep in this case */
1699 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1700 		ieee80211_queue_work(hw, &sta->drv_deliver_wk);
1701 	} else {
1702 		clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1703 		ieee80211_check_fast_xmit(sta);
1704 	}
1705 }
1706 EXPORT_SYMBOL(ieee80211_sta_block_awake);
1707 
1708 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
1709 {
1710 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1711 	struct ieee80211_local *local = sta->local;
1712 
1713 	trace_api_eosp(local, pubsta);
1714 
1715 	clear_sta_flag(sta, WLAN_STA_SP);
1716 }
1717 EXPORT_SYMBOL(ieee80211_sta_eosp);
1718 
1719 void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid)
1720 {
1721 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1722 	enum ieee80211_frame_release_type reason;
1723 	bool more_data;
1724 
1725 	trace_api_send_eosp_nullfunc(sta->local, pubsta, tid);
1726 
1727 	reason = IEEE80211_FRAME_RELEASE_UAPSD;
1728 	more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues,
1729 					       reason, 0);
1730 
1731 	ieee80211_send_null_response(sta, tid, reason, false, more_data);
1732 }
1733 EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc);
1734 
1735 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1736 				u8 tid, bool buffered)
1737 {
1738 	struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1739 
1740 	if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1741 		return;
1742 
1743 	trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1744 
1745 	if (buffered)
1746 		set_bit(tid, &sta->driver_buffered_tids);
1747 	else
1748 		clear_bit(tid, &sta->driver_buffered_tids);
1749 
1750 	sta_info_recalc_tim(sta);
1751 }
1752 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1753 
1754 int sta_info_move_state(struct sta_info *sta,
1755 			enum ieee80211_sta_state new_state)
1756 {
1757 	might_sleep();
1758 
1759 	if (sta->sta_state == new_state)
1760 		return 0;
1761 
1762 	/* check allowed transitions first */
1763 
1764 	switch (new_state) {
1765 	case IEEE80211_STA_NONE:
1766 		if (sta->sta_state != IEEE80211_STA_AUTH)
1767 			return -EINVAL;
1768 		break;
1769 	case IEEE80211_STA_AUTH:
1770 		if (sta->sta_state != IEEE80211_STA_NONE &&
1771 		    sta->sta_state != IEEE80211_STA_ASSOC)
1772 			return -EINVAL;
1773 		break;
1774 	case IEEE80211_STA_ASSOC:
1775 		if (sta->sta_state != IEEE80211_STA_AUTH &&
1776 		    sta->sta_state != IEEE80211_STA_AUTHORIZED)
1777 			return -EINVAL;
1778 		break;
1779 	case IEEE80211_STA_AUTHORIZED:
1780 		if (sta->sta_state != IEEE80211_STA_ASSOC)
1781 			return -EINVAL;
1782 		break;
1783 	default:
1784 		WARN(1, "invalid state %d", new_state);
1785 		return -EINVAL;
1786 	}
1787 
1788 	sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1789 		sta->sta.addr, new_state);
1790 
1791 	/*
1792 	 * notify the driver before the actual changes so it can
1793 	 * fail the transition
1794 	 */
1795 	if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1796 		int err = drv_sta_state(sta->local, sta->sdata, sta,
1797 					sta->sta_state, new_state);
1798 		if (err)
1799 			return err;
1800 	}
1801 
1802 	/* reflect the change in all state variables */
1803 
1804 	switch (new_state) {
1805 	case IEEE80211_STA_NONE:
1806 		if (sta->sta_state == IEEE80211_STA_AUTH)
1807 			clear_bit(WLAN_STA_AUTH, &sta->_flags);
1808 		break;
1809 	case IEEE80211_STA_AUTH:
1810 		if (sta->sta_state == IEEE80211_STA_NONE)
1811 			set_bit(WLAN_STA_AUTH, &sta->_flags);
1812 		else if (sta->sta_state == IEEE80211_STA_ASSOC)
1813 			clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1814 		break;
1815 	case IEEE80211_STA_ASSOC:
1816 		if (sta->sta_state == IEEE80211_STA_AUTH) {
1817 			set_bit(WLAN_STA_ASSOC, &sta->_flags);
1818 		} else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1819 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1820 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1821 			     !sta->sdata->u.vlan.sta))
1822 				atomic_dec(&sta->sdata->bss->num_mcast_sta);
1823 			clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1824 			ieee80211_clear_fast_xmit(sta);
1825 		}
1826 		break;
1827 	case IEEE80211_STA_AUTHORIZED:
1828 		if (sta->sta_state == IEEE80211_STA_ASSOC) {
1829 			if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1830 			    (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1831 			     !sta->sdata->u.vlan.sta))
1832 				atomic_inc(&sta->sdata->bss->num_mcast_sta);
1833 			set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1834 			ieee80211_check_fast_xmit(sta);
1835 		}
1836 		break;
1837 	default:
1838 		break;
1839 	}
1840 
1841 	sta->sta_state = new_state;
1842 
1843 	return 0;
1844 }
1845 
1846 u8 sta_info_tx_streams(struct sta_info *sta)
1847 {
1848 	struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1849 	u8 rx_streams;
1850 
1851 	if (!sta->sta.ht_cap.ht_supported)
1852 		return 1;
1853 
1854 	if (sta->sta.vht_cap.vht_supported) {
1855 		int i;
1856 		u16 tx_mcs_map =
1857 			le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1858 
1859 		for (i = 7; i >= 0; i--)
1860 			if ((tx_mcs_map & (0x3 << (i * 2))) !=
1861 			    IEEE80211_VHT_MCS_NOT_SUPPORTED)
1862 				return i + 1;
1863 	}
1864 
1865 	if (ht_cap->mcs.rx_mask[3])
1866 		rx_streams = 4;
1867 	else if (ht_cap->mcs.rx_mask[2])
1868 		rx_streams = 3;
1869 	else if (ht_cap->mcs.rx_mask[1])
1870 		rx_streams = 2;
1871 	else
1872 		rx_streams = 1;
1873 
1874 	if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1875 		return rx_streams;
1876 
1877 	return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1878 			>> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1879 }
1880 
1881 static void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo)
1882 {
1883 	rinfo->flags = 0;
1884 
1885 	if (sta->rx_stats.last_rate_flag & RX_FLAG_HT) {
1886 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
1887 		rinfo->mcs = sta->rx_stats.last_rate_idx;
1888 	} else if (sta->rx_stats.last_rate_flag & RX_FLAG_VHT) {
1889 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
1890 		rinfo->nss = sta->rx_stats.last_rate_vht_nss;
1891 		rinfo->mcs = sta->rx_stats.last_rate_idx;
1892 	} else {
1893 		struct ieee80211_supported_band *sband;
1894 		int shift = ieee80211_vif_get_shift(&sta->sdata->vif);
1895 		u16 brate;
1896 
1897 		sband = sta->local->hw.wiphy->bands[
1898 				ieee80211_get_sdata_band(sta->sdata)];
1899 		brate = sband->bitrates[sta->rx_stats.last_rate_idx].bitrate;
1900 		rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift);
1901 	}
1902 
1903 	if (sta->rx_stats.last_rate_flag & RX_FLAG_SHORT_GI)
1904 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
1905 
1906 	if (sta->rx_stats.last_rate_flag & RX_FLAG_5MHZ)
1907 		rinfo->bw = RATE_INFO_BW_5;
1908 	else if (sta->rx_stats.last_rate_flag & RX_FLAG_10MHZ)
1909 		rinfo->bw = RATE_INFO_BW_10;
1910 	else if (sta->rx_stats.last_rate_flag & RX_FLAG_40MHZ)
1911 		rinfo->bw = RATE_INFO_BW_40;
1912 	else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_80MHZ)
1913 		rinfo->bw = RATE_INFO_BW_80;
1914 	else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_160MHZ)
1915 		rinfo->bw = RATE_INFO_BW_160;
1916 	else
1917 		rinfo->bw = RATE_INFO_BW_20;
1918 }
1919 
1920 void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
1921 {
1922 	struct ieee80211_sub_if_data *sdata = sta->sdata;
1923 	struct ieee80211_local *local = sdata->local;
1924 	struct rate_control_ref *ref = NULL;
1925 	u32 thr = 0;
1926 	int i, ac;
1927 
1928 	if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL))
1929 		ref = local->rate_ctrl;
1930 
1931 	sinfo->generation = sdata->local->sta_generation;
1932 
1933 	/* do before driver, so beacon filtering drivers have a
1934 	 * chance to e.g. just add the number of filtered beacons
1935 	 * (or just modify the value entirely, of course)
1936 	 */
1937 	if (sdata->vif.type == NL80211_IFTYPE_STATION)
1938 		sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal;
1939 
1940 	drv_sta_statistics(local, sdata, &sta->sta, sinfo);
1941 
1942 	sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) |
1943 			 BIT(NL80211_STA_INFO_STA_FLAGS) |
1944 			 BIT(NL80211_STA_INFO_BSS_PARAM) |
1945 			 BIT(NL80211_STA_INFO_CONNECTED_TIME) |
1946 			 BIT(NL80211_STA_INFO_RX_DROP_MISC);
1947 
1948 	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1949 		sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count;
1950 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS);
1951 	}
1952 
1953 	sinfo->connected_time = ktime_get_seconds() - sta->last_connected;
1954 	sinfo->inactive_time =
1955 		jiffies_to_msecs(jiffies - sta->rx_stats.last_rx);
1956 
1957 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) |
1958 			       BIT(NL80211_STA_INFO_TX_BYTES)))) {
1959 		sinfo->tx_bytes = 0;
1960 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1961 			sinfo->tx_bytes += sta->tx_stats.bytes[ac];
1962 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
1963 	}
1964 
1965 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) {
1966 		sinfo->tx_packets = 0;
1967 		for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1968 			sinfo->tx_packets += sta->tx_stats.packets[ac];
1969 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
1970 	}
1971 
1972 	if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) |
1973 			       BIT(NL80211_STA_INFO_RX_BYTES)))) {
1974 		sinfo->rx_bytes = sta->rx_stats.bytes;
1975 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
1976 	}
1977 
1978 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) {
1979 		sinfo->rx_packets = sta->rx_stats.packets;
1980 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
1981 	}
1982 
1983 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) {
1984 		sinfo->tx_retries = sta->status_stats.retry_count;
1985 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES);
1986 	}
1987 
1988 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) {
1989 		sinfo->tx_failed = sta->status_stats.retry_failed;
1990 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
1991 	}
1992 
1993 	sinfo->rx_dropped_misc = sta->rx_stats.dropped;
1994 
1995 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1996 	    !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) {
1997 		sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) |
1998 				 BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
1999 		sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif);
2000 	}
2001 
2002 	if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) ||
2003 	    ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) {
2004 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) {
2005 			sinfo->signal = (s8)sta->rx_stats.last_signal;
2006 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
2007 		}
2008 
2009 		if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) {
2010 			sinfo->signal_avg =
2011 				-ewma_signal_read(&sta->rx_stats.avg_signal);
2012 			sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
2013 		}
2014 	}
2015 
2016 	if (sta->rx_stats.chains &&
2017 	    !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2018 			       BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) {
2019 		sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) |
2020 				 BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG);
2021 
2022 		sinfo->chains = sta->rx_stats.chains;
2023 		for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
2024 			sinfo->chain_signal[i] =
2025 				sta->rx_stats.chain_signal_last[i];
2026 			sinfo->chain_signal_avg[i] =
2027 				-ewma_signal_read(&sta->rx_stats.chain_signal_avg[i]);
2028 		}
2029 	}
2030 
2031 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) {
2032 		sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate,
2033 				     &sinfo->txrate);
2034 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
2035 	}
2036 
2037 	if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) {
2038 		sta_set_rate_info_rx(sta, &sinfo->rxrate);
2039 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE);
2040 	}
2041 
2042 	sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS);
2043 	for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) {
2044 		struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i];
2045 
2046 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) {
2047 			tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU);
2048 			tidstats->rx_msdu = sta->rx_stats.msdu[i];
2049 		}
2050 
2051 		if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) {
2052 			tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU);
2053 			tidstats->tx_msdu = sta->tx_stats.msdu[i];
2054 		}
2055 
2056 		if (!(tidstats->filled &
2057 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) &&
2058 		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2059 			tidstats->filled |=
2060 				BIT(NL80211_TID_STATS_TX_MSDU_RETRIES);
2061 			tidstats->tx_msdu_retries =
2062 				sta->status_stats.msdu_retries[i];
2063 		}
2064 
2065 		if (!(tidstats->filled &
2066 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) &&
2067 		    ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) {
2068 			tidstats->filled |=
2069 				BIT(NL80211_TID_STATS_TX_MSDU_FAILED);
2070 			tidstats->tx_msdu_failed =
2071 				sta->status_stats.msdu_failed[i];
2072 		}
2073 	}
2074 
2075 	if (ieee80211_vif_is_mesh(&sdata->vif)) {
2076 #ifdef CONFIG_MAC80211_MESH
2077 		sinfo->filled |= BIT(NL80211_STA_INFO_LLID) |
2078 				 BIT(NL80211_STA_INFO_PLID) |
2079 				 BIT(NL80211_STA_INFO_PLINK_STATE) |
2080 				 BIT(NL80211_STA_INFO_LOCAL_PM) |
2081 				 BIT(NL80211_STA_INFO_PEER_PM) |
2082 				 BIT(NL80211_STA_INFO_NONPEER_PM);
2083 
2084 		sinfo->llid = sta->mesh->llid;
2085 		sinfo->plid = sta->mesh->plid;
2086 		sinfo->plink_state = sta->mesh->plink_state;
2087 		if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
2088 			sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET);
2089 			sinfo->t_offset = sta->mesh->t_offset;
2090 		}
2091 		sinfo->local_pm = sta->mesh->local_pm;
2092 		sinfo->peer_pm = sta->mesh->peer_pm;
2093 		sinfo->nonpeer_pm = sta->mesh->nonpeer_pm;
2094 #endif
2095 	}
2096 
2097 	sinfo->bss_param.flags = 0;
2098 	if (sdata->vif.bss_conf.use_cts_prot)
2099 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2100 	if (sdata->vif.bss_conf.use_short_preamble)
2101 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2102 	if (sdata->vif.bss_conf.use_short_slot)
2103 		sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2104 	sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period;
2105 	sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int;
2106 
2107 	sinfo->sta_flags.set = 0;
2108 	sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2109 				BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2110 				BIT(NL80211_STA_FLAG_WME) |
2111 				BIT(NL80211_STA_FLAG_MFP) |
2112 				BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2113 				BIT(NL80211_STA_FLAG_ASSOCIATED) |
2114 				BIT(NL80211_STA_FLAG_TDLS_PEER);
2115 	if (test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2116 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2117 	if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE))
2118 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
2119 	if (sta->sta.wme)
2120 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME);
2121 	if (test_sta_flag(sta, WLAN_STA_MFP))
2122 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP);
2123 	if (test_sta_flag(sta, WLAN_STA_AUTH))
2124 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2125 	if (test_sta_flag(sta, WLAN_STA_ASSOC))
2126 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2127 	if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
2128 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2129 
2130 	/* check if the driver has a SW RC implementation */
2131 	if (ref && ref->ops->get_expected_throughput)
2132 		thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv);
2133 	else
2134 		thr = drv_get_expected_throughput(local, &sta->sta);
2135 
2136 	if (thr != 0) {
2137 		sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT);
2138 		sinfo->expected_throughput = thr;
2139 	}
2140 }
2141