File Index
Files and directories
Sign in
FILE

sta_info.c

File preview with syntax highlighting.

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