root/net/mac80211/sta_info.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sta_info_hash_del
  2. __cleanup_single_sta
  3. cleanup_single_sta
  4. sta_info_hash_lookup
  5. sta_info_get
  6. sta_info_get_bss
  7. sta_info_get_by_idx
  8. sta_info_free
  9. sta_info_hash_add
  10. sta_deliver_ps_frames
  11. sta_prepare_rate_control
  12. sta_info_alloc
  13. sta_info_insert_check
  14. sta_info_insert_drv_state
  15. ieee80211_recalc_p2p_go_ps_allowed
  16. sta_info_insert_finish
  17. sta_info_insert_rcu
  18. sta_info_insert
  19. __bss_tim_set
  20. __bss_tim_clear
  21. __bss_tim_get
  22. ieee80211_tids_for_ac
  23. __sta_info_recalc_tim
  24. sta_info_recalc_tim
  25. sta_info_buffer_expired
  26. sta_info_cleanup_expire_buffered_ac
  27. sta_info_cleanup_expire_buffered
  28. __sta_info_destroy_part1
  29. __sta_info_destroy_part2
  30. __sta_info_destroy
  31. sta_info_destroy_addr
  32. sta_info_destroy_addr_bss
  33. sta_info_cleanup
  34. sta_info_init
  35. sta_info_stop
  36. __sta_info_flush
  37. ieee80211_sta_expire
  38. ieee80211_find_sta_by_ifaddr
  39. ieee80211_find_sta
  40. ieee80211_sta_ps_deliver_wakeup
  41. ieee80211_send_null_response
  42. find_highest_prio_tid
  43. ieee80211_sta_ps_more_data
  44. ieee80211_sta_ps_get_frames
  45. ieee80211_sta_ps_deliver_response
  46. ieee80211_sta_ps_deliver_poll_response
  47. ieee80211_sta_ps_deliver_uapsd
  48. ieee80211_sta_block_awake
  49. ieee80211_sta_eosp
  50. ieee80211_send_eosp_nullfunc
  51. ieee80211_sta_set_buffered
  52. ieee80211_sta_register_airtime
  53. sta_info_move_state
  54. sta_info_tx_streams
  55. sta_get_last_rx_stats
  56. sta_stats_decode_rate
  57. sta_set_rate_info_rx
  58. sta_set_tidstats
  59. sta_get_stats_bytes
  60. sta_set_sinfo
  61. sta_get_expected_throughput
  62. ieee80211_sta_last_active
  63. sta_update_codel_params
  64. ieee80211_sta_set_expected_throughput

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

/* [<][>][^][v][top][bottom][index][help] */