1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2f0706e82SJiri Benc /* 3f0706e82SJiri Benc * Copyright 2002-2005, Instant802 Networks, Inc. 4f0706e82SJiri Benc * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> 5d98ad83eSJohannes Berg * Copyright 2013-2014 Intel Mobile Communications GmbH 6dcba665bSJohannes Berg * Copyright (C) 2015 - 2017 Intel Deutschland GmbH 7*52b4810bSIlan Peer * Copyright (C) 2018-2020 Intel Corporation 8f0706e82SJiri Benc */ 9f0706e82SJiri Benc 10f0706e82SJiri Benc #include <linux/module.h> 11f0706e82SJiri Benc #include <linux/init.h> 12888d04dfSFelix Fietkau #include <linux/etherdevice.h> 13f0706e82SJiri Benc #include <linux/netdevice.h> 14f0706e82SJiri Benc #include <linux/types.h> 15f0706e82SJiri Benc #include <linux/slab.h> 16f0706e82SJiri Benc #include <linux/skbuff.h> 17f0706e82SJiri Benc #include <linux/if_arp.h> 180d174406SJohannes Berg #include <linux/timer.h> 19d0709a65SJohannes Berg #include <linux/rtnetlink.h> 20f0706e82SJiri Benc 21484a54c2SToke Høiland-Jørgensen #include <net/codel.h> 22f0706e82SJiri Benc #include <net/mac80211.h> 23f0706e82SJiri Benc #include "ieee80211_i.h" 2424487981SJohannes Berg #include "driver-ops.h" 252c8dccc7SJohannes Berg #include "rate.h" 26f0706e82SJiri Benc #include "sta_info.h" 27e9f207f0SJiri Benc #include "debugfs_sta.h" 28ee385855SLuis Carlos Cobo #include "mesh.h" 29ce662b44SJohannes Berg #include "wme.h" 30f0706e82SJiri Benc 31d0709a65SJohannes Berg /** 32d0709a65SJohannes Berg * DOC: STA information lifetime rules 33d0709a65SJohannes Berg * 34d0709a65SJohannes Berg * STA info structures (&struct sta_info) are managed in a hash table 35d0709a65SJohannes Berg * for faster lookup and a list for iteration. They are managed using 36d0709a65SJohannes Berg * RCU, i.e. access to the list and hash table is protected by RCU. 37d0709a65SJohannes Berg * 3834e89507SJohannes Berg * Upon allocating a STA info structure with sta_info_alloc(), the caller 3934e89507SJohannes Berg * owns that structure. It must then insert it into the hash table using 4034e89507SJohannes Berg * either sta_info_insert() or sta_info_insert_rcu(); only in the latter 4134e89507SJohannes Berg * case (which acquires an rcu read section but must not be called from 4234e89507SJohannes Berg * within one) will the pointer still be valid after the call. Note that 4334e89507SJohannes Berg * the caller may not do much with the STA info before inserting it, in 4434e89507SJohannes Berg * particular, it may not start any mesh peer link management or add 4534e89507SJohannes Berg * encryption keys. 4693e5deb1SJohannes Berg * 4793e5deb1SJohannes Berg * When the insertion fails (sta_info_insert()) returns non-zero), the 4893e5deb1SJohannes Berg * structure will have been freed by sta_info_insert()! 49d0709a65SJohannes Berg * 5034e89507SJohannes Berg * Station entries are added by mac80211 when you establish a link with a 517e189a12SLuis R. Rodriguez * peer. This means different things for the different type of interfaces 527e189a12SLuis R. Rodriguez * we support. For a regular station this mean we add the AP sta when we 5325985edcSLucas De Marchi * receive an association response from the AP. For IBSS this occurs when 5434e89507SJohannes Berg * get to know about a peer on the same IBSS. For WDS we add the sta for 5525985edcSLucas De Marchi * the peer immediately upon device open. When using AP mode we add stations 5634e89507SJohannes Berg * for each respective station upon request from userspace through nl80211. 577e189a12SLuis R. Rodriguez * 5834e89507SJohannes Berg * In order to remove a STA info structure, various sta_info_destroy_*() 5934e89507SJohannes Berg * calls are available. 60d0709a65SJohannes Berg * 6134e89507SJohannes Berg * There is no concept of ownership on a STA entry, each structure is 6234e89507SJohannes Berg * owned by the global hash table/list until it is removed. All users of 6334e89507SJohannes Berg * the structure need to be RCU protected so that the structure won't be 6434e89507SJohannes Berg * freed before they are done using it. 65d0709a65SJohannes Berg */ 66f0706e82SJiri Benc 677bedd0cfSJohannes Berg static const struct rhashtable_params sta_rht_params = { 687bedd0cfSJohannes Berg .nelem_hint = 3, /* start small */ 69caf22d31SJohannes Berg .automatic_shrinking = true, 707bedd0cfSJohannes Berg .head_offset = offsetof(struct sta_info, hash_node), 71ac100ce5SJohannes Berg .key_offset = offsetof(struct sta_info, addr), 727bedd0cfSJohannes Berg .key_len = ETH_ALEN, 73ebd82b39SJohannes Berg .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE, 747bedd0cfSJohannes Berg }; 757bedd0cfSJohannes Berg 764d33960bSJohannes Berg /* Caller must hold local->sta_mtx */ 77be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local, 78f0706e82SJiri Benc struct sta_info *sta) 79f0706e82SJiri Benc { 8083e7e4ceSHerbert Xu return rhltable_remove(&local->sta_hash, &sta->hash_node, 817bedd0cfSJohannes Berg sta_rht_params); 82f0706e82SJiri Benc } 83f0706e82SJiri Benc 845108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta) 85b22cfcfcSEliad Peller { 86b22cfcfcSEliad Peller int ac, i; 87b22cfcfcSEliad Peller struct tid_ampdu_tx *tid_tx; 88b22cfcfcSEliad Peller struct ieee80211_sub_if_data *sdata = sta->sdata; 89b22cfcfcSEliad Peller struct ieee80211_local *local = sdata->local; 90d012a605SMarco Porsch struct ps_data *ps; 91b22cfcfcSEliad Peller 92e3685e03SJohannes Berg if (test_sta_flag(sta, WLAN_STA_PS_STA) || 935ac2e350SJohannes Berg test_sta_flag(sta, WLAN_STA_PS_DRIVER) || 945ac2e350SJohannes Berg test_sta_flag(sta, WLAN_STA_PS_DELIVER)) { 95d012a605SMarco Porsch if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 96d012a605SMarco Porsch sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 97d012a605SMarco Porsch ps = &sdata->bss->ps; 983f52b7e3SMarco Porsch else if (ieee80211_vif_is_mesh(&sdata->vif)) 993f52b7e3SMarco Porsch ps = &sdata->u.mesh.ps; 100d012a605SMarco Porsch else 101d012a605SMarco Porsch return; 102b22cfcfcSEliad Peller 103b22cfcfcSEliad Peller clear_sta_flag(sta, WLAN_STA_PS_STA); 104e3685e03SJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 1055ac2e350SJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DELIVER); 106b22cfcfcSEliad Peller 107d012a605SMarco Porsch atomic_dec(&ps->num_sta_ps); 108b22cfcfcSEliad Peller } 109b22cfcfcSEliad Peller 110ba8c3d6fSFelix Fietkau if (sta->sta.txq[0]) { 111ba8c3d6fSFelix Fietkau for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) { 112adf8ed01SJohannes Berg struct txq_info *txqi; 113adf8ed01SJohannes Berg 114adf8ed01SJohannes Berg if (!sta->sta.txq[i]) 115adf8ed01SJohannes Berg continue; 116adf8ed01SJohannes Berg 117adf8ed01SJohannes Berg txqi = to_txq_info(sta->sta.txq[i]); 118ba8c3d6fSFelix Fietkau 119fa962b92SMichal Kazior ieee80211_txq_purge(local, txqi); 120ba8c3d6fSFelix Fietkau } 121ba8c3d6fSFelix Fietkau } 122ba8c3d6fSFelix Fietkau 123b22cfcfcSEliad Peller for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 124b22cfcfcSEliad Peller local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]); 1251f98ab7fSFelix Fietkau ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]); 1261f98ab7fSFelix Fietkau ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]); 127b22cfcfcSEliad Peller } 128b22cfcfcSEliad Peller 12945b5028eSThomas Pedersen if (ieee80211_vif_is_mesh(&sdata->vif)) 13045b5028eSThomas Pedersen mesh_sta_cleanup(sta); 131b22cfcfcSEliad Peller 1325ac2e350SJohannes Berg cancel_work_sync(&sta->drv_deliver_wk); 133b22cfcfcSEliad Peller 134b22cfcfcSEliad Peller /* 135b22cfcfcSEliad Peller * Destroy aggregation state here. It would be nice to wait for the 136b22cfcfcSEliad Peller * driver to finish aggregation stop and then clean up, but for now 137b22cfcfcSEliad Peller * drivers have to handle aggregation stop being requested, followed 138b22cfcfcSEliad Peller * directly by station destruction. 139b22cfcfcSEliad Peller */ 1405a306f58SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 141661eb381SJohannes Berg kfree(sta->ampdu_mlme.tid_start_tx[i]); 142b22cfcfcSEliad Peller tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]); 143b22cfcfcSEliad Peller if (!tid_tx) 144b22cfcfcSEliad Peller continue; 1451f98ab7fSFelix Fietkau ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending); 146b22cfcfcSEliad Peller kfree(tid_tx); 147b22cfcfcSEliad Peller } 1485108ca82SJohannes Berg } 149b22cfcfcSEliad Peller 1505108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta) 1515108ca82SJohannes Berg { 1525108ca82SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1535108ca82SJohannes Berg struct ieee80211_local *local = sdata->local; 1545108ca82SJohannes Berg 1555108ca82SJohannes Berg __cleanup_single_sta(sta); 156b22cfcfcSEliad Peller sta_info_free(local, sta); 157b22cfcfcSEliad Peller } 158b22cfcfcSEliad Peller 15983e7e4ceSHerbert Xu struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local, 16083e7e4ceSHerbert Xu const u8 *addr) 16183e7e4ceSHerbert Xu { 16283e7e4ceSHerbert Xu return rhltable_lookup(&local->sta_hash, addr, sta_rht_params); 16383e7e4ceSHerbert Xu } 16483e7e4ceSHerbert Xu 165d0709a65SJohannes Berg /* protected by RCU */ 166abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata, 167abe60632SJohannes Berg const u8 *addr) 16843ba7e95SJohannes Berg { 169abe60632SJohannes Berg struct ieee80211_local *local = sdata->local; 17083e7e4ceSHerbert Xu struct rhlist_head *tmp; 17160f4b626SJohannes Berg struct sta_info *sta; 17243ba7e95SJohannes Berg 17360f4b626SJohannes Berg rcu_read_lock(); 17483e7e4ceSHerbert Xu for_each_sta_info(local, addr, sta, tmp) { 17560f4b626SJohannes Berg if (sta->sdata == sdata) { 17660f4b626SJohannes Berg rcu_read_unlock(); 17760f4b626SJohannes Berg /* this is safe as the caller must already hold 17860f4b626SJohannes Berg * another rcu read section or the mutex 17960f4b626SJohannes Berg */ 18060f4b626SJohannes Berg return sta; 18160f4b626SJohannes Berg } 18260f4b626SJohannes Berg } 18360f4b626SJohannes Berg rcu_read_unlock(); 18460f4b626SJohannes Berg return NULL; 18543ba7e95SJohannes Berg } 18643ba7e95SJohannes Berg 1870e5ded5aSFelix Fietkau /* 1880e5ded5aSFelix Fietkau * Get sta info either from the specified interface 1890e5ded5aSFelix Fietkau * or from one of its vlans 1900e5ded5aSFelix Fietkau */ 1910e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata, 1920e5ded5aSFelix Fietkau const u8 *addr) 1930e5ded5aSFelix Fietkau { 1940e5ded5aSFelix Fietkau struct ieee80211_local *local = sdata->local; 19583e7e4ceSHerbert Xu struct rhlist_head *tmp; 1960e5ded5aSFelix Fietkau struct sta_info *sta; 1970e5ded5aSFelix Fietkau 1987bedd0cfSJohannes Berg rcu_read_lock(); 19983e7e4ceSHerbert Xu for_each_sta_info(local, addr, sta, tmp) { 2007bedd0cfSJohannes Berg if (sta->sdata == sdata || 2017bedd0cfSJohannes Berg (sta->sdata->bss && sta->sdata->bss == sdata->bss)) { 2027bedd0cfSJohannes Berg rcu_read_unlock(); 2037bedd0cfSJohannes Berg /* this is safe as the caller must already hold 2047bedd0cfSJohannes Berg * another rcu read section or the mutex 2057bedd0cfSJohannes Berg */ 2060e5ded5aSFelix Fietkau return sta; 2070e5ded5aSFelix Fietkau } 2087bedd0cfSJohannes Berg } 2097bedd0cfSJohannes Berg rcu_read_unlock(); 2107bedd0cfSJohannes Berg return NULL; 2117bedd0cfSJohannes Berg } 2120e5ded5aSFelix Fietkau 2135072f73cSToke Høiland-Jørgensen struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local, 2145072f73cSToke Høiland-Jørgensen const u8 *sta_addr, const u8 *vif_addr) 2155072f73cSToke Høiland-Jørgensen { 2165072f73cSToke Høiland-Jørgensen struct rhlist_head *tmp; 2175072f73cSToke Høiland-Jørgensen struct sta_info *sta; 2185072f73cSToke Høiland-Jørgensen 2195072f73cSToke Høiland-Jørgensen for_each_sta_info(local, sta_addr, sta, tmp) { 2205072f73cSToke Høiland-Jørgensen if (ether_addr_equal(vif_addr, sta->sdata->vif.addr)) 2215072f73cSToke Høiland-Jørgensen return sta; 2225072f73cSToke Høiland-Jørgensen } 2235072f73cSToke Høiland-Jørgensen 2245072f73cSToke Høiland-Jørgensen return NULL; 2255072f73cSToke Høiland-Jørgensen } 2265072f73cSToke Høiland-Jørgensen 2273b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata, 2283b53fde8SJohannes Berg int idx) 229ee385855SLuis Carlos Cobo { 2303b53fde8SJohannes Berg struct ieee80211_local *local = sdata->local; 231ee385855SLuis Carlos Cobo struct sta_info *sta; 232ee385855SLuis Carlos Cobo int i = 0; 233ee385855SLuis Carlos Cobo 234d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) { 2353b53fde8SJohannes Berg if (sdata != sta->sdata) 2362a8ca29aSLuis Carlos Cobo continue; 237ee385855SLuis Carlos Cobo if (i < idx) { 238ee385855SLuis Carlos Cobo ++i; 239ee385855SLuis Carlos Cobo continue; 240ee385855SLuis Carlos Cobo } 2412a8ca29aSLuis Carlos Cobo return sta; 242ee385855SLuis Carlos Cobo } 243ee385855SLuis Carlos Cobo 244ee385855SLuis Carlos Cobo return NULL; 245ee385855SLuis Carlos Cobo } 246f0706e82SJiri Benc 24793e5deb1SJohannes Berg /** 248d9a7ddb0SJohannes Berg * sta_info_free - free STA 24993e5deb1SJohannes Berg * 2506ef307bcSRandy Dunlap * @local: pointer to the global information 25193e5deb1SJohannes Berg * @sta: STA info to free 25293e5deb1SJohannes Berg * 25393e5deb1SJohannes Berg * This function must undo everything done by sta_info_alloc() 254d9a7ddb0SJohannes Berg * that may happen before sta_info_insert(). It may only be 255d9a7ddb0SJohannes Berg * called when sta_info_insert() has not been attempted (and 256d9a7ddb0SJohannes Berg * if that fails, the station is freed anyway.) 25793e5deb1SJohannes Berg */ 258d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta) 25993e5deb1SJohannes Berg { 260889cbb91SJohannes Berg if (sta->rate_ctrl) 2614b7679a5SJohannes Berg rate_control_free_sta(sta); 26293e5deb1SJohannes Berg 263bdcbd8e0SJohannes Berg sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr); 26493e5deb1SJohannes Berg 265ba8c3d6fSFelix Fietkau if (sta->sta.txq[0]) 266ba8c3d6fSFelix Fietkau kfree(to_txq_info(sta->sta.txq[0])); 26753d04525SFelix Fietkau kfree(rcu_dereference_raw(sta->sta.rates)); 268433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH 269433f5bc1SJohannes Berg kfree(sta->mesh); 270433f5bc1SJohannes Berg #endif 271c9c5962bSJohannes Berg free_percpu(sta->pcpu_rx_stats); 27293e5deb1SJohannes Berg kfree(sta); 27393e5deb1SJohannes Berg } 27493e5deb1SJohannes Berg 2754d33960bSJohannes Berg /* Caller must hold local->sta_mtx */ 27662b14b24SJohannes Berg static int sta_info_hash_add(struct ieee80211_local *local, 277d0709a65SJohannes Berg struct sta_info *sta) 278f0706e82SJiri Benc { 27983e7e4ceSHerbert Xu return rhltable_insert(&local->sta_hash, &sta->hash_node, 2807bedd0cfSJohannes Berg sta_rht_params); 281f0706e82SJiri Benc } 282f0706e82SJiri Benc 2835ac2e350SJohannes Berg static void sta_deliver_ps_frames(struct work_struct *wk) 284af818581SJohannes Berg { 285af818581SJohannes Berg struct sta_info *sta; 286af818581SJohannes Berg 2875ac2e350SJohannes Berg sta = container_of(wk, struct sta_info, drv_deliver_wk); 288af818581SJohannes Berg 289af818581SJohannes Berg if (sta->dead) 290af818581SJohannes Berg return; 291af818581SJohannes Berg 29254420473SHelmut Schaa local_bh_disable(); 2935ac2e350SJohannes Berg if (!test_sta_flag(sta, WLAN_STA_PS_STA)) 294af818581SJohannes Berg ieee80211_sta_ps_deliver_wakeup(sta); 2955ac2e350SJohannes Berg else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) 296af818581SJohannes Berg ieee80211_sta_ps_deliver_poll_response(sta); 2975ac2e350SJohannes Berg else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) 29847086fc5SJohannes Berg ieee80211_sta_ps_deliver_uapsd(sta); 299ce662b44SJohannes Berg local_bh_enable(); 300af818581SJohannes Berg } 301af818581SJohannes Berg 302af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local, 303af65cd96SJohannes Berg struct sta_info *sta, gfp_t gfp) 304af65cd96SJohannes Berg { 30530686bf7SJohannes Berg if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) 306af65cd96SJohannes Berg return 0; 307af65cd96SJohannes Berg 308889cbb91SJohannes Berg sta->rate_ctrl = local->rate_ctrl; 309af65cd96SJohannes Berg sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, 31035c347acSJohannes Berg sta, gfp); 311889cbb91SJohannes Berg if (!sta->rate_ctrl_priv) 312af65cd96SJohannes Berg return -ENOMEM; 313af65cd96SJohannes Berg 314af65cd96SJohannes Berg return 0; 315af65cd96SJohannes Berg } 316af65cd96SJohannes Berg 31773651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, 31856544160SJohannes Berg const u8 *addr, gfp_t gfp) 319f0706e82SJiri Benc { 320d0709a65SJohannes Berg struct ieee80211_local *local = sdata->local; 321ba8c3d6fSFelix Fietkau struct ieee80211_hw *hw = &local->hw; 322f0706e82SJiri Benc struct sta_info *sta; 32316c5f15cSRon Rindjunsky int i; 324f0706e82SJiri Benc 325ba8c3d6fSFelix Fietkau sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp); 326f0706e82SJiri Benc if (!sta) 32773651ee6SJohannes Berg return NULL; 328f0706e82SJiri Benc 329c9c5962bSJohannes Berg if (ieee80211_hw_check(hw, USES_RSS)) { 330c9c5962bSJohannes Berg sta->pcpu_rx_stats = 33195f3ce6aSSara Sharon alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp); 332c9c5962bSJohannes Berg if (!sta->pcpu_rx_stats) 333c9c5962bSJohannes Berg goto free; 334c9c5962bSJohannes Berg } 335c9c5962bSJohannes Berg 33607346f81SJohannes Berg spin_lock_init(&sta->lock); 3371d147bfaSEmmanuel Grumbach spin_lock_init(&sta->ps_lock); 3385ac2e350SJohannes Berg INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames); 33967c282c0SJohannes Berg INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work); 340a93e3644SJohannes Berg mutex_init(&sta->ampdu_mlme.mtx); 34187f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH 342433f5bc1SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) { 343433f5bc1SJohannes Berg sta->mesh = kzalloc(sizeof(*sta->mesh), gfp); 344433f5bc1SJohannes Berg if (!sta->mesh) 345433f5bc1SJohannes Berg goto free; 3464c02d62fSKees Cook sta->mesh->plink_sta = sta; 347433f5bc1SJohannes Berg spin_lock_init(&sta->mesh->plink_lock); 34887f59c70SThomas Pedersen if (ieee80211_vif_is_mesh(&sdata->vif) && 34987f59c70SThomas Pedersen !sdata->u.mesh.user_mpm) 3504c02d62fSKees Cook timer_setup(&sta->mesh->plink_timer, mesh_plink_timer, 3514c02d62fSKees Cook 0); 352433f5bc1SJohannes Berg sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE; 353433f5bc1SJohannes Berg } 35487f59c70SThomas Pedersen #endif 35507346f81SJohannes Berg 356ac100ce5SJohannes Berg memcpy(sta->addr, addr, ETH_ALEN); 35717741cdcSJohannes Berg memcpy(sta->sta.addr, addr, ETH_ALEN); 358480dd46bSMaxim Altshul sta->sta.max_rx_aggregation_subframes = 359480dd46bSMaxim Altshul local->hw.max_rx_aggregation_subframes; 360480dd46bSMaxim Altshul 36196fc6efbSAlexander Wetzel /* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only. 36296fc6efbSAlexander Wetzel * The Tx path starts to use a key as soon as the key slot ptk_idx 36396fc6efbSAlexander Wetzel * references to is not NULL. To not use the initial Rx-only key 36496fc6efbSAlexander Wetzel * prematurely for Tx initialize ptk_idx to an impossible PTK keyid 36596fc6efbSAlexander Wetzel * which always will refer to a NULL key. 36696fc6efbSAlexander Wetzel */ 36796fc6efbSAlexander Wetzel BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX); 36896fc6efbSAlexander Wetzel sta->ptk_idx = INVALID_PTK_KEYIDX; 36996fc6efbSAlexander Wetzel 370d0709a65SJohannes Berg sta->local = local; 371d0709a65SJohannes Berg sta->sdata = sdata; 372e5a9f8d0SJohannes Berg sta->rx_stats.last_rx = jiffies; 373f0706e82SJiri Benc 3740f9c5a61SJohannes Berg u64_stats_init(&sta->rx_stats.syncp); 3750f9c5a61SJohannes Berg 37671ec375cSJohannes Berg sta->sta_state = IEEE80211_STA_NONE; 37771ec375cSJohannes Berg 378b6da911bSLiad Kaufman /* Mark TID as unreserved */ 379b6da911bSLiad Kaufman sta->reserved_tid = IEEE80211_TID_UNRESERVED; 380b6da911bSLiad Kaufman 38184b00607SArnd Bergmann sta->last_connected = ktime_get_seconds(); 3820be6ed13SJohannes Berg ewma_signal_init(&sta->rx_stats_avg.signal); 383cc60dbbfSBalaji Pothunoori ewma_avg_signal_init(&sta->status_stats.avg_ack_signal); 3840be6ed13SJohannes Berg for (i = 0; i < ARRAY_SIZE(sta->rx_stats_avg.chain_signal); i++) 3850be6ed13SJohannes Berg ewma_signal_init(&sta->rx_stats_avg.chain_signal[i]); 386541a45a1SBruno Randolf 387ba8c3d6fSFelix Fietkau if (local->ops->wake_tx_queue) { 388ba8c3d6fSFelix Fietkau void *txq_data; 389ba8c3d6fSFelix Fietkau int size = sizeof(struct txq_info) + 390ba8c3d6fSFelix Fietkau ALIGN(hw->txq_data_size, sizeof(void *)); 391ba8c3d6fSFelix Fietkau 392ba8c3d6fSFelix Fietkau txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp); 393ba8c3d6fSFelix Fietkau if (!txq_data) 394ba8c3d6fSFelix Fietkau goto free; 395ba8c3d6fSFelix Fietkau 396ba8c3d6fSFelix Fietkau for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) { 397ba8c3d6fSFelix Fietkau struct txq_info *txq = txq_data + i * size; 398ba8c3d6fSFelix Fietkau 399adf8ed01SJohannes Berg /* might not do anything for the bufferable MMPDU TXQ */ 400fa962b92SMichal Kazior ieee80211_txq_init(sdata, sta, txq, i); 401abfbc3afSJohannes Berg } 402ba8c3d6fSFelix Fietkau } 403ba8c3d6fSFelix Fietkau 404ba8c3d6fSFelix Fietkau if (sta_prepare_rate_control(local, sta, gfp)) 405ba8c3d6fSFelix Fietkau goto free_txq; 406f0706e82SJiri Benc 407b4809e94SToke Høiland-Jørgensen sta->airtime_weight = IEEE80211_DEFAULT_AIRTIME_WEIGHT; 408b4809e94SToke Høiland-Jørgensen 409948d887dSJohannes Berg for (i = 0; i < IEEE80211_NUM_ACS; i++) { 410948d887dSJohannes Berg skb_queue_head_init(&sta->ps_tx_buf[i]); 411948d887dSJohannes Berg skb_queue_head_init(&sta->tx_filtered[i]); 412b4809e94SToke Høiland-Jørgensen sta->airtime[i].deficit = sta->airtime_weight; 4133ace10f5SKan Yan atomic_set(&sta->airtime[i].aql_tx_pending, 0); 4143ace10f5SKan Yan sta->airtime[i].aql_limit_low = local->aql_txq_limit_low[i]; 4153ace10f5SKan Yan sta->airtime[i].aql_limit_high = local->aql_txq_limit_high[i]; 416948d887dSJohannes Berg } 41773651ee6SJohannes Berg 4185a306f58SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) 4194be929beSAlexey Dobriyan sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX); 420cccaec98SSenthil Balasubramanian 421bd718fc1SJohannes Berg for (i = 0; i < NUM_NL80211_BANDS; i++) { 422bd718fc1SJohannes Berg u32 mandatory = 0; 423bd718fc1SJohannes Berg int r; 424bd718fc1SJohannes Berg 425bd718fc1SJohannes Berg if (!hw->wiphy->bands[i]) 426bd718fc1SJohannes Berg continue; 427bd718fc1SJohannes Berg 428bd718fc1SJohannes Berg switch (i) { 429bd718fc1SJohannes Berg case NL80211_BAND_2GHZ: 430bd718fc1SJohannes Berg /* 431bd718fc1SJohannes Berg * We use both here, even if we cannot really know for 432bd718fc1SJohannes Berg * sure the station will support both, but the only use 433bd718fc1SJohannes Berg * for this is when we don't know anything yet and send 434bd718fc1SJohannes Berg * management frames, and then we'll pick the lowest 435bd718fc1SJohannes Berg * possible rate anyway. 436bd718fc1SJohannes Berg * If we don't include _G here, we cannot find a rate 437bd718fc1SJohannes Berg * in P2P, and thus trigger the WARN_ONCE() in rate.c 438bd718fc1SJohannes Berg */ 439bd718fc1SJohannes Berg mandatory = IEEE80211_RATE_MANDATORY_B | 440bd718fc1SJohannes Berg IEEE80211_RATE_MANDATORY_G; 441bd718fc1SJohannes Berg break; 442bd718fc1SJohannes Berg case NL80211_BAND_5GHZ: 443bd718fc1SJohannes Berg mandatory = IEEE80211_RATE_MANDATORY_A; 444bd718fc1SJohannes Berg break; 445bd718fc1SJohannes Berg case NL80211_BAND_60GHZ: 446bd718fc1SJohannes Berg WARN_ON(1); 447bd718fc1SJohannes Berg mandatory = 0; 448bd718fc1SJohannes Berg break; 449bd718fc1SJohannes Berg } 450bd718fc1SJohannes Berg 451bd718fc1SJohannes Berg for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) { 452bd718fc1SJohannes Berg struct ieee80211_rate *rate; 453bd718fc1SJohannes Berg 454bd718fc1SJohannes Berg rate = &hw->wiphy->bands[i]->bitrates[r]; 455bd718fc1SJohannes Berg 456bd718fc1SJohannes Berg if (!(rate->flags & mandatory)) 457bd718fc1SJohannes Berg continue; 458bd718fc1SJohannes Berg sta->sta.supp_rates[i] |= BIT(r); 459bd718fc1SJohannes Berg } 460bd718fc1SJohannes Berg } 461bd718fc1SJohannes Berg 462af0ed69bSJohannes Berg sta->sta.smps_mode = IEEE80211_SMPS_OFF; 463687da132SEmmanuel Grumbach if (sdata->vif.type == NL80211_IFTYPE_AP || 464687da132SEmmanuel Grumbach sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 46521a8e9ddSMohammed Shafi Shajakhan struct ieee80211_supported_band *sband; 46621a8e9ddSMohammed Shafi Shajakhan u8 smps; 46721a8e9ddSMohammed Shafi Shajakhan 46821a8e9ddSMohammed Shafi Shajakhan sband = ieee80211_get_sband(sdata); 46921a8e9ddSMohammed Shafi Shajakhan if (!sband) 47021a8e9ddSMohammed Shafi Shajakhan goto free_txq; 47121a8e9ddSMohammed Shafi Shajakhan 47221a8e9ddSMohammed Shafi Shajakhan smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 473687da132SEmmanuel Grumbach IEEE80211_HT_CAP_SM_PS_SHIFT; 474687da132SEmmanuel Grumbach /* 475687da132SEmmanuel Grumbach * Assume that hostapd advertises our caps in the beacon and 476687da132SEmmanuel Grumbach * this is the known_smps_mode for a station that just assciated 477687da132SEmmanuel Grumbach */ 478687da132SEmmanuel Grumbach switch (smps) { 479687da132SEmmanuel Grumbach case WLAN_HT_SMPS_CONTROL_DISABLED: 480687da132SEmmanuel Grumbach sta->known_smps_mode = IEEE80211_SMPS_OFF; 481687da132SEmmanuel Grumbach break; 482687da132SEmmanuel Grumbach case WLAN_HT_SMPS_CONTROL_STATIC: 483687da132SEmmanuel Grumbach sta->known_smps_mode = IEEE80211_SMPS_STATIC; 484687da132SEmmanuel Grumbach break; 485687da132SEmmanuel Grumbach case WLAN_HT_SMPS_CONTROL_DYNAMIC: 486687da132SEmmanuel Grumbach sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC; 487687da132SEmmanuel Grumbach break; 488687da132SEmmanuel Grumbach default: 489687da132SEmmanuel Grumbach WARN_ON(1); 490687da132SEmmanuel Grumbach } 491687da132SEmmanuel Grumbach } 492af0ed69bSJohannes Berg 4936e0456b5SFelix Fietkau sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA; 4946e0456b5SFelix Fietkau 495484a54c2SToke Høiland-Jørgensen sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD; 496484a54c2SToke Høiland-Jørgensen sta->cparams.target = MS2TIME(20); 497484a54c2SToke Høiland-Jørgensen sta->cparams.interval = MS2TIME(100); 498484a54c2SToke Høiland-Jørgensen sta->cparams.ecn = true; 499484a54c2SToke Høiland-Jørgensen 500bdcbd8e0SJohannes Berg sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr); 501ef04a297SJohannes Berg 502abfbc3afSJohannes Berg return sta; 503ba8c3d6fSFelix Fietkau 504ba8c3d6fSFelix Fietkau free_txq: 505ba8c3d6fSFelix Fietkau if (sta->sta.txq[0]) 506ba8c3d6fSFelix Fietkau kfree(to_txq_info(sta->sta.txq[0])); 507ba8c3d6fSFelix Fietkau free: 508d78d9ee9SSara Sharon free_percpu(sta->pcpu_rx_stats); 509433f5bc1SJohannes Berg #ifdef CONFIG_MAC80211_MESH 510433f5bc1SJohannes Berg kfree(sta->mesh); 511433f5bc1SJohannes Berg #endif 512ba8c3d6fSFelix Fietkau kfree(sta); 513ba8c3d6fSFelix Fietkau return NULL; 51473651ee6SJohannes Berg } 51573651ee6SJohannes Berg 5168c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta) 51734e89507SJohannes Berg { 51834e89507SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 51934e89507SJohannes Berg 52003e4497eSJohannes Berg /* 52103e4497eSJohannes Berg * Can't be a WARN_ON because it can be triggered through a race: 52203e4497eSJohannes Berg * something inserts a STA (on one CPU) without holding the RTNL 52303e4497eSJohannes Berg * and another CPU turns off the net device. 52403e4497eSJohannes Berg */ 5258c71df7aSGuy Eilam if (unlikely(!ieee80211_sdata_running(sdata))) 5268c71df7aSGuy Eilam return -ENETDOWN; 52703e4497eSJohannes Berg 528b203ca39SJoe Perches if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) || 5298c71df7aSGuy Eilam is_multicast_ether_addr(sta->sta.addr))) 5308c71df7aSGuy Eilam return -EINVAL; 5318c71df7aSGuy Eilam 53283e7e4ceSHerbert Xu /* The RCU read lock is required by rhashtable due to 53383e7e4ceSHerbert Xu * asynchronous resize/rehash. We also require the mutex 53483e7e4ceSHerbert Xu * for correctness. 53531104891SJohannes Berg */ 53631104891SJohannes Berg rcu_read_lock(); 53731104891SJohannes Berg lockdep_assert_held(&sdata->local->sta_mtx); 53831104891SJohannes Berg if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) && 53931104891SJohannes Berg ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) { 54031104891SJohannes Berg rcu_read_unlock(); 54131104891SJohannes Berg return -ENOTUNIQ; 54231104891SJohannes Berg } 54331104891SJohannes Berg rcu_read_unlock(); 54431104891SJohannes Berg 5458c71df7aSGuy Eilam return 0; 54693e5deb1SJohannes Berg } 54744213b5eSJohannes Berg 548f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local, 549f09603a2SJohannes Berg struct ieee80211_sub_if_data *sdata, 550f09603a2SJohannes Berg struct sta_info *sta) 551f09603a2SJohannes Berg { 552f09603a2SJohannes Berg enum ieee80211_sta_state state; 553f09603a2SJohannes Berg int err = 0; 554f09603a2SJohannes Berg 555f09603a2SJohannes Berg for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) { 556f09603a2SJohannes Berg err = drv_sta_state(local, sdata, sta, state, state + 1); 557f09603a2SJohannes Berg if (err) 558f09603a2SJohannes Berg break; 559f09603a2SJohannes Berg } 560f09603a2SJohannes Berg 561f09603a2SJohannes Berg if (!err) { 562a4ec45a4SJohannes Berg /* 563a4ec45a4SJohannes Berg * Drivers using legacy sta_add/sta_remove callbacks only 564a4ec45a4SJohannes Berg * get uploaded set to true after sta_add is called. 565a4ec45a4SJohannes Berg */ 566a4ec45a4SJohannes Berg if (!local->ops->sta_add) 567f09603a2SJohannes Berg sta->uploaded = true; 568f09603a2SJohannes Berg return 0; 569f09603a2SJohannes Berg } 570f09603a2SJohannes Berg 571f09603a2SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { 572bdcbd8e0SJohannes Berg sdata_info(sdata, 573bdcbd8e0SJohannes Berg "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n", 574bdcbd8e0SJohannes Berg sta->sta.addr, state + 1, err); 575f09603a2SJohannes Berg err = 0; 576f09603a2SJohannes Berg } 577f09603a2SJohannes Berg 578f09603a2SJohannes Berg /* unwind on error */ 579f09603a2SJohannes Berg for (; state > IEEE80211_STA_NOTEXIST; state--) 580f09603a2SJohannes Berg WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1)); 581f09603a2SJohannes Berg 582f09603a2SJohannes Berg return err; 583f09603a2SJohannes Berg } 584f09603a2SJohannes Berg 585d405fd8cSGregory Greenman static void 586d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata) 587d405fd8cSGregory Greenman { 588d405fd8cSGregory Greenman struct ieee80211_local *local = sdata->local; 589d405fd8cSGregory Greenman bool allow_p2p_go_ps = sdata->vif.p2p; 590d405fd8cSGregory Greenman struct sta_info *sta; 591d405fd8cSGregory Greenman 592d405fd8cSGregory Greenman rcu_read_lock(); 593d405fd8cSGregory Greenman list_for_each_entry_rcu(sta, &local->sta_list, list) { 594d405fd8cSGregory Greenman if (sdata != sta->sdata || 595d405fd8cSGregory Greenman !test_sta_flag(sta, WLAN_STA_ASSOC)) 596d405fd8cSGregory Greenman continue; 597d405fd8cSGregory Greenman if (!sta->sta.support_p2p_ps) { 598d405fd8cSGregory Greenman allow_p2p_go_ps = false; 599d405fd8cSGregory Greenman break; 600d405fd8cSGregory Greenman } 601d405fd8cSGregory Greenman } 602d405fd8cSGregory Greenman rcu_read_unlock(); 603d405fd8cSGregory Greenman 604d405fd8cSGregory Greenman if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) { 605d405fd8cSGregory Greenman sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps; 606d405fd8cSGregory Greenman ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_P2P_PS); 607d405fd8cSGregory Greenman } 608d405fd8cSGregory Greenman } 609d405fd8cSGregory Greenman 61034e89507SJohannes Berg /* 6118c71df7aSGuy Eilam * should be called with sta_mtx locked 6128c71df7aSGuy Eilam * this function replaces the mutex lock 6138c71df7aSGuy Eilam * with a RCU lock 6148c71df7aSGuy Eilam */ 6154d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU) 6168c71df7aSGuy Eilam { 6178c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 6188c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 6190c2e3842SKoen Vandeputte struct station_info *sinfo = NULL; 6208c71df7aSGuy Eilam int err = 0; 6218c71df7aSGuy Eilam 6228c71df7aSGuy Eilam lockdep_assert_held(&local->sta_mtx); 6238c71df7aSGuy Eilam 6247852e361SJohannes Berg /* check if STA exists already */ 6257852e361SJohannes Berg if (sta_info_get_bss(sdata, sta->sta.addr)) { 6264d33960bSJohannes Berg err = -EEXIST; 6274d33960bSJohannes Berg goto out_err; 62834e89507SJohannes Berg } 62934e89507SJohannes Berg 6300c2e3842SKoen Vandeputte sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL); 6310c2e3842SKoen Vandeputte if (!sinfo) { 6320c2e3842SKoen Vandeputte err = -ENOMEM; 6330c2e3842SKoen Vandeputte goto out_err; 6340c2e3842SKoen Vandeputte } 6350c2e3842SKoen Vandeputte 6364d33960bSJohannes Berg local->num_sta++; 6374d33960bSJohannes Berg local->sta_generation++; 6384d33960bSJohannes Berg smp_mb(); 6394d33960bSJohannes Berg 6405108ca82SJohannes Berg /* simplify things and don't accept BA sessions yet */ 6415108ca82SJohannes Berg set_sta_flag(sta, WLAN_STA_BLOCK_BA); 6425108ca82SJohannes Berg 6434d33960bSJohannes Berg /* make the station visible */ 64462b14b24SJohannes Berg err = sta_info_hash_add(local, sta); 64562b14b24SJohannes Berg if (err) 64662b14b24SJohannes Berg goto out_drop_sta; 6474d33960bSJohannes Berg 6482bad7748SArik Nemtsov list_add_tail_rcu(&sta->list, &local->sta_list); 64983d5cc01SJohannes Berg 6505108ca82SJohannes Berg /* notify driver */ 6515108ca82SJohannes Berg err = sta_info_insert_drv_state(local, sdata, sta); 6525108ca82SJohannes Berg if (err) 6535108ca82SJohannes Berg goto out_remove; 6545108ca82SJohannes Berg 65583d5cc01SJohannes Berg set_sta_flag(sta, WLAN_STA_INSERTED); 656d405fd8cSGregory Greenman 657d405fd8cSGregory Greenman if (sta->sta_state >= IEEE80211_STA_ASSOC) { 658d405fd8cSGregory Greenman ieee80211_recalc_min_chandef(sta->sdata); 659d405fd8cSGregory Greenman if (!sta->sta.support_p2p_ps) 660d405fd8cSGregory Greenman ieee80211_recalc_p2p_go_ps_allowed(sta->sdata); 661d405fd8cSGregory Greenman } 662d405fd8cSGregory Greenman 6635108ca82SJohannes Berg /* accept BA sessions now */ 6645108ca82SJohannes Berg clear_sta_flag(sta, WLAN_STA_BLOCK_BA); 6654d33960bSJohannes Berg 6664d33960bSJohannes Berg ieee80211_sta_debugfs_add(sta); 6674d33960bSJohannes Berg rate_control_add_sta_debugfs(sta); 6684d33960bSJohannes Berg 6690ef049dcSArnd Bergmann sinfo->generation = local->sta_generation; 6700ef049dcSArnd Bergmann cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL); 6710ef049dcSArnd Bergmann kfree(sinfo); 672d0709a65SJohannes Berg 673bdcbd8e0SJohannes Berg sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr); 674f0706e82SJiri Benc 67534e89507SJohannes Berg /* move reference to rcu-protected */ 67634e89507SJohannes Berg rcu_read_lock(); 67734e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 678e9f207f0SJiri Benc 67973651ee6SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 68073651ee6SJohannes Berg mesh_accept_plinks_update(sdata); 68173651ee6SJohannes Berg 68273651ee6SJohannes Berg return 0; 6835108ca82SJohannes Berg out_remove: 6845108ca82SJohannes Berg sta_info_hash_del(local, sta); 6855108ca82SJohannes Berg list_del_rcu(&sta->list); 68662b14b24SJohannes Berg out_drop_sta: 6875108ca82SJohannes Berg local->num_sta--; 6885108ca82SJohannes Berg synchronize_net(); 6895108ca82SJohannes Berg __cleanup_single_sta(sta); 6904d33960bSJohannes Berg out_err: 6914d33960bSJohannes Berg mutex_unlock(&local->sta_mtx); 692ea32f065SSudip Mukherjee kfree(sinfo); 6934d33960bSJohannes Berg rcu_read_lock(); 6944d33960bSJohannes Berg return err; 6958c71df7aSGuy Eilam } 6968c71df7aSGuy Eilam 6978c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) 6988c71df7aSGuy Eilam { 6998c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 700308f7fcfSZhao, Gang int err; 7018c71df7aSGuy Eilam 7024d33960bSJohannes Berg might_sleep(); 7034d33960bSJohannes Berg 70431104891SJohannes Berg mutex_lock(&local->sta_mtx); 70531104891SJohannes Berg 7068c71df7aSGuy Eilam err = sta_info_insert_check(sta); 7078c71df7aSGuy Eilam if (err) { 70831104891SJohannes Berg mutex_unlock(&local->sta_mtx); 7098c71df7aSGuy Eilam rcu_read_lock(); 7108c71df7aSGuy Eilam goto out_free; 7118c71df7aSGuy Eilam } 7128c71df7aSGuy Eilam 7134d33960bSJohannes Berg err = sta_info_insert_finish(sta); 7148c71df7aSGuy Eilam if (err) 715004c872eSJohannes Berg goto out_free; 716d0709a65SJohannes Berg 717004c872eSJohannes Berg return 0; 71893e5deb1SJohannes Berg out_free: 719d9a7ddb0SJohannes Berg sta_info_free(local, sta); 72093e5deb1SJohannes Berg return err; 721f0706e82SJiri Benc } 722f0706e82SJiri Benc 72334e89507SJohannes Berg int sta_info_insert(struct sta_info *sta) 72434e89507SJohannes Berg { 72534e89507SJohannes Berg int err = sta_info_insert_rcu(sta); 72634e89507SJohannes Berg 72734e89507SJohannes Berg rcu_read_unlock(); 72834e89507SJohannes Berg 72934e89507SJohannes Berg return err; 73034e89507SJohannes Berg } 73134e89507SJohannes Berg 732d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id) 733004c872eSJohannes Berg { 734004c872eSJohannes Berg /* 735004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 736004c872eSJohannes Berg * so this line may not be changed to use the __set_bit() format. 737004c872eSJohannes Berg */ 738d012a605SMarco Porsch tim[id / 8] |= (1 << (id % 8)); 739004c872eSJohannes Berg } 740004c872eSJohannes Berg 741d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id) 742004c872eSJohannes Berg { 743004c872eSJohannes Berg /* 744004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 745004c872eSJohannes Berg * so this line may not be changed to use the __clear_bit() format. 746004c872eSJohannes Berg */ 747d012a605SMarco Porsch tim[id / 8] &= ~(1 << (id % 8)); 748004c872eSJohannes Berg } 749004c872eSJohannes Berg 7503d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id) 7513d5839b6SIlan Peer { 7523d5839b6SIlan Peer /* 7533d5839b6SIlan Peer * This format has been mandated by the IEEE specifications, 7543d5839b6SIlan Peer * so this line may not be changed to use the test_bit() format. 7553d5839b6SIlan Peer */ 7563d5839b6SIlan Peer return tim[id / 8] & (1 << (id % 8)); 7573d5839b6SIlan Peer } 7583d5839b6SIlan Peer 759948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac) 760004c872eSJohannes Berg { 761948d887dSJohannes Berg /* If we ever support TIDs > 7, this obviously needs to be adjusted */ 762948d887dSJohannes Berg switch (ac) { 763948d887dSJohannes Berg case IEEE80211_AC_VO: 764948d887dSJohannes Berg return BIT(6) | BIT(7); 765948d887dSJohannes Berg case IEEE80211_AC_VI: 766948d887dSJohannes Berg return BIT(4) | BIT(5); 767948d887dSJohannes Berg case IEEE80211_AC_BE: 768948d887dSJohannes Berg return BIT(0) | BIT(3); 769948d887dSJohannes Berg case IEEE80211_AC_BK: 770948d887dSJohannes Berg return BIT(1) | BIT(2); 771948d887dSJohannes Berg default: 772948d887dSJohannes Berg WARN_ON(1); 773948d887dSJohannes Berg return 0; 774d0709a65SJohannes Berg } 775004c872eSJohannes Berg } 776004c872eSJohannes Berg 7779b7a86f3SJohannes Berg static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending) 778004c872eSJohannes Berg { 779c868cb35SJohannes Berg struct ieee80211_local *local = sta->local; 780d012a605SMarco Porsch struct ps_data *ps; 781948d887dSJohannes Berg bool indicate_tim = false; 782948d887dSJohannes Berg u8 ignore_for_tim = sta->sta.uapsd_queues; 783948d887dSJohannes Berg int ac; 784a69bd8e6SBob Copeland u16 id = sta->sta.aid; 785004c872eSJohannes Berg 786d012a605SMarco Porsch if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 787d012a605SMarco Porsch sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 788c868cb35SJohannes Berg if (WARN_ON_ONCE(!sta->sdata->bss)) 789c868cb35SJohannes Berg return; 7903e122be0SJohannes Berg 791d012a605SMarco Porsch ps = &sta->sdata->bss->ps; 7923f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH 7933f52b7e3SMarco Porsch } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) { 7943f52b7e3SMarco Porsch ps = &sta->sdata->u.mesh.ps; 7953f52b7e3SMarco Porsch #endif 796d012a605SMarco Porsch } else { 797d012a605SMarco Porsch return; 798d012a605SMarco Porsch } 799d012a605SMarco Porsch 800c868cb35SJohannes Berg /* No need to do anything if the driver does all */ 801d98937f4SEmmanuel Grumbach if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim) 802c868cb35SJohannes Berg return; 803004c872eSJohannes Berg 804c868cb35SJohannes Berg if (sta->dead) 805c868cb35SJohannes Berg goto done; 8063e122be0SJohannes Berg 807948d887dSJohannes Berg /* 808948d887dSJohannes Berg * If all ACs are delivery-enabled then we should build 809948d887dSJohannes Berg * the TIM bit for all ACs anyway; if only some are then 810948d887dSJohannes Berg * we ignore those and build the TIM bit using only the 811948d887dSJohannes Berg * non-enabled ones. 812948d887dSJohannes Berg */ 813948d887dSJohannes Berg if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1) 814948d887dSJohannes Berg ignore_for_tim = 0; 815948d887dSJohannes Berg 8169b7a86f3SJohannes Berg if (ignore_pending) 8179b7a86f3SJohannes Berg ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1; 8189b7a86f3SJohannes Berg 819948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 820948d887dSJohannes Berg unsigned long tids; 821948d887dSJohannes Berg 822f438ceb8SEmmanuel Grumbach if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac]) 823948d887dSJohannes Berg continue; 824948d887dSJohannes Berg 825948d887dSJohannes Berg indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) || 826948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac]); 827948d887dSJohannes Berg if (indicate_tim) 828948d887dSJohannes Berg break; 829948d887dSJohannes Berg 830948d887dSJohannes Berg tids = ieee80211_tids_for_ac(ac); 831948d887dSJohannes Berg 832948d887dSJohannes Berg indicate_tim |= 833948d887dSJohannes Berg sta->driver_buffered_tids & tids; 834ba8c3d6fSFelix Fietkau indicate_tim |= 835ba8c3d6fSFelix Fietkau sta->txq_buffered_tids & tids; 836004c872eSJohannes Berg } 837004c872eSJohannes Berg 838c868cb35SJohannes Berg done: 83965f704a5SJohannes Berg spin_lock_bh(&local->tim_lock); 840004c872eSJohannes Berg 8413d5839b6SIlan Peer if (indicate_tim == __bss_tim_get(ps->tim, id)) 8423d5839b6SIlan Peer goto out_unlock; 8433d5839b6SIlan Peer 844948d887dSJohannes Berg if (indicate_tim) 845d012a605SMarco Porsch __bss_tim_set(ps->tim, id); 846c868cb35SJohannes Berg else 847d012a605SMarco Porsch __bss_tim_clear(ps->tim, id); 8483e122be0SJohannes Berg 8499b7a86f3SJohannes Berg if (local->ops->set_tim && !WARN_ON(sta->dead)) { 850c868cb35SJohannes Berg local->tim_in_locked_section = true; 851948d887dSJohannes Berg drv_set_tim(local, &sta->sta, indicate_tim); 852c868cb35SJohannes Berg local->tim_in_locked_section = false; 853004c872eSJohannes Berg } 854004c872eSJohannes Berg 8553d5839b6SIlan Peer out_unlock: 85665f704a5SJohannes Berg spin_unlock_bh(&local->tim_lock); 857004c872eSJohannes Berg } 858004c872eSJohannes Berg 8599b7a86f3SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta) 8609b7a86f3SJohannes Berg { 8619b7a86f3SJohannes Berg __sta_info_recalc_tim(sta, false); 8629b7a86f3SJohannes Berg } 8639b7a86f3SJohannes Berg 864cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb) 865f0706e82SJiri Benc { 866e039fa4aSJohannes Berg struct ieee80211_tx_info *info; 867f0706e82SJiri Benc int timeout; 868f0706e82SJiri Benc 869f0706e82SJiri Benc if (!skb) 870cd0b8d89SJohannes Berg return false; 871f0706e82SJiri Benc 872e039fa4aSJohannes Berg info = IEEE80211_SKB_CB(skb); 873f0706e82SJiri Benc 874f0706e82SJiri Benc /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ 87557c4d7b4SJohannes Berg timeout = (sta->listen_interval * 87657c4d7b4SJohannes Berg sta->sdata->vif.bss_conf.beacon_int * 87757c4d7b4SJohannes Berg 32 / 15625) * HZ; 878f0706e82SJiri Benc if (timeout < STA_TX_BUFFER_EXPIRE) 879f0706e82SJiri Benc timeout = STA_TX_BUFFER_EXPIRE; 880e039fa4aSJohannes Berg return time_after(jiffies, info->control.jiffies + timeout); 881f0706e82SJiri Benc } 882f0706e82SJiri Benc 883f0706e82SJiri Benc 884948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local, 885948d887dSJohannes Berg struct sta_info *sta, int ac) 886f0706e82SJiri Benc { 887f0706e82SJiri Benc unsigned long flags; 888f0706e82SJiri Benc struct sk_buff *skb; 889f0706e82SJiri Benc 89060750397SJohannes Berg /* 89160750397SJohannes Berg * First check for frames that should expire on the filtered 89260750397SJohannes Berg * queue. Frames here were rejected by the driver and are on 89360750397SJohannes Berg * a separate queue to avoid reordering with normal PS-buffered 89460750397SJohannes Berg * frames. They also aren't accounted for right now in the 89560750397SJohannes Berg * total_ps_buffered counter. 89660750397SJohannes Berg */ 897f0706e82SJiri Benc for (;;) { 898948d887dSJohannes Berg spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 899948d887dSJohannes Berg skb = skb_peek(&sta->tx_filtered[ac]); 90057c4d7b4SJohannes Berg if (sta_info_buffer_expired(sta, skb)) 901948d887dSJohannes Berg skb = __skb_dequeue(&sta->tx_filtered[ac]); 902836341a7SJohannes Berg else 903f0706e82SJiri Benc skb = NULL; 904948d887dSJohannes Berg spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 905f0706e82SJiri Benc 90660750397SJohannes Berg /* 90760750397SJohannes Berg * Frames are queued in order, so if this one 90860750397SJohannes Berg * hasn't expired yet we can stop testing. If 90960750397SJohannes Berg * we actually reached the end of the queue we 91060750397SJohannes Berg * also need to stop, of course. 91160750397SJohannes Berg */ 91260750397SJohannes Berg if (!skb) 91360750397SJohannes Berg break; 914d4fa14cdSFelix Fietkau ieee80211_free_txskb(&local->hw, skb); 91560750397SJohannes Berg } 91660750397SJohannes Berg 91760750397SJohannes Berg /* 91860750397SJohannes Berg * Now also check the normal PS-buffered queue, this will 91960750397SJohannes Berg * only find something if the filtered queue was emptied 92060750397SJohannes Berg * since the filtered frames are all before the normal PS 92160750397SJohannes Berg * buffered frames. 92260750397SJohannes Berg */ 923f0706e82SJiri Benc for (;;) { 924948d887dSJohannes Berg spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 925948d887dSJohannes Berg skb = skb_peek(&sta->ps_tx_buf[ac]); 926f0706e82SJiri Benc if (sta_info_buffer_expired(sta, skb)) 927948d887dSJohannes Berg skb = __skb_dequeue(&sta->ps_tx_buf[ac]); 928f0706e82SJiri Benc else 929f0706e82SJiri Benc skb = NULL; 930948d887dSJohannes Berg spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 931f0706e82SJiri Benc 93260750397SJohannes Berg /* 93360750397SJohannes Berg * frames are queued in order, so if this one 93460750397SJohannes Berg * hasn't expired yet (or we reached the end of 93560750397SJohannes Berg * the queue) we can stop testing 93660750397SJohannes Berg */ 937836341a7SJohannes Berg if (!skb) 938836341a7SJohannes Berg break; 939836341a7SJohannes Berg 940f0706e82SJiri Benc local->total_ps_buffered--; 941bdcbd8e0SJohannes Berg ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n", 942bdcbd8e0SJohannes Berg sta->sta.addr); 943d4fa14cdSFelix Fietkau ieee80211_free_txskb(&local->hw, skb); 944f0706e82SJiri Benc } 9453393a608SJuuso Oikarinen 94660750397SJohannes Berg /* 94760750397SJohannes Berg * Finally, recalculate the TIM bit for this station -- it might 94860750397SJohannes Berg * now be clear because the station was too slow to retrieve its 94960750397SJohannes Berg * frames. 95060750397SJohannes Berg */ 95160750397SJohannes Berg sta_info_recalc_tim(sta); 95260750397SJohannes Berg 95360750397SJohannes Berg /* 95460750397SJohannes Berg * Return whether there are any frames still buffered, this is 95560750397SJohannes Berg * used to check whether the cleanup timer still needs to run, 95660750397SJohannes Berg * if there are no frames we don't need to rearm the timer. 95760750397SJohannes Berg */ 958948d887dSJohannes Berg return !(skb_queue_empty(&sta->ps_tx_buf[ac]) && 959948d887dSJohannes Berg skb_queue_empty(&sta->tx_filtered[ac])); 960948d887dSJohannes Berg } 961948d887dSJohannes Berg 962948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local, 963948d887dSJohannes Berg struct sta_info *sta) 964948d887dSJohannes Berg { 965948d887dSJohannes Berg bool have_buffered = false; 966948d887dSJohannes Berg int ac; 967948d887dSJohannes Berg 9683f52b7e3SMarco Porsch /* This is only necessary for stations on BSS/MBSS interfaces */ 9693f52b7e3SMarco Porsch if (!sta->sdata->bss && 9703f52b7e3SMarco Porsch !ieee80211_vif_is_mesh(&sta->sdata->vif)) 971948d887dSJohannes Berg return false; 972948d887dSJohannes Berg 973948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 974948d887dSJohannes Berg have_buffered |= 975948d887dSJohannes Berg sta_info_cleanup_expire_buffered_ac(local, sta, ac); 976948d887dSJohannes Berg 977948d887dSJohannes Berg return have_buffered; 978f0706e82SJiri Benc } 979f0706e82SJiri Benc 980d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta) 98134e89507SJohannes Berg { 98234e89507SJohannes Berg struct ieee80211_local *local; 98334e89507SJohannes Berg struct ieee80211_sub_if_data *sdata; 9846d10e46bSJohannes Berg int ret; 98534e89507SJohannes Berg 98634e89507SJohannes Berg might_sleep(); 98734e89507SJohannes Berg 98834e89507SJohannes Berg if (!sta) 98934e89507SJohannes Berg return -ENOENT; 99034e89507SJohannes Berg 99134e89507SJohannes Berg local = sta->local; 99234e89507SJohannes Berg sdata = sta->sdata; 99334e89507SJohannes Berg 99483d5cc01SJohannes Berg lockdep_assert_held(&local->sta_mtx); 99583d5cc01SJohannes Berg 996098a6070SJohannes Berg /* 997098a6070SJohannes Berg * Before removing the station from the driver and 998098a6070SJohannes Berg * rate control, it might still start new aggregation 999098a6070SJohannes Berg * sessions -- block that to make sure the tear-down 1000098a6070SJohannes Berg * will be sufficient. 1001098a6070SJohannes Berg */ 1002c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_BLOCK_BA); 1003c82c4a80SJohannes Berg ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA); 1004098a6070SJohannes Berg 1005f59374ebSSara Sharon /* 1006f59374ebSSara Sharon * Before removing the station from the driver there might be pending 1007f59374ebSSara Sharon * rx frames on RSS queues sent prior to the disassociation - wait for 1008f59374ebSSara Sharon * all such frames to be processed. 1009f59374ebSSara Sharon */ 1010f59374ebSSara Sharon drv_sync_rx_queues(local, sta); 1011f59374ebSSara Sharon 101234e89507SJohannes Berg ret = sta_info_hash_del(local, sta); 1013b01711beSJohannes Berg if (WARN_ON(ret)) 101434e89507SJohannes Berg return ret; 101534e89507SJohannes Berg 1016a7a6bdd0SArik Nemtsov /* 1017a7a6bdd0SArik Nemtsov * for TDLS peers, make sure to return to the base channel before 1018a7a6bdd0SArik Nemtsov * removal. 1019a7a6bdd0SArik Nemtsov */ 1020a7a6bdd0SArik Nemtsov if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) { 1021a7a6bdd0SArik Nemtsov drv_tdls_cancel_channel_switch(local, sdata, &sta->sta); 1022a7a6bdd0SArik Nemtsov clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL); 1023a7a6bdd0SArik Nemtsov } 1024a7a6bdd0SArik Nemtsov 1025794454ceSArik Nemtsov list_del_rcu(&sta->list); 1026ef044763SEliad Peller sta->removed = true; 10274d33960bSJohannes Berg 10286a9d1b91SJohannes Berg drv_sta_pre_rcu_remove(local, sta->sdata, sta); 10296a9d1b91SJohannes Berg 1030a710c816SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 1031a710c816SJohannes Berg rcu_access_pointer(sdata->u.vlan.sta) == sta) 1032a710c816SJohannes Berg RCU_INIT_POINTER(sdata->u.vlan.sta, NULL); 1033a710c816SJohannes Berg 1034d778207bSJohannes Berg return 0; 1035d778207bSJohannes Berg } 1036d778207bSJohannes Berg 1037d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta) 1038d778207bSJohannes Berg { 1039d778207bSJohannes Berg struct ieee80211_local *local = sta->local; 1040d778207bSJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 10410ef049dcSArnd Bergmann struct station_info *sinfo; 1042d778207bSJohannes Berg int ret; 1043d778207bSJohannes Berg 1044d778207bSJohannes Berg /* 1045d778207bSJohannes Berg * NOTE: This assumes at least synchronize_net() was done 1046d778207bSJohannes Berg * after _part1 and before _part2! 1047d778207bSJohannes Berg */ 1048d778207bSJohannes Berg 1049d778207bSJohannes Berg might_sleep(); 1050d778207bSJohannes Berg lockdep_assert_held(&local->sta_mtx); 1051d778207bSJohannes Berg 1052c8782078SJohannes Berg /* now keys can no longer be reached */ 10536d10e46bSJohannes Berg ieee80211_free_sta_keys(local, sta); 105434e89507SJohannes Berg 10559b7a86f3SJohannes Berg /* disable TIM bit - last chance to tell driver */ 10569b7a86f3SJohannes Berg __sta_info_recalc_tim(sta, true); 10579b7a86f3SJohannes Berg 105834e89507SJohannes Berg sta->dead = true; 105934e89507SJohannes Berg 106034e89507SJohannes Berg local->num_sta--; 106134e89507SJohannes Berg local->sta_generation++; 106234e89507SJohannes Berg 106383d5cc01SJohannes Berg while (sta->sta_state > IEEE80211_STA_NONE) { 1064f09603a2SJohannes Berg ret = sta_info_move_state(sta, sta->sta_state - 1); 1065f09603a2SJohannes Berg if (ret) { 106683d5cc01SJohannes Berg WARN_ON_ONCE(1); 106783d5cc01SJohannes Berg break; 106883d5cc01SJohannes Berg } 106983d5cc01SJohannes Berg } 1070d9a7ddb0SJohannes Berg 1071f09603a2SJohannes Berg if (sta->uploaded) { 1072f09603a2SJohannes Berg ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE, 1073f09603a2SJohannes Berg IEEE80211_STA_NOTEXIST); 1074f09603a2SJohannes Berg WARN_ON_ONCE(ret != 0); 1075f09603a2SJohannes Berg } 107634e89507SJohannes Berg 1077bdcbd8e0SJohannes Berg sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr); 1078bdcbd8e0SJohannes Berg 10790ef049dcSArnd Bergmann sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL); 10800ef049dcSArnd Bergmann if (sinfo) 10810fdf1493SJohannes Berg sta_set_sinfo(sta, sinfo, true); 10820ef049dcSArnd Bergmann cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL); 10830ef049dcSArnd Bergmann kfree(sinfo); 1084ec15e68bSJouni Malinen 108534e89507SJohannes Berg ieee80211_sta_debugfs_remove(sta); 108634e89507SJohannes Berg 1087d34ba216SJohannes Berg cleanup_single_sta(sta); 1088d778207bSJohannes Berg } 1089d778207bSJohannes Berg 1090d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta) 1091d778207bSJohannes Berg { 1092d778207bSJohannes Berg int err = __sta_info_destroy_part1(sta); 1093d778207bSJohannes Berg 1094d778207bSJohannes Berg if (err) 1095d778207bSJohannes Berg return err; 1096d778207bSJohannes Berg 1097d778207bSJohannes Berg synchronize_net(); 1098d778207bSJohannes Berg 1099d778207bSJohannes Berg __sta_info_destroy_part2(sta); 110034e89507SJohannes Berg 110134e89507SJohannes Berg return 0; 110234e89507SJohannes Berg } 110334e89507SJohannes Berg 110434e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr) 110534e89507SJohannes Berg { 110634e89507SJohannes Berg struct sta_info *sta; 110734e89507SJohannes Berg int ret; 110834e89507SJohannes Berg 110934e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 11107852e361SJohannes Berg sta = sta_info_get(sdata, addr); 111134e89507SJohannes Berg ret = __sta_info_destroy(sta); 111234e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 111334e89507SJohannes Berg 111434e89507SJohannes Berg return ret; 111534e89507SJohannes Berg } 111634e89507SJohannes Berg 111734e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata, 111834e89507SJohannes Berg const u8 *addr) 111934e89507SJohannes Berg { 112034e89507SJohannes Berg struct sta_info *sta; 112134e89507SJohannes Berg int ret; 112234e89507SJohannes Berg 112334e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 11247852e361SJohannes Berg sta = sta_info_get_bss(sdata, addr); 112534e89507SJohannes Berg ret = __sta_info_destroy(sta); 112634e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 112734e89507SJohannes Berg 112834e89507SJohannes Berg return ret; 112934e89507SJohannes Berg } 1130f0706e82SJiri Benc 113134f11cd3SKees Cook static void sta_info_cleanup(struct timer_list *t) 1132f0706e82SJiri Benc { 113334f11cd3SKees Cook struct ieee80211_local *local = from_timer(local, t, sta_cleanup); 1134f0706e82SJiri Benc struct sta_info *sta; 11353393a608SJuuso Oikarinen bool timer_needed = false; 1136f0706e82SJiri Benc 1137d0709a65SJohannes Berg rcu_read_lock(); 1138d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) 11393393a608SJuuso Oikarinen if (sta_info_cleanup_expire_buffered(local, sta)) 11403393a608SJuuso Oikarinen timer_needed = true; 1141d0709a65SJohannes Berg rcu_read_unlock(); 1142f0706e82SJiri Benc 11435bb644a0SJohannes Berg if (local->quiescing) 11445bb644a0SJohannes Berg return; 11455bb644a0SJohannes Berg 11463393a608SJuuso Oikarinen if (!timer_needed) 11473393a608SJuuso Oikarinen return; 11483393a608SJuuso Oikarinen 114926d59535SJohannes Berg mod_timer(&local->sta_cleanup, 115026d59535SJohannes Berg round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); 1151f0706e82SJiri Benc } 1152f0706e82SJiri Benc 11537bedd0cfSJohannes Berg int sta_info_init(struct ieee80211_local *local) 11547bedd0cfSJohannes Berg { 11557bedd0cfSJohannes Berg int err; 11567bedd0cfSJohannes Berg 115783e7e4ceSHerbert Xu err = rhltable_init(&local->sta_hash, &sta_rht_params); 11587bedd0cfSJohannes Berg if (err) 11597bedd0cfSJohannes Berg return err; 11607bedd0cfSJohannes Berg 11614d33960bSJohannes Berg spin_lock_init(&local->tim_lock); 116234e89507SJohannes Berg mutex_init(&local->sta_mtx); 1163f0706e82SJiri Benc INIT_LIST_HEAD(&local->sta_list); 1164f0706e82SJiri Benc 116534f11cd3SKees Cook timer_setup(&local->sta_cleanup, sta_info_cleanup, 0); 11667bedd0cfSJohannes Berg return 0; 1167f0706e82SJiri Benc } 1168f0706e82SJiri Benc 1169f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local) 1170f0706e82SJiri Benc { 1171a56f992cSJohannes Berg del_timer_sync(&local->sta_cleanup); 117283e7e4ceSHerbert Xu rhltable_destroy(&local->sta_hash); 1173f0706e82SJiri Benc } 1174f0706e82SJiri Benc 1175051007d9SJohannes Berg 1176e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans) 1177f0706e82SJiri Benc { 1178b998e8bbSJohannes Berg struct ieee80211_local *local = sdata->local; 1179f0706e82SJiri Benc struct sta_info *sta, *tmp; 1180d778207bSJohannes Berg LIST_HEAD(free_list); 118144213b5eSJohannes Berg int ret = 0; 1182f0706e82SJiri Benc 1183d0709a65SJohannes Berg might_sleep(); 1184d0709a65SJohannes Berg 1185e716251dSJohannes Berg WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP); 1186e716251dSJohannes Berg WARN_ON(vlans && !sdata->bss); 1187e716251dSJohannes Berg 118834e89507SJohannes Berg mutex_lock(&local->sta_mtx); 118934e89507SJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 1190e716251dSJohannes Berg if (sdata == sta->sdata || 1191e716251dSJohannes Berg (vlans && sdata->bss == sta->sdata->bss)) { 1192d778207bSJohannes Berg if (!WARN_ON(__sta_info_destroy_part1(sta))) 1193d778207bSJohannes Berg list_add(&sta->free_list, &free_list); 119434316837SJohannes Berg ret++; 119534316837SJohannes Berg } 119634e89507SJohannes Berg } 1197d778207bSJohannes Berg 1198d778207bSJohannes Berg if (!list_empty(&free_list)) { 1199d778207bSJohannes Berg synchronize_net(); 1200d778207bSJohannes Berg list_for_each_entry_safe(sta, tmp, &free_list, free_list) 1201d778207bSJohannes Berg __sta_info_destroy_part2(sta); 1202d778207bSJohannes Berg } 120334e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 120444213b5eSJohannes Berg 1205051007d9SJohannes Berg return ret; 1206051007d9SJohannes Berg } 1207051007d9SJohannes Berg 120824723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, 120924723d1bSJohannes Berg unsigned long exp_time) 121024723d1bSJohannes Berg { 121124723d1bSJohannes Berg struct ieee80211_local *local = sdata->local; 121224723d1bSJohannes Berg struct sta_info *sta, *tmp; 121324723d1bSJohannes Berg 121434e89507SJohannes Berg mutex_lock(&local->sta_mtx); 1215e46a2cf9SMohammed Shafi Shajakhan 1216e46a2cf9SMohammed Shafi Shajakhan list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 1217b8da6b6aSJohannes Berg unsigned long last_active = ieee80211_sta_last_active(sta); 1218b8da6b6aSJohannes Berg 1219ec2b774eSMarek Lindner if (sdata != sta->sdata) 1220ec2b774eSMarek Lindner continue; 1221ec2b774eSMarek Lindner 1222b8da6b6aSJohannes Berg if (time_is_before_jiffies(last_active + exp_time)) { 1223eea57d42SMohammed Shafi Shajakhan sta_dbg(sta->sdata, "expiring inactive STA %pM\n", 1224bdcbd8e0SJohannes Berg sta->sta.addr); 12253f52b7e3SMarco Porsch 12263f52b7e3SMarco Porsch if (ieee80211_vif_is_mesh(&sdata->vif) && 12273f52b7e3SMarco Porsch test_sta_flag(sta, WLAN_STA_PS_STA)) 12283f52b7e3SMarco Porsch atomic_dec(&sdata->u.mesh.ps.num_sta_ps); 12293f52b7e3SMarco Porsch 123034e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 123124723d1bSJohannes Berg } 1232e46a2cf9SMohammed Shafi Shajakhan } 1233e46a2cf9SMohammed Shafi Shajakhan 123434e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 123524723d1bSJohannes Berg } 123617741cdcSJohannes Berg 1237686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, 1238686b9cb9SBen Greear const u8 *addr, 1239686b9cb9SBen Greear const u8 *localaddr) 124017741cdcSJohannes Berg { 12417bedd0cfSJohannes Berg struct ieee80211_local *local = hw_to_local(hw); 124283e7e4ceSHerbert Xu struct rhlist_head *tmp; 12437bedd0cfSJohannes Berg struct sta_info *sta; 124417741cdcSJohannes Berg 1245686b9cb9SBen Greear /* 1246686b9cb9SBen Greear * Just return a random station if localaddr is NULL 1247686b9cb9SBen Greear * ... first in list. 1248686b9cb9SBen Greear */ 124983e7e4ceSHerbert Xu for_each_sta_info(local, addr, sta, tmp) { 1250686b9cb9SBen Greear if (localaddr && 1251b203ca39SJoe Perches !ether_addr_equal(sta->sdata->vif.addr, localaddr)) 1252686b9cb9SBen Greear continue; 1253f7c65594SJohannes Berg if (!sta->uploaded) 1254f7c65594SJohannes Berg return NULL; 125517741cdcSJohannes Berg return &sta->sta; 1256f7c65594SJohannes Berg } 1257f7c65594SJohannes Berg 1258abe60632SJohannes Berg return NULL; 125917741cdcSJohannes Berg } 1260686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr); 12615ed176e1SJohannes Berg 12625ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif, 12635ed176e1SJohannes Berg const u8 *addr) 12645ed176e1SJohannes Berg { 1265f7c65594SJohannes Berg struct sta_info *sta; 12665ed176e1SJohannes Berg 12675ed176e1SJohannes Berg if (!vif) 12685ed176e1SJohannes Berg return NULL; 12695ed176e1SJohannes Berg 1270f7c65594SJohannes Berg sta = sta_info_get_bss(vif_to_sdata(vif), addr); 1271f7c65594SJohannes Berg if (!sta) 1272f7c65594SJohannes Berg return NULL; 12735ed176e1SJohannes Berg 1274f7c65594SJohannes Berg if (!sta->uploaded) 1275f7c65594SJohannes Berg return NULL; 1276f7c65594SJohannes Berg 1277f7c65594SJohannes Berg return &sta->sta; 12785ed176e1SJohannes Berg } 127917741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta); 1280af818581SJohannes Berg 1281e3685e03SJohannes Berg /* powersave support code */ 1282e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) 128350a9432dSJohannes Berg { 1284608383bfSHelmut Schaa struct ieee80211_sub_if_data *sdata = sta->sdata; 1285e3685e03SJohannes Berg struct ieee80211_local *local = sdata->local; 1286e3685e03SJohannes Berg struct sk_buff_head pending; 1287ba8c3d6fSFelix Fietkau int filtered = 0, buffered = 0, ac, i; 1288e3685e03SJohannes Berg unsigned long flags; 1289d012a605SMarco Porsch struct ps_data *ps; 1290d012a605SMarco Porsch 12913918edb0SFelix Fietkau if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 12923918edb0SFelix Fietkau sdata = container_of(sdata->bss, struct ieee80211_sub_if_data, 12933918edb0SFelix Fietkau u.ap); 12943918edb0SFelix Fietkau 12953918edb0SFelix Fietkau if (sdata->vif.type == NL80211_IFTYPE_AP) 1296d012a605SMarco Porsch ps = &sdata->bss->ps; 12973f52b7e3SMarco Porsch else if (ieee80211_vif_is_mesh(&sdata->vif)) 12983f52b7e3SMarco Porsch ps = &sdata->u.mesh.ps; 1299d012a605SMarco Porsch else 1300d012a605SMarco Porsch return; 130150a9432dSJohannes Berg 1302c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_SP); 130347086fc5SJohannes Berg 13045a306f58SJohannes Berg BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1); 1305948d887dSJohannes Berg sta->driver_buffered_tids = 0; 1306ba8c3d6fSFelix Fietkau sta->txq_buffered_tids = 0; 1307948d887dSJohannes Berg 130830686bf7SJohannes Berg if (!ieee80211_hw_check(&local->hw, AP_LINK_PS)) 130912375ef9SJohannes Berg drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); 1310af818581SJohannes Berg 1311ba8c3d6fSFelix Fietkau for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) { 1312adf8ed01SJohannes Berg if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i])) 1313ba8c3d6fSFelix Fietkau continue; 1314ba8c3d6fSFelix Fietkau 131518667600SToke Høiland-Jørgensen schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i])); 1316ba8c3d6fSFelix Fietkau } 1317ba8c3d6fSFelix Fietkau 1318948d887dSJohannes Berg skb_queue_head_init(&pending); 1319af818581SJohannes Berg 13201d147bfaSEmmanuel Grumbach /* sync with ieee80211_tx_h_unicast_ps_buf */ 13211d147bfaSEmmanuel Grumbach spin_lock(&sta->ps_lock); 1322af818581SJohannes Berg /* Send all buffered frames to the station */ 1323948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1324948d887dSJohannes Berg int count = skb_queue_len(&pending), tmp; 1325948d887dSJohannes Berg 1326987c285cSArik Nemtsov spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 1327948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending); 1328987c285cSArik Nemtsov spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 1329948d887dSJohannes Berg tmp = skb_queue_len(&pending); 1330948d887dSJohannes Berg filtered += tmp - count; 1331948d887dSJohannes Berg count = tmp; 1332948d887dSJohannes Berg 1333987c285cSArik Nemtsov spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 1334948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending); 1335987c285cSArik Nemtsov spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 1336948d887dSJohannes Berg tmp = skb_queue_len(&pending); 1337948d887dSJohannes Berg buffered += tmp - count; 1338948d887dSJohannes Berg } 1339948d887dSJohannes Berg 1340e3685e03SJohannes Berg ieee80211_add_pending_skbs(local, &pending); 13415ac2e350SJohannes Berg 13425ac2e350SJohannes Berg /* now we're no longer in the deliver code */ 13435ac2e350SJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DELIVER); 13445ac2e350SJohannes Berg 13455ac2e350SJohannes Berg /* The station might have polled and then woken up before we responded, 13465ac2e350SJohannes Berg * so clear these flags now to avoid them sticking around. 13475ac2e350SJohannes Berg */ 13485ac2e350SJohannes Berg clear_sta_flag(sta, WLAN_STA_PSPOLL); 13495ac2e350SJohannes Berg clear_sta_flag(sta, WLAN_STA_UAPSD); 13501d147bfaSEmmanuel Grumbach spin_unlock(&sta->ps_lock); 1351948d887dSJohannes Berg 1352e3685e03SJohannes Berg atomic_dec(&ps->num_sta_ps); 1353e3685e03SJohannes Berg 1354af818581SJohannes Berg local->total_ps_buffered -= buffered; 1355af818581SJohannes Berg 1356c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1357c868cb35SJohannes Berg 1358bdcbd8e0SJohannes Berg ps_dbg(sdata, 13592595d259SSara Sharon "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n", 1360bdcbd8e0SJohannes Berg sta->sta.addr, sta->sta.aid, filtered, buffered); 136117c18bf8SJohannes Berg 136217c18bf8SJohannes Berg ieee80211_check_fast_xmit(sta); 1363af818581SJohannes Berg } 1364af818581SJohannes Berg 13650ead2510SEmmanuel Grumbach static void ieee80211_send_null_response(struct sta_info *sta, int tid, 1366b77cf4f8SJohannes Berg enum ieee80211_frame_release_type reason, 13670ead2510SEmmanuel Grumbach bool call_driver, bool more_data) 1368ce662b44SJohannes Berg { 13690ead2510SEmmanuel Grumbach struct ieee80211_sub_if_data *sdata = sta->sdata; 1370ce662b44SJohannes Berg struct ieee80211_local *local = sdata->local; 1371ce662b44SJohannes Berg struct ieee80211_qos_hdr *nullfunc; 1372ce662b44SJohannes Berg struct sk_buff *skb; 1373ce662b44SJohannes Berg int size = sizeof(*nullfunc); 1374ce662b44SJohannes Berg __le16 fc; 1375a74a8c84SJohannes Berg bool qos = sta->sta.wme; 1376ce662b44SJohannes Berg struct ieee80211_tx_info *info; 137755de908aSJohannes Berg struct ieee80211_chanctx_conf *chanctx_conf; 1378ce662b44SJohannes Berg 137941cbb0f5SLuca Coelho /* Don't send NDPs when STA is connected HE */ 138041cbb0f5SLuca Coelho if (sdata->vif.type == NL80211_IFTYPE_STATION && 138141cbb0f5SLuca Coelho !(sdata->u.mgd.flags & IEEE80211_STA_DISABLE_HE)) 138241cbb0f5SLuca Coelho return; 138341cbb0f5SLuca Coelho 1384ce662b44SJohannes Berg if (qos) { 1385ce662b44SJohannes Berg fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1386ce662b44SJohannes Berg IEEE80211_STYPE_QOS_NULLFUNC | 1387ce662b44SJohannes Berg IEEE80211_FCTL_FROMDS); 1388ce662b44SJohannes Berg } else { 1389ce662b44SJohannes Berg size -= 2; 1390ce662b44SJohannes Berg fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1391ce662b44SJohannes Berg IEEE80211_STYPE_NULLFUNC | 1392ce662b44SJohannes Berg IEEE80211_FCTL_FROMDS); 1393ce662b44SJohannes Berg } 1394ce662b44SJohannes Berg 1395ce662b44SJohannes Berg skb = dev_alloc_skb(local->hw.extra_tx_headroom + size); 1396ce662b44SJohannes Berg if (!skb) 1397ce662b44SJohannes Berg return; 1398ce662b44SJohannes Berg 1399ce662b44SJohannes Berg skb_reserve(skb, local->hw.extra_tx_headroom); 1400ce662b44SJohannes Berg 14014df864c1SJohannes Berg nullfunc = skb_put(skb, size); 1402ce662b44SJohannes Berg nullfunc->frame_control = fc; 1403ce662b44SJohannes Berg nullfunc->duration_id = 0; 1404ce662b44SJohannes Berg memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN); 1405ce662b44SJohannes Berg memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN); 1406ce662b44SJohannes Berg memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN); 1407864a6040SJohannes Berg nullfunc->seq_ctrl = 0; 1408ce662b44SJohannes Berg 1409ce662b44SJohannes Berg skb->priority = tid; 1410ce662b44SJohannes Berg skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]); 141159b66255SJohannes Berg if (qos) { 1412ce662b44SJohannes Berg nullfunc->qos_ctrl = cpu_to_le16(tid); 1413ce662b44SJohannes Berg 14140ead2510SEmmanuel Grumbach if (reason == IEEE80211_FRAME_RELEASE_UAPSD) { 1415ce662b44SJohannes Berg nullfunc->qos_ctrl |= 1416ce662b44SJohannes Berg cpu_to_le16(IEEE80211_QOS_CTL_EOSP); 14170ead2510SEmmanuel Grumbach if (more_data) 14180ead2510SEmmanuel Grumbach nullfunc->frame_control |= 14190ead2510SEmmanuel Grumbach cpu_to_le16(IEEE80211_FCTL_MOREDATA); 14200ead2510SEmmanuel Grumbach } 1421ce662b44SJohannes Berg } 1422ce662b44SJohannes Berg 1423ce662b44SJohannes Berg info = IEEE80211_SKB_CB(skb); 1424ce662b44SJohannes Berg 1425ce662b44SJohannes Berg /* 1426ce662b44SJohannes Berg * Tell TX path to send this frame even though the 1427ce662b44SJohannes Berg * STA may still remain is PS mode after this frame 1428deeaee19SJohannes Berg * exchange. Also set EOSP to indicate this packet 1429deeaee19SJohannes Berg * ends the poll/service period. 1430ce662b44SJohannes Berg */ 143102f2f1a9SJohannes Berg info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER | 1432deeaee19SJohannes Berg IEEE80211_TX_STATUS_EOSP | 1433ce662b44SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 1434ce662b44SJohannes Berg 14356b127c71SSujith Manoharan info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE; 14366b127c71SSujith Manoharan 1437b77cf4f8SJohannes Berg if (call_driver) 1438b77cf4f8SJohannes Berg drv_allow_buffered_frames(local, sta, BIT(tid), 1, 1439b77cf4f8SJohannes Berg reason, false); 144040b96408SJohannes Berg 144189afe614SJohannes Berg skb->dev = sdata->dev; 144289afe614SJohannes Berg 144355de908aSJohannes Berg rcu_read_lock(); 144455de908aSJohannes Berg chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf); 144555de908aSJohannes Berg if (WARN_ON(!chanctx_conf)) { 144655de908aSJohannes Berg rcu_read_unlock(); 144755de908aSJohannes Berg kfree_skb(skb); 144855de908aSJohannes Berg return; 144955de908aSJohannes Berg } 145055de908aSJohannes Berg 145173c4e195SJohannes Berg info->band = chanctx_conf->def.chan->band; 1452b9771d41SJohannes Berg ieee80211_xmit(sdata, sta, skb, 0); 145355de908aSJohannes Berg rcu_read_unlock(); 1454ce662b44SJohannes Berg } 1455ce662b44SJohannes Berg 14560a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids) 14570a1cb809SJohannes Berg { 14580a1cb809SJohannes Berg /* lower 3 TIDs aren't ordered perfectly */ 14590a1cb809SJohannes Berg if (tids & 0xF8) 14600a1cb809SJohannes Berg return fls(tids) - 1; 14610a1cb809SJohannes Berg /* TID 0 is BE just like TID 3 */ 14620a1cb809SJohannes Berg if (tids & BIT(0)) 14630a1cb809SJohannes Berg return 0; 14640a1cb809SJohannes Berg return fls(tids) - 1; 14650a1cb809SJohannes Berg } 14660a1cb809SJohannes Berg 14670ead2510SEmmanuel Grumbach /* Indicates if the MORE_DATA bit should be set in the last 14680ead2510SEmmanuel Grumbach * frame obtained by ieee80211_sta_ps_get_frames. 14690ead2510SEmmanuel Grumbach * Note that driver_release_tids is relevant only if 14700ead2510SEmmanuel Grumbach * reason = IEEE80211_FRAME_RELEASE_PSPOLL 14710ead2510SEmmanuel Grumbach */ 14720ead2510SEmmanuel Grumbach static bool 14730ead2510SEmmanuel Grumbach ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs, 14740ead2510SEmmanuel Grumbach enum ieee80211_frame_release_type reason, 14750ead2510SEmmanuel Grumbach unsigned long driver_release_tids) 14760ead2510SEmmanuel Grumbach { 14770ead2510SEmmanuel Grumbach int ac; 14780ead2510SEmmanuel Grumbach 14790ead2510SEmmanuel Grumbach /* If the driver has data on more than one TID then 14800ead2510SEmmanuel Grumbach * certainly there's more data if we release just a 14810ead2510SEmmanuel Grumbach * single frame now (from a single TID). This will 14820ead2510SEmmanuel Grumbach * only happen for PS-Poll. 14830ead2510SEmmanuel Grumbach */ 14840ead2510SEmmanuel Grumbach if (reason == IEEE80211_FRAME_RELEASE_PSPOLL && 14850ead2510SEmmanuel Grumbach hweight16(driver_release_tids) > 1) 14860ead2510SEmmanuel Grumbach return true; 14870ead2510SEmmanuel Grumbach 14880ead2510SEmmanuel Grumbach for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1489f438ceb8SEmmanuel Grumbach if (ignored_acs & ieee80211_ac_to_qos_mask[ac]) 14900ead2510SEmmanuel Grumbach continue; 14910ead2510SEmmanuel Grumbach 14920ead2510SEmmanuel Grumbach if (!skb_queue_empty(&sta->tx_filtered[ac]) || 14930ead2510SEmmanuel Grumbach !skb_queue_empty(&sta->ps_tx_buf[ac])) 14940ead2510SEmmanuel Grumbach return true; 14950ead2510SEmmanuel Grumbach } 14960ead2510SEmmanuel Grumbach 14970ead2510SEmmanuel Grumbach return false; 14980ead2510SEmmanuel Grumbach } 14990ead2510SEmmanuel Grumbach 150047086fc5SJohannes Berg static void 15010ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs, 15020ead2510SEmmanuel Grumbach enum ieee80211_frame_release_type reason, 15030ead2510SEmmanuel Grumbach struct sk_buff_head *frames, 15040ead2510SEmmanuel Grumbach unsigned long *driver_release_tids) 1505af818581SJohannes Berg { 1506af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1507af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 1508948d887dSJohannes Berg int ac; 1509af818581SJohannes Berg 1510f9f760b4SJohannes Berg /* Get response frame(s) and more data bit for the last one. */ 1511948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 15124049e09aSJohannes Berg unsigned long tids; 15134049e09aSJohannes Berg 1514f438ceb8SEmmanuel Grumbach if (ignored_acs & ieee80211_ac_to_qos_mask[ac]) 1515948d887dSJohannes Berg continue; 1516948d887dSJohannes Berg 15174049e09aSJohannes Berg tids = ieee80211_tids_for_ac(ac); 15184049e09aSJohannes Berg 1519f9f760b4SJohannes Berg /* if we already have frames from software, then we can't also 1520f9f760b4SJohannes Berg * release from hardware queues 1521f9f760b4SJohannes Berg */ 15220ead2510SEmmanuel Grumbach if (skb_queue_empty(frames)) { 15230ead2510SEmmanuel Grumbach *driver_release_tids |= 15240ead2510SEmmanuel Grumbach sta->driver_buffered_tids & tids; 15250ead2510SEmmanuel Grumbach *driver_release_tids |= sta->txq_buffered_tids & tids; 1526ba8c3d6fSFelix Fietkau } 1527f9f760b4SJohannes Berg 15280ead2510SEmmanuel Grumbach if (!*driver_release_tids) { 152947086fc5SJohannes Berg struct sk_buff *skb; 153047086fc5SJohannes Berg 153147086fc5SJohannes Berg while (n_frames > 0) { 1532948d887dSJohannes Berg skb = skb_dequeue(&sta->tx_filtered[ac]); 1533948d887dSJohannes Berg if (!skb) { 153447086fc5SJohannes Berg skb = skb_dequeue( 153547086fc5SJohannes Berg &sta->ps_tx_buf[ac]); 1536af818581SJohannes Berg if (skb) 1537af818581SJohannes Berg local->total_ps_buffered--; 1538af818581SJohannes Berg } 153947086fc5SJohannes Berg if (!skb) 154047086fc5SJohannes Berg break; 154147086fc5SJohannes Berg n_frames--; 15420ead2510SEmmanuel Grumbach __skb_queue_tail(frames, skb); 154347086fc5SJohannes Berg } 1544948d887dSJohannes Berg } 1545948d887dSJohannes Berg 15460ead2510SEmmanuel Grumbach /* If we have more frames buffered on this AC, then abort the 15470ead2510SEmmanuel Grumbach * loop since we can't send more data from other ACs before 15480ead2510SEmmanuel Grumbach * the buffered frames from this. 15494049e09aSJohannes Berg */ 1550948d887dSJohannes Berg if (!skb_queue_empty(&sta->tx_filtered[ac]) || 15510ead2510SEmmanuel Grumbach !skb_queue_empty(&sta->ps_tx_buf[ac])) 1552948d887dSJohannes Berg break; 1553948d887dSJohannes Berg } 1554948d887dSJohannes Berg } 1555af818581SJohannes Berg 15560ead2510SEmmanuel Grumbach static void 15570ead2510SEmmanuel Grumbach ieee80211_sta_ps_deliver_response(struct sta_info *sta, 15580ead2510SEmmanuel Grumbach int n_frames, u8 ignored_acs, 15590ead2510SEmmanuel Grumbach enum ieee80211_frame_release_type reason) 15600ead2510SEmmanuel Grumbach { 15610ead2510SEmmanuel Grumbach struct ieee80211_sub_if_data *sdata = sta->sdata; 15620ead2510SEmmanuel Grumbach struct ieee80211_local *local = sdata->local; 15630ead2510SEmmanuel Grumbach unsigned long driver_release_tids = 0; 15640ead2510SEmmanuel Grumbach struct sk_buff_head frames; 15650ead2510SEmmanuel Grumbach bool more_data; 15660ead2510SEmmanuel Grumbach 15670ead2510SEmmanuel Grumbach /* Service or PS-Poll period starts */ 15680ead2510SEmmanuel Grumbach set_sta_flag(sta, WLAN_STA_SP); 15690ead2510SEmmanuel Grumbach 15700ead2510SEmmanuel Grumbach __skb_queue_head_init(&frames); 15710ead2510SEmmanuel Grumbach 15720ead2510SEmmanuel Grumbach ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason, 15730ead2510SEmmanuel Grumbach &frames, &driver_release_tids); 15740ead2510SEmmanuel Grumbach 15750ead2510SEmmanuel Grumbach more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids); 15760ead2510SEmmanuel Grumbach 15771a57081aSEmmanuel Grumbach if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL) 15780ead2510SEmmanuel Grumbach driver_release_tids = 15790ead2510SEmmanuel Grumbach BIT(find_highest_prio_tid(driver_release_tids)); 15800ead2510SEmmanuel Grumbach 1581f9f760b4SJohannes Berg if (skb_queue_empty(&frames) && !driver_release_tids) { 1582f438ceb8SEmmanuel Grumbach int tid, ac; 15834049e09aSJohannes Berg 1584ce662b44SJohannes Berg /* 1585ce662b44SJohannes Berg * For PS-Poll, this can only happen due to a race condition 1586ce662b44SJohannes Berg * when we set the TIM bit and the station notices it, but 1587ce662b44SJohannes Berg * before it can poll for the frame we expire it. 1588ce662b44SJohannes Berg * 1589ce662b44SJohannes Berg * For uAPSD, this is said in the standard (11.2.1.5 h): 1590ce662b44SJohannes Berg * At each unscheduled SP for a non-AP STA, the AP shall 1591ce662b44SJohannes Berg * attempt to transmit at least one MSDU or MMPDU, but no 1592ce662b44SJohannes Berg * more than the value specified in the Max SP Length field 1593ce662b44SJohannes Berg * in the QoS Capability element from delivery-enabled ACs, 1594ce662b44SJohannes Berg * that are destined for the non-AP STA. 1595ce662b44SJohannes Berg * 1596ce662b44SJohannes Berg * Since we have no other MSDU/MMPDU, transmit a QoS null frame. 1597ce662b44SJohannes Berg */ 1598ce662b44SJohannes Berg 1599ce662b44SJohannes Berg /* This will evaluate to 1, 3, 5 or 7. */ 1600f438ceb8SEmmanuel Grumbach for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++) 1601d7f84244SEmmanuel Grumbach if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac])) 1602d7f84244SEmmanuel Grumbach break; 1603f438ceb8SEmmanuel Grumbach tid = 7 - 2 * ac; 1604ce662b44SJohannes Berg 16050ead2510SEmmanuel Grumbach ieee80211_send_null_response(sta, tid, reason, true, false); 1606f9f760b4SJohannes Berg } else if (!driver_release_tids) { 160747086fc5SJohannes Berg struct sk_buff_head pending; 160847086fc5SJohannes Berg struct sk_buff *skb; 160940b96408SJohannes Berg int num = 0; 161040b96408SJohannes Berg u16 tids = 0; 1611b77cf4f8SJohannes Berg bool need_null = false; 161247086fc5SJohannes Berg 161347086fc5SJohannes Berg skb_queue_head_init(&pending); 161447086fc5SJohannes Berg 161547086fc5SJohannes Berg while ((skb = __skb_dequeue(&frames))) { 1616af818581SJohannes Berg struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 161747086fc5SJohannes Berg struct ieee80211_hdr *hdr = (void *) skb->data; 161840b96408SJohannes Berg u8 *qoshdr = NULL; 161940b96408SJohannes Berg 162040b96408SJohannes Berg num++; 1621af818581SJohannes Berg 1622af818581SJohannes Berg /* 162347086fc5SJohannes Berg * Tell TX path to send this frame even though the 162447086fc5SJohannes Berg * STA may still remain is PS mode after this frame 162547086fc5SJohannes Berg * exchange. 1626af818581SJohannes Berg */ 16276b127c71SSujith Manoharan info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER; 16286b127c71SSujith Manoharan info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE; 1629af818581SJohannes Berg 163047086fc5SJohannes Berg /* 163147086fc5SJohannes Berg * Use MoreData flag to indicate whether there are 163247086fc5SJohannes Berg * more buffered frames for this STA 163347086fc5SJohannes Berg */ 163424b9c373SJanusz.Dziedzic@tieto.com if (more_data || !skb_queue_empty(&frames)) 163547086fc5SJohannes Berg hdr->frame_control |= 163647086fc5SJohannes Berg cpu_to_le16(IEEE80211_FCTL_MOREDATA); 163724b9c373SJanusz.Dziedzic@tieto.com else 163824b9c373SJanusz.Dziedzic@tieto.com hdr->frame_control &= 163924b9c373SJanusz.Dziedzic@tieto.com cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 1640af818581SJohannes Berg 164140b96408SJohannes Berg if (ieee80211_is_data_qos(hdr->frame_control) || 164240b96408SJohannes Berg ieee80211_is_qos_nullfunc(hdr->frame_control)) 164340b96408SJohannes Berg qoshdr = ieee80211_get_qos_ctl(hdr); 164440b96408SJohannes Berg 1645b77cf4f8SJohannes Berg tids |= BIT(skb->priority); 1646b77cf4f8SJohannes Berg 1647b77cf4f8SJohannes Berg __skb_queue_tail(&pending, skb); 1648b77cf4f8SJohannes Berg 1649b77cf4f8SJohannes Berg /* end service period after last frame or add one */ 1650b77cf4f8SJohannes Berg if (!skb_queue_empty(&frames)) 1651b77cf4f8SJohannes Berg continue; 1652b77cf4f8SJohannes Berg 1653b77cf4f8SJohannes Berg if (reason != IEEE80211_FRAME_RELEASE_UAPSD) { 1654b77cf4f8SJohannes Berg /* for PS-Poll, there's only one frame */ 1655b77cf4f8SJohannes Berg info->flags |= IEEE80211_TX_STATUS_EOSP | 1656b77cf4f8SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 1657b77cf4f8SJohannes Berg break; 1658b77cf4f8SJohannes Berg } 1659b77cf4f8SJohannes Berg 1660b77cf4f8SJohannes Berg /* For uAPSD, things are a bit more complicated. If the 1661b77cf4f8SJohannes Berg * last frame has a QoS header (i.e. is a QoS-data or 1662b77cf4f8SJohannes Berg * QoS-nulldata frame) then just set the EOSP bit there 1663b77cf4f8SJohannes Berg * and be done. 1664b77cf4f8SJohannes Berg * If the frame doesn't have a QoS header (which means 1665b77cf4f8SJohannes Berg * it should be a bufferable MMPDU) then we can't set 1666b77cf4f8SJohannes Berg * the EOSP bit in the QoS header; add a QoS-nulldata 1667b77cf4f8SJohannes Berg * frame to the list to send it after the MMPDU. 1668b77cf4f8SJohannes Berg * 1669b77cf4f8SJohannes Berg * Note that this code is only in the mac80211-release 1670b77cf4f8SJohannes Berg * code path, we assume that the driver will not buffer 1671b77cf4f8SJohannes Berg * anything but QoS-data frames, or if it does, will 1672b77cf4f8SJohannes Berg * create the QoS-nulldata frame by itself if needed. 1673b77cf4f8SJohannes Berg * 1674b77cf4f8SJohannes Berg * Cf. 802.11-2012 10.2.1.10 (c). 1675b77cf4f8SJohannes Berg */ 1676b77cf4f8SJohannes Berg if (qoshdr) { 167740b96408SJohannes Berg *qoshdr |= IEEE80211_QOS_CTL_EOSP; 1678deeaee19SJohannes Berg 167947086fc5SJohannes Berg info->flags |= IEEE80211_TX_STATUS_EOSP | 168047086fc5SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 1681b77cf4f8SJohannes Berg } else { 1682b77cf4f8SJohannes Berg /* The standard isn't completely clear on this 1683b77cf4f8SJohannes Berg * as it says the more-data bit should be set 1684b77cf4f8SJohannes Berg * if there are more BUs. The QoS-Null frame 1685b77cf4f8SJohannes Berg * we're about to send isn't buffered yet, we 1686b77cf4f8SJohannes Berg * only create it below, but let's pretend it 1687b77cf4f8SJohannes Berg * was buffered just in case some clients only 1688b77cf4f8SJohannes Berg * expect more-data=0 when eosp=1. 1689b77cf4f8SJohannes Berg */ 1690b77cf4f8SJohannes Berg hdr->frame_control |= 1691b77cf4f8SJohannes Berg cpu_to_le16(IEEE80211_FCTL_MOREDATA); 1692b77cf4f8SJohannes Berg need_null = true; 1693b77cf4f8SJohannes Berg num++; 169452a3f20cSMarco Porsch } 1695b77cf4f8SJohannes Berg break; 169647086fc5SJohannes Berg } 169747086fc5SJohannes Berg 169840b96408SJohannes Berg drv_allow_buffered_frames(local, sta, tids, num, 169940b96408SJohannes Berg reason, more_data); 170040b96408SJohannes Berg 170147086fc5SJohannes Berg ieee80211_add_pending_skbs(local, &pending); 1702af818581SJohannes Berg 1703b77cf4f8SJohannes Berg if (need_null) 1704b77cf4f8SJohannes Berg ieee80211_send_null_response( 17050ead2510SEmmanuel Grumbach sta, find_highest_prio_tid(tids), 17060ead2510SEmmanuel Grumbach reason, false, false); 1707b77cf4f8SJohannes Berg 1708c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1709af818581SJohannes Berg } else { 1710ba8c3d6fSFelix Fietkau int tid; 1711ba8c3d6fSFelix Fietkau 1712af818581SJohannes Berg /* 17134049e09aSJohannes Berg * We need to release a frame that is buffered somewhere in the 17144049e09aSJohannes Berg * driver ... it'll have to handle that. 1715f9f760b4SJohannes Berg * Note that the driver also has to check the number of frames 1716f9f760b4SJohannes Berg * on the TIDs we're releasing from - if there are more than 1717f9f760b4SJohannes Berg * n_frames it has to set the more-data bit (if we didn't ask 1718f9f760b4SJohannes Berg * it to set it anyway due to other buffered frames); if there 1719f9f760b4SJohannes Berg * are fewer than n_frames it has to make sure to adjust that 1720f9f760b4SJohannes Berg * to allow the service period to end properly. 1721af818581SJohannes Berg */ 17224049e09aSJohannes Berg drv_release_buffered_frames(local, sta, driver_release_tids, 172347086fc5SJohannes Berg n_frames, reason, more_data); 17244049e09aSJohannes Berg 17254049e09aSJohannes Berg /* 17264049e09aSJohannes Berg * Note that we don't recalculate the TIM bit here as it would 17274049e09aSJohannes Berg * most likely have no effect at all unless the driver told us 1728f9f760b4SJohannes Berg * that the TID(s) became empty before returning here from the 17294049e09aSJohannes Berg * release function. 1730f9f760b4SJohannes Berg * Either way, however, when the driver tells us that the TID(s) 1731ba8c3d6fSFelix Fietkau * became empty or we find that a txq became empty, we'll do the 1732ba8c3d6fSFelix Fietkau * TIM recalculation. 17334049e09aSJohannes Berg */ 1734ba8c3d6fSFelix Fietkau 1735ba8c3d6fSFelix Fietkau if (!sta->sta.txq[0]) 1736ba8c3d6fSFelix Fietkau return; 1737ba8c3d6fSFelix Fietkau 1738ba8c3d6fSFelix Fietkau for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) { 1739adf8ed01SJohannes Berg if (!sta->sta.txq[tid] || 1740adf8ed01SJohannes Berg !(driver_release_tids & BIT(tid)) || 17411e1430d5SJohannes Berg txq_has_queue(sta->sta.txq[tid])) 1742ba8c3d6fSFelix Fietkau continue; 1743ba8c3d6fSFelix Fietkau 1744ba8c3d6fSFelix Fietkau sta_info_recalc_tim(sta); 1745ba8c3d6fSFelix Fietkau break; 1746ba8c3d6fSFelix Fietkau } 1747af818581SJohannes Berg } 1748af818581SJohannes Berg } 1749af818581SJohannes Berg 1750af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) 1751af818581SJohannes Berg { 175247086fc5SJohannes Berg u8 ignore_for_response = sta->sta.uapsd_queues; 1753af818581SJohannes Berg 1754af818581SJohannes Berg /* 175547086fc5SJohannes Berg * If all ACs are delivery-enabled then we should reply 175647086fc5SJohannes Berg * from any of them, if only some are enabled we reply 175747086fc5SJohannes Berg * only from the non-enabled ones. 1758af818581SJohannes Berg */ 175947086fc5SJohannes Berg if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1) 176047086fc5SJohannes Berg ignore_for_response = 0; 1761af818581SJohannes Berg 176247086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response, 176347086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_PSPOLL); 1764af818581SJohannes Berg } 176547086fc5SJohannes Berg 176647086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta) 176747086fc5SJohannes Berg { 176847086fc5SJohannes Berg int n_frames = sta->sta.max_sp; 176947086fc5SJohannes Berg u8 delivery_enabled = sta->sta.uapsd_queues; 177047086fc5SJohannes Berg 177147086fc5SJohannes Berg /* 177247086fc5SJohannes Berg * If we ever grow support for TSPEC this might happen if 177347086fc5SJohannes Berg * the TSPEC update from hostapd comes in between a trigger 177447086fc5SJohannes Berg * frame setting WLAN_STA_UAPSD in the RX path and this 177547086fc5SJohannes Berg * actually getting called. 177647086fc5SJohannes Berg */ 177747086fc5SJohannes Berg if (!delivery_enabled) 177847086fc5SJohannes Berg return; 177947086fc5SJohannes Berg 178047086fc5SJohannes Berg switch (sta->sta.max_sp) { 178147086fc5SJohannes Berg case 1: 178247086fc5SJohannes Berg n_frames = 2; 178347086fc5SJohannes Berg break; 178447086fc5SJohannes Berg case 2: 178547086fc5SJohannes Berg n_frames = 4; 178647086fc5SJohannes Berg break; 178747086fc5SJohannes Berg case 3: 178847086fc5SJohannes Berg n_frames = 6; 178947086fc5SJohannes Berg break; 179047086fc5SJohannes Berg case 0: 179147086fc5SJohannes Berg /* XXX: what is a good value? */ 179213a8098aSAndrei Otcheretianski n_frames = 128; 179347086fc5SJohannes Berg break; 179447086fc5SJohannes Berg } 179547086fc5SJohannes Berg 179647086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled, 179747086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_UAPSD); 1798af818581SJohannes Berg } 1799af818581SJohannes Berg 1800af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw, 1801af818581SJohannes Berg struct ieee80211_sta *pubsta, bool block) 1802af818581SJohannes Berg { 1803af818581SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1804af818581SJohannes Berg 1805b5878a2dSJohannes Berg trace_api_sta_block_awake(sta->local, pubsta, block); 1806b5878a2dSJohannes Berg 18075ac2e350SJohannes Berg if (block) { 1808c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_PS_DRIVER); 180917c18bf8SJohannes Berg ieee80211_clear_fast_xmit(sta); 18105ac2e350SJohannes Berg return; 18115ac2e350SJohannes Berg } 18125ac2e350SJohannes Berg 18135ac2e350SJohannes Berg if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER)) 18145ac2e350SJohannes Berg return; 18155ac2e350SJohannes Berg 18165ac2e350SJohannes Berg if (!test_sta_flag(sta, WLAN_STA_PS_STA)) { 18175ac2e350SJohannes Berg set_sta_flag(sta, WLAN_STA_PS_DELIVER); 18185ac2e350SJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 18195ac2e350SJohannes Berg ieee80211_queue_work(hw, &sta->drv_deliver_wk); 18205ac2e350SJohannes Berg } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) || 18215ac2e350SJohannes Berg test_sta_flag(sta, WLAN_STA_UAPSD)) { 18225ac2e350SJohannes Berg /* must be asleep in this case */ 18235ac2e350SJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 18245ac2e350SJohannes Berg ieee80211_queue_work(hw, &sta->drv_deliver_wk); 18255ac2e350SJohannes Berg } else { 18265ac2e350SJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 182717c18bf8SJohannes Berg ieee80211_check_fast_xmit(sta); 18285ac2e350SJohannes Berg } 1829af818581SJohannes Berg } 1830af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake); 1831dcf55fb5SFelix Fietkau 1832e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta) 183337fbd908SJohannes Berg { 183437fbd908SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 183537fbd908SJohannes Berg struct ieee80211_local *local = sta->local; 183637fbd908SJohannes Berg 183737fbd908SJohannes Berg trace_api_eosp(local, pubsta); 183837fbd908SJohannes Berg 183937fbd908SJohannes Berg clear_sta_flag(sta, WLAN_STA_SP); 184037fbd908SJohannes Berg } 1841e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp); 184237fbd908SJohannes Berg 18430ead2510SEmmanuel Grumbach void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid) 18440ead2510SEmmanuel Grumbach { 18450ead2510SEmmanuel Grumbach struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 18460ead2510SEmmanuel Grumbach enum ieee80211_frame_release_type reason; 18470ead2510SEmmanuel Grumbach bool more_data; 18480ead2510SEmmanuel Grumbach 18490ead2510SEmmanuel Grumbach trace_api_send_eosp_nullfunc(sta->local, pubsta, tid); 18500ead2510SEmmanuel Grumbach 18510ead2510SEmmanuel Grumbach reason = IEEE80211_FRAME_RELEASE_UAPSD; 18520ead2510SEmmanuel Grumbach more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues, 18530ead2510SEmmanuel Grumbach reason, 0); 18540ead2510SEmmanuel Grumbach 18550ead2510SEmmanuel Grumbach ieee80211_send_null_response(sta, tid, reason, false, more_data); 18560ead2510SEmmanuel Grumbach } 18570ead2510SEmmanuel Grumbach EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc); 18580ead2510SEmmanuel Grumbach 1859042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta, 1860042ec453SJohannes Berg u8 tid, bool buffered) 1861dcf55fb5SFelix Fietkau { 1862dcf55fb5SFelix Fietkau struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1863dcf55fb5SFelix Fietkau 18645a306f58SJohannes Berg if (WARN_ON(tid >= IEEE80211_NUM_TIDS)) 1865042ec453SJohannes Berg return; 1866042ec453SJohannes Berg 18671b000789SJohannes Berg trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered); 18681b000789SJohannes Berg 1869948d887dSJohannes Berg if (buffered) 1870948d887dSJohannes Berg set_bit(tid, &sta->driver_buffered_tids); 1871948d887dSJohannes Berg else 1872948d887dSJohannes Berg clear_bit(tid, &sta->driver_buffered_tids); 1873948d887dSJohannes Berg 1874c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1875dcf55fb5SFelix Fietkau } 1876042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered); 1877d9a7ddb0SJohannes Berg 1878b4809e94SToke Høiland-Jørgensen void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid, 1879b4809e94SToke Høiland-Jørgensen u32 tx_airtime, u32 rx_airtime) 1880b4809e94SToke Høiland-Jørgensen { 1881b4809e94SToke Høiland-Jørgensen struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1882b4809e94SToke Høiland-Jørgensen struct ieee80211_local *local = sta->sdata->local; 1883b4809e94SToke Høiland-Jørgensen u8 ac = ieee80211_ac_from_tid(tid); 1884b4809e94SToke Høiland-Jørgensen u32 airtime = 0; 1885b4809e94SToke Høiland-Jørgensen 1886b4809e94SToke Høiland-Jørgensen if (sta->local->airtime_flags & AIRTIME_USE_TX) 1887b4809e94SToke Høiland-Jørgensen airtime += tx_airtime; 1888b4809e94SToke Høiland-Jørgensen if (sta->local->airtime_flags & AIRTIME_USE_RX) 1889b4809e94SToke Høiland-Jørgensen airtime += rx_airtime; 1890b4809e94SToke Høiland-Jørgensen 1891b4809e94SToke Høiland-Jørgensen spin_lock_bh(&local->active_txq_lock[ac]); 1892b4809e94SToke Høiland-Jørgensen sta->airtime[ac].tx_airtime += tx_airtime; 1893b4809e94SToke Høiland-Jørgensen sta->airtime[ac].rx_airtime += rx_airtime; 1894b4809e94SToke Høiland-Jørgensen sta->airtime[ac].deficit -= airtime; 1895b4809e94SToke Høiland-Jørgensen spin_unlock_bh(&local->active_txq_lock[ac]); 1896b4809e94SToke Høiland-Jørgensen } 1897b4809e94SToke Høiland-Jørgensen EXPORT_SYMBOL(ieee80211_sta_register_airtime); 1898b4809e94SToke Høiland-Jørgensen 18993ace10f5SKan Yan void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local, 19003ace10f5SKan Yan struct sta_info *sta, u8 ac, 19013ace10f5SKan Yan u16 tx_airtime, bool tx_completed) 19023ace10f5SKan Yan { 19033ace10f5SKan Yan int tx_pending; 19043ace10f5SKan Yan 1905911bde0fSToke Høiland-Jørgensen if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) 1906911bde0fSToke Høiland-Jørgensen return; 1907911bde0fSToke Høiland-Jørgensen 19083ace10f5SKan Yan if (!tx_completed) { 19093ace10f5SKan Yan if (sta) 19103ace10f5SKan Yan atomic_add(tx_airtime, 19113ace10f5SKan Yan &sta->airtime[ac].aql_tx_pending); 19123ace10f5SKan Yan 19133ace10f5SKan Yan atomic_add(tx_airtime, &local->aql_total_pending_airtime); 19143ace10f5SKan Yan return; 19153ace10f5SKan Yan } 19163ace10f5SKan Yan 19173ace10f5SKan Yan if (sta) { 19183ace10f5SKan Yan tx_pending = atomic_sub_return(tx_airtime, 19193ace10f5SKan Yan &sta->airtime[ac].aql_tx_pending); 19203ace10f5SKan Yan if (WARN_ONCE(tx_pending < 0, 19213ace10f5SKan Yan "STA %pM AC %d txq pending airtime underflow: %u, %u", 19223ace10f5SKan Yan sta->addr, ac, tx_pending, tx_airtime)) 19233ace10f5SKan Yan atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending, 19243ace10f5SKan Yan tx_pending, 0); 19253ace10f5SKan Yan } 19263ace10f5SKan Yan 19273ace10f5SKan Yan tx_pending = atomic_sub_return(tx_airtime, 19283ace10f5SKan Yan &local->aql_total_pending_airtime); 19293ace10f5SKan Yan if (WARN_ONCE(tx_pending < 0, 19303ace10f5SKan Yan "Device %s AC %d pending airtime underflow: %u, %u", 19313ace10f5SKan Yan wiphy_name(local->hw.wiphy), ac, tx_pending, 19323ace10f5SKan Yan tx_airtime)) 19333ace10f5SKan Yan atomic_cmpxchg(&local->aql_total_pending_airtime, 19343ace10f5SKan Yan tx_pending, 0); 19353ace10f5SKan Yan } 19363ace10f5SKan Yan 193783d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta, 1938d9a7ddb0SJohannes Berg enum ieee80211_sta_state new_state) 1939d9a7ddb0SJohannes Berg { 19408bf11d8dSJohannes Berg might_sleep(); 1941d9a7ddb0SJohannes Berg 1942d9a7ddb0SJohannes Berg if (sta->sta_state == new_state) 1943d9a7ddb0SJohannes Berg return 0; 1944d9a7ddb0SJohannes Berg 1945f09603a2SJohannes Berg /* check allowed transitions first */ 1946f09603a2SJohannes Berg 1947d9a7ddb0SJohannes Berg switch (new_state) { 1948d9a7ddb0SJohannes Berg case IEEE80211_STA_NONE: 1949f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_AUTH) 1950d9a7ddb0SJohannes Berg return -EINVAL; 1951d9a7ddb0SJohannes Berg break; 1952d9a7ddb0SJohannes Berg case IEEE80211_STA_AUTH: 1953f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_NONE && 1954f09603a2SJohannes Berg sta->sta_state != IEEE80211_STA_ASSOC) 1955d9a7ddb0SJohannes Berg return -EINVAL; 1956d9a7ddb0SJohannes Berg break; 1957d9a7ddb0SJohannes Berg case IEEE80211_STA_ASSOC: 1958f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_AUTH && 1959f09603a2SJohannes Berg sta->sta_state != IEEE80211_STA_AUTHORIZED) 1960d9a7ddb0SJohannes Berg return -EINVAL; 1961d9a7ddb0SJohannes Berg break; 1962d9a7ddb0SJohannes Berg case IEEE80211_STA_AUTHORIZED: 1963f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_ASSOC) 1964d9a7ddb0SJohannes Berg return -EINVAL; 1965d9a7ddb0SJohannes Berg break; 1966d9a7ddb0SJohannes Berg default: 1967d9a7ddb0SJohannes Berg WARN(1, "invalid state %d", new_state); 1968d9a7ddb0SJohannes Berg return -EINVAL; 1969d9a7ddb0SJohannes Berg } 1970d9a7ddb0SJohannes Berg 1971bdcbd8e0SJohannes Berg sta_dbg(sta->sdata, "moving STA %pM to state %d\n", 1972bdcbd8e0SJohannes Berg sta->sta.addr, new_state); 1973f09603a2SJohannes Berg 1974f09603a2SJohannes Berg /* 1975f09603a2SJohannes Berg * notify the driver before the actual changes so it can 1976f09603a2SJohannes Berg * fail the transition 1977f09603a2SJohannes Berg */ 1978f09603a2SJohannes Berg if (test_sta_flag(sta, WLAN_STA_INSERTED)) { 1979f09603a2SJohannes Berg int err = drv_sta_state(sta->local, sta->sdata, sta, 1980f09603a2SJohannes Berg sta->sta_state, new_state); 1981f09603a2SJohannes Berg if (err) 1982f09603a2SJohannes Berg return err; 1983f09603a2SJohannes Berg } 1984f09603a2SJohannes Berg 1985f09603a2SJohannes Berg /* reflect the change in all state variables */ 1986f09603a2SJohannes Berg 1987f09603a2SJohannes Berg switch (new_state) { 1988f09603a2SJohannes Berg case IEEE80211_STA_NONE: 1989f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_AUTH) 1990f09603a2SJohannes Berg clear_bit(WLAN_STA_AUTH, &sta->_flags); 1991f09603a2SJohannes Berg break; 1992f09603a2SJohannes Berg case IEEE80211_STA_AUTH: 1993a7201a6cSIlan Peer if (sta->sta_state == IEEE80211_STA_NONE) { 1994f09603a2SJohannes Berg set_bit(WLAN_STA_AUTH, &sta->_flags); 1995a7201a6cSIlan Peer } else if (sta->sta_state == IEEE80211_STA_ASSOC) { 1996f09603a2SJohannes Berg clear_bit(WLAN_STA_ASSOC, &sta->_flags); 1997a7201a6cSIlan Peer ieee80211_recalc_min_chandef(sta->sdata); 199852cfa1d6SAyala Beker if (!sta->sta.support_p2p_ps) 199952cfa1d6SAyala Beker ieee80211_recalc_p2p_go_ps_allowed(sta->sdata); 2000a7201a6cSIlan Peer } 2001f09603a2SJohannes Berg break; 2002f09603a2SJohannes Berg case IEEE80211_STA_ASSOC: 2003f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_AUTH) { 2004f09603a2SJohannes Berg set_bit(WLAN_STA_ASSOC, &sta->_flags); 20059cf02338SBen Greear sta->assoc_at = ktime_get_boottime_ns(); 2006a7201a6cSIlan Peer ieee80211_recalc_min_chandef(sta->sdata); 200752cfa1d6SAyala Beker if (!sta->sta.support_p2p_ps) 200852cfa1d6SAyala Beker ieee80211_recalc_p2p_go_ps_allowed(sta->sdata); 2009f09603a2SJohannes Berg } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) { 201072f15d53SMichael Braun ieee80211_vif_dec_num_mcast(sta->sdata); 2011f09603a2SJohannes Berg clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 201217c18bf8SJohannes Berg ieee80211_clear_fast_xmit(sta); 201349ddf8e6SJohannes Berg ieee80211_clear_fast_rx(sta); 2014f09603a2SJohannes Berg } 2015f09603a2SJohannes Berg break; 2016f09603a2SJohannes Berg case IEEE80211_STA_AUTHORIZED: 2017f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_ASSOC) { 201872f15d53SMichael Braun ieee80211_vif_inc_num_mcast(sta->sdata); 2019f09603a2SJohannes Berg set_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 202017c18bf8SJohannes Berg ieee80211_check_fast_xmit(sta); 202149ddf8e6SJohannes Berg ieee80211_check_fast_rx(sta); 2022f09603a2SJohannes Berg } 20233e493173SJouni Malinen if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN || 20243e493173SJouni Malinen sta->sdata->vif.type == NL80211_IFTYPE_AP) 20253e493173SJouni Malinen cfg80211_send_layer2_update(sta->sdata->dev, 20263e493173SJouni Malinen sta->sta.addr); 2027f09603a2SJohannes Berg break; 2028f09603a2SJohannes Berg default: 2029f09603a2SJohannes Berg break; 2030f09603a2SJohannes Berg } 2031f09603a2SJohannes Berg 2032d9a7ddb0SJohannes Berg sta->sta_state = new_state; 2033d9a7ddb0SJohannes Berg 2034d9a7ddb0SJohannes Berg return 0; 2035d9a7ddb0SJohannes Berg } 2036687da132SEmmanuel Grumbach 2037687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta) 2038687da132SEmmanuel Grumbach { 2039687da132SEmmanuel Grumbach struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap; 2040687da132SEmmanuel Grumbach u8 rx_streams; 2041687da132SEmmanuel Grumbach 2042687da132SEmmanuel Grumbach if (!sta->sta.ht_cap.ht_supported) 2043687da132SEmmanuel Grumbach return 1; 2044687da132SEmmanuel Grumbach 2045687da132SEmmanuel Grumbach if (sta->sta.vht_cap.vht_supported) { 2046687da132SEmmanuel Grumbach int i; 2047687da132SEmmanuel Grumbach u16 tx_mcs_map = 2048687da132SEmmanuel Grumbach le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map); 2049687da132SEmmanuel Grumbach 2050687da132SEmmanuel Grumbach for (i = 7; i >= 0; i--) 2051687da132SEmmanuel Grumbach if ((tx_mcs_map & (0x3 << (i * 2))) != 2052687da132SEmmanuel Grumbach IEEE80211_VHT_MCS_NOT_SUPPORTED) 2053687da132SEmmanuel Grumbach return i + 1; 2054687da132SEmmanuel Grumbach } 2055687da132SEmmanuel Grumbach 2056687da132SEmmanuel Grumbach if (ht_cap->mcs.rx_mask[3]) 2057687da132SEmmanuel Grumbach rx_streams = 4; 2058687da132SEmmanuel Grumbach else if (ht_cap->mcs.rx_mask[2]) 2059687da132SEmmanuel Grumbach rx_streams = 3; 2060687da132SEmmanuel Grumbach else if (ht_cap->mcs.rx_mask[1]) 2061687da132SEmmanuel Grumbach rx_streams = 2; 2062687da132SEmmanuel Grumbach else 2063687da132SEmmanuel Grumbach rx_streams = 1; 2064687da132SEmmanuel Grumbach 2065687da132SEmmanuel Grumbach if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF)) 2066687da132SEmmanuel Grumbach return rx_streams; 2067687da132SEmmanuel Grumbach 2068687da132SEmmanuel Grumbach return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK) 2069687da132SEmmanuel Grumbach >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1; 2070687da132SEmmanuel Grumbach } 2071b7ffbd7eSJohannes Berg 2072c9c5962bSJohannes Berg static struct ieee80211_sta_rx_stats * 2073c9c5962bSJohannes Berg sta_get_last_rx_stats(struct sta_info *sta) 2074c9c5962bSJohannes Berg { 2075c9c5962bSJohannes Berg struct ieee80211_sta_rx_stats *stats = &sta->rx_stats; 2076c9c5962bSJohannes Berg struct ieee80211_local *local = sta->local; 2077c9c5962bSJohannes Berg int cpu; 2078c9c5962bSJohannes Berg 2079c9c5962bSJohannes Berg if (!ieee80211_hw_check(&local->hw, USES_RSS)) 2080c9c5962bSJohannes Berg return stats; 2081c9c5962bSJohannes Berg 2082c9c5962bSJohannes Berg for_each_possible_cpu(cpu) { 2083c9c5962bSJohannes Berg struct ieee80211_sta_rx_stats *cpustats; 2084c9c5962bSJohannes Berg 2085c9c5962bSJohannes Berg cpustats = per_cpu_ptr(sta->pcpu_rx_stats, cpu); 2086c9c5962bSJohannes Berg 2087c9c5962bSJohannes Berg if (time_after(cpustats->last_rx, stats->last_rx)) 2088c9c5962bSJohannes Berg stats = cpustats; 2089c9c5962bSJohannes Berg } 2090c9c5962bSJohannes Berg 2091c9c5962bSJohannes Berg return stats; 2092c9c5962bSJohannes Berg } 2093c9c5962bSJohannes Berg 209441cbb0f5SLuca Coelho static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate, 20954f6b1b3dSJohannes Berg struct rate_info *rinfo) 2096fbd6ff5cSJohannes Berg { 2097dcba665bSJohannes Berg rinfo->bw = STA_STATS_GET(BW, rate); 2098fbd6ff5cSJohannes Berg 2099dcba665bSJohannes Berg switch (STA_STATS_GET(TYPE, rate)) { 21007f406cd1SJohannes Berg case STA_STATS_RATE_TYPE_VHT: 21014f6b1b3dSJohannes Berg rinfo->flags = RATE_INFO_FLAGS_VHT_MCS; 2102dcba665bSJohannes Berg rinfo->mcs = STA_STATS_GET(VHT_MCS, rate); 2103dcba665bSJohannes Berg rinfo->nss = STA_STATS_GET(VHT_NSS, rate); 2104dcba665bSJohannes Berg if (STA_STATS_GET(SGI, rate)) 2105dcba665bSJohannes Berg rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI; 21067f406cd1SJohannes Berg break; 21077f406cd1SJohannes Berg case STA_STATS_RATE_TYPE_HT: 21084f6b1b3dSJohannes Berg rinfo->flags = RATE_INFO_FLAGS_MCS; 2109dcba665bSJohannes Berg rinfo->mcs = STA_STATS_GET(HT_MCS, rate); 2110dcba665bSJohannes Berg if (STA_STATS_GET(SGI, rate)) 2111dcba665bSJohannes Berg rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI; 21127f406cd1SJohannes Berg break; 21137f406cd1SJohannes Berg case STA_STATS_RATE_TYPE_LEGACY: { 2114fbd6ff5cSJohannes Berg struct ieee80211_supported_band *sband; 2115fbd6ff5cSJohannes Berg u16 brate; 21164f6b1b3dSJohannes Berg unsigned int shift; 2117dcba665bSJohannes Berg int band = STA_STATS_GET(LEGACY_BAND, rate); 2118dcba665bSJohannes Berg int rate_idx = STA_STATS_GET(LEGACY_IDX, rate); 2119fbd6ff5cSJohannes Berg 2120dcba665bSJohannes Berg sband = local->hw.wiphy->bands[band]; 2121dcba665bSJohannes Berg brate = sband->bitrates[rate_idx].bitrate; 21224f6b1b3dSJohannes Berg if (rinfo->bw == RATE_INFO_BW_5) 21234f6b1b3dSJohannes Berg shift = 2; 21244f6b1b3dSJohannes Berg else if (rinfo->bw == RATE_INFO_BW_10) 21254f6b1b3dSJohannes Berg shift = 1; 21264f6b1b3dSJohannes Berg else 21274f6b1b3dSJohannes Berg shift = 0; 2128fbd6ff5cSJohannes Berg rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift); 21297f406cd1SJohannes Berg break; 21307f406cd1SJohannes Berg } 213141cbb0f5SLuca Coelho case STA_STATS_RATE_TYPE_HE: 213241cbb0f5SLuca Coelho rinfo->flags = RATE_INFO_FLAGS_HE_MCS; 213341cbb0f5SLuca Coelho rinfo->mcs = STA_STATS_GET(HE_MCS, rate); 213441cbb0f5SLuca Coelho rinfo->nss = STA_STATS_GET(HE_NSS, rate); 213541cbb0f5SLuca Coelho rinfo->he_gi = STA_STATS_GET(HE_GI, rate); 213641cbb0f5SLuca Coelho rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate); 213741cbb0f5SLuca Coelho rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate); 213841cbb0f5SLuca Coelho break; 2139fbd6ff5cSJohannes Berg } 21404f6b1b3dSJohannes Berg } 2141fbd6ff5cSJohannes Berg 2142a17d93ffSBen Greear static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo) 21434f6b1b3dSJohannes Berg { 21446aa7de05SMark Rutland u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate); 21454f6b1b3dSJohannes Berg 21464f6b1b3dSJohannes Berg if (rate == STA_STATS_RATE_INVALID) 2147a17d93ffSBen Greear return -EINVAL; 2148a17d93ffSBen Greear 21494f6b1b3dSJohannes Berg sta_stats_decode_rate(sta->local, rate, rinfo); 2150a17d93ffSBen Greear return 0; 2151fbd6ff5cSJohannes Berg } 2152fbd6ff5cSJohannes Berg 21530f9c5a61SJohannes Berg static void sta_set_tidstats(struct sta_info *sta, 21540f9c5a61SJohannes Berg struct cfg80211_tid_stats *tidstats, 21550f9c5a61SJohannes Berg int tid) 21560f9c5a61SJohannes Berg { 21570f9c5a61SJohannes Berg struct ieee80211_local *local = sta->local; 21580f9c5a61SJohannes Berg 21590f9c5a61SJohannes Berg if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) { 21600f9c5a61SJohannes Berg unsigned int start; 21610f9c5a61SJohannes Berg 21620f9c5a61SJohannes Berg do { 21630f9c5a61SJohannes Berg start = u64_stats_fetch_begin(&sta->rx_stats.syncp); 21640f9c5a61SJohannes Berg tidstats->rx_msdu = sta->rx_stats.msdu[tid]; 21650f9c5a61SJohannes Berg } while (u64_stats_fetch_retry(&sta->rx_stats.syncp, start)); 21660f9c5a61SJohannes Berg 21670f9c5a61SJohannes Berg tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU); 21680f9c5a61SJohannes Berg } 21690f9c5a61SJohannes Berg 21700f9c5a61SJohannes Berg if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) { 21710f9c5a61SJohannes Berg tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU); 21720f9c5a61SJohannes Berg tidstats->tx_msdu = sta->tx_stats.msdu[tid]; 21730f9c5a61SJohannes Berg } 21740f9c5a61SJohannes Berg 21750f9c5a61SJohannes Berg if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) && 21760f9c5a61SJohannes Berg ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) { 21770f9c5a61SJohannes Berg tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES); 21780f9c5a61SJohannes Berg tidstats->tx_msdu_retries = sta->status_stats.msdu_retries[tid]; 21790f9c5a61SJohannes Berg } 21800f9c5a61SJohannes Berg 21810f9c5a61SJohannes Berg if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) && 21820f9c5a61SJohannes Berg ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) { 21830f9c5a61SJohannes Berg tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED); 21840f9c5a61SJohannes Berg tidstats->tx_msdu_failed = sta->status_stats.msdu_failed[tid]; 21850f9c5a61SJohannes Berg } 21862fe4a29aSToke Høiland-Jørgensen 21872fe4a29aSToke Høiland-Jørgensen if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) { 21882fe4a29aSToke Høiland-Jørgensen spin_lock_bh(&local->fq.lock); 21892fe4a29aSToke Høiland-Jørgensen rcu_read_lock(); 21902fe4a29aSToke Høiland-Jørgensen 21912fe4a29aSToke Høiland-Jørgensen tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS); 21922fe4a29aSToke Høiland-Jørgensen ieee80211_fill_txq_stats(&tidstats->txq_stats, 21932fe4a29aSToke Høiland-Jørgensen to_txq_info(sta->sta.txq[tid])); 21942fe4a29aSToke Høiland-Jørgensen 21952fe4a29aSToke Høiland-Jørgensen rcu_read_unlock(); 21962fe4a29aSToke Høiland-Jørgensen spin_unlock_bh(&local->fq.lock); 21972fe4a29aSToke Høiland-Jørgensen } 21980f9c5a61SJohannes Berg } 21990f9c5a61SJohannes Berg 2200c9c5962bSJohannes Berg static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats) 2201c9c5962bSJohannes Berg { 2202c9c5962bSJohannes Berg unsigned int start; 2203c9c5962bSJohannes Berg u64 value; 2204c9c5962bSJohannes Berg 2205c9c5962bSJohannes Berg do { 2206c9c5962bSJohannes Berg start = u64_stats_fetch_begin(&rxstats->syncp); 2207c9c5962bSJohannes Berg value = rxstats->bytes; 2208c9c5962bSJohannes Berg } while (u64_stats_fetch_retry(&rxstats->syncp, start)); 2209c9c5962bSJohannes Berg 2210c9c5962bSJohannes Berg return value; 2211c9c5962bSJohannes Berg } 2212c9c5962bSJohannes Berg 22130fdf1493SJohannes Berg void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo, 22140fdf1493SJohannes Berg bool tidstats) 2215b7ffbd7eSJohannes Berg { 2216b7ffbd7eSJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 2217b7ffbd7eSJohannes Berg struct ieee80211_local *local = sdata->local; 2218b7ffbd7eSJohannes Berg u32 thr = 0; 2219c9c5962bSJohannes Berg int i, ac, cpu; 2220c9c5962bSJohannes Berg struct ieee80211_sta_rx_stats *last_rxstats; 2221c9c5962bSJohannes Berg 2222c9c5962bSJohannes Berg last_rxstats = sta_get_last_rx_stats(sta); 2223b7ffbd7eSJohannes Berg 2224b7ffbd7eSJohannes Berg sinfo->generation = sdata->local->sta_generation; 2225b7ffbd7eSJohannes Berg 2226225b8189SJohannes Berg /* do before driver, so beacon filtering drivers have a 2227225b8189SJohannes Berg * chance to e.g. just add the number of filtered beacons 2228225b8189SJohannes Berg * (or just modify the value entirely, of course) 2229225b8189SJohannes Berg */ 2230225b8189SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_STATION) 2231225b8189SJohannes Berg sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal; 2232225b8189SJohannes Berg 22332b9a7e1bSJohannes Berg drv_sta_statistics(local, sdata, &sta->sta, sinfo); 22342b9a7e1bSJohannes Berg 2235a4217750SOmer Efrat sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) | 2236a4217750SOmer Efrat BIT_ULL(NL80211_STA_INFO_STA_FLAGS) | 2237a4217750SOmer Efrat BIT_ULL(NL80211_STA_INFO_BSS_PARAM) | 2238a4217750SOmer Efrat BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) | 22399cf02338SBen Greear BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) | 2240a4217750SOmer Efrat BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC); 2241976bd9efSJohannes Berg 2242976bd9efSJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_STATION) { 2243976bd9efSJohannes Berg sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count; 2244a4217750SOmer Efrat sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS); 2245976bd9efSJohannes Berg } 2246b7ffbd7eSJohannes Berg 224784b00607SArnd Bergmann sinfo->connected_time = ktime_get_seconds() - sta->last_connected; 22489cf02338SBen Greear sinfo->assoc_at = sta->assoc_at; 2249e5a9f8d0SJohannes Berg sinfo->inactive_time = 2250b8da6b6aSJohannes Berg jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta)); 22512b9a7e1bSJohannes Berg 2252a4217750SOmer Efrat if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) | 2253a4217750SOmer Efrat BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) { 2254b7ffbd7eSJohannes Berg sinfo->tx_bytes = 0; 22552b9a7e1bSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 2256e5a9f8d0SJohannes Berg sinfo->tx_bytes += sta->tx_stats.bytes[ac]; 2257a4217750SOmer Efrat sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64); 2258b7ffbd7eSJohannes Berg } 22592b9a7e1bSJohannes Berg 2260a4217750SOmer Efrat if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) { 22612b9a7e1bSJohannes Berg sinfo->tx_packets = 0; 22622b9a7e1bSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 2263e5a9f8d0SJohannes Berg sinfo->tx_packets += sta->tx_stats.packets[ac]; 2264a4217750SOmer Efrat sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS); 22652b9a7e1bSJohannes Berg } 22662b9a7e1bSJohannes Berg 2267a4217750SOmer Efrat if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) | 2268a4217750SOmer Efrat BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) { 2269c9c5962bSJohannes Berg sinfo->rx_bytes += sta_get_stats_bytes(&sta->rx_stats); 22700f9c5a61SJohannes Berg 2271c9c5962bSJohannes Berg if (sta->pcpu_rx_stats) { 2272c9c5962bSJohannes Berg for_each_possible_cpu(cpu) { 2273c9c5962bSJohannes Berg struct ieee80211_sta_rx_stats *cpurxs; 2274c9c5962bSJohannes Berg 2275c9c5962bSJohannes Berg cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu); 2276c9c5962bSJohannes Berg sinfo->rx_bytes += sta_get_stats_bytes(cpurxs); 2277c9c5962bSJohannes Berg } 2278c9c5962bSJohannes Berg } 2279c9c5962bSJohannes Berg 2280a4217750SOmer Efrat sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64); 22812b9a7e1bSJohannes Berg } 22822b9a7e1bSJohannes Berg 2283a4217750SOmer Efrat if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) { 2284e5a9f8d0SJohannes Berg sinfo->rx_packets = sta->rx_stats.packets; 2285c9c5962bSJohannes Berg if (sta->pcpu_rx_stats) { 2286c9c5962bSJohannes Berg for_each_possible_cpu(cpu) { 2287c9c5962bSJohannes Berg struct ieee80211_sta_rx_stats *cpurxs; 2288c9c5962bSJohannes Berg 2289c9c5962bSJohannes Berg cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu); 2290c9c5962bSJohannes Berg sinfo->rx_packets += cpurxs->packets; 2291c9c5962bSJohannes Berg } 2292c9c5962bSJohannes Berg } 2293a4217750SOmer Efrat sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS); 22942b9a7e1bSJohannes Berg } 22952b9a7e1bSJohannes Berg 2296a4217750SOmer Efrat if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) { 2297e5a9f8d0SJohannes Berg sinfo->tx_retries = sta->status_stats.retry_count; 2298a4217750SOmer Efrat sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES); 22992b9a7e1bSJohannes Berg } 23002b9a7e1bSJohannes Berg 2301a4217750SOmer Efrat if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) { 2302e5a9f8d0SJohannes Berg sinfo->tx_failed = sta->status_stats.retry_failed; 2303a4217750SOmer Efrat sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED); 23042b9a7e1bSJohannes Berg } 23052b9a7e1bSJohannes Berg 2306b4809e94SToke Høiland-Jørgensen if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) { 2307b4809e94SToke Høiland-Jørgensen for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 2308b4809e94SToke Høiland-Jørgensen sinfo->rx_duration += sta->airtime[ac].rx_airtime; 2309b4809e94SToke Høiland-Jørgensen sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION); 2310b4809e94SToke Høiland-Jørgensen } 2311b4809e94SToke Høiland-Jørgensen 2312b4809e94SToke Høiland-Jørgensen if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) { 2313b4809e94SToke Høiland-Jørgensen for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 2314b4809e94SToke Høiland-Jørgensen sinfo->tx_duration += sta->airtime[ac].tx_airtime; 2315b4809e94SToke Høiland-Jørgensen sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION); 2316b4809e94SToke Høiland-Jørgensen } 2317b4809e94SToke Høiland-Jørgensen 2318b4809e94SToke Høiland-Jørgensen if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) { 2319b4809e94SToke Høiland-Jørgensen sinfo->airtime_weight = sta->airtime_weight; 2320b4809e94SToke Høiland-Jørgensen sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT); 2321b4809e94SToke Høiland-Jørgensen } 2322b4809e94SToke Høiland-Jørgensen 2323e5a9f8d0SJohannes Berg sinfo->rx_dropped_misc = sta->rx_stats.dropped; 2324c9c5962bSJohannes Berg if (sta->pcpu_rx_stats) { 2325c9c5962bSJohannes Berg for_each_possible_cpu(cpu) { 2326c9c5962bSJohannes Berg struct ieee80211_sta_rx_stats *cpurxs; 2327c9c5962bSJohannes Berg 2328c9c5962bSJohannes Berg cpurxs = per_cpu_ptr(sta->pcpu_rx_stats, cpu); 2329e165bc02SJohannes Berg sinfo->rx_dropped_misc += cpurxs->dropped; 2330c9c5962bSJohannes Berg } 2331c9c5962bSJohannes Berg } 2332b7ffbd7eSJohannes Berg 2333225b8189SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_STATION && 2334225b8189SJohannes Berg !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) { 2335a4217750SOmer Efrat sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) | 2336a4217750SOmer Efrat BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG); 2337225b8189SJohannes Berg sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif); 2338225b8189SJohannes Berg } 2339225b8189SJohannes Berg 234030686bf7SJohannes Berg if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) || 234130686bf7SJohannes Berg ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) { 2342a4217750SOmer Efrat if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) { 2343c9c5962bSJohannes Berg sinfo->signal = (s8)last_rxstats->last_signal; 2344a4217750SOmer Efrat sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL); 2345b7ffbd7eSJohannes Berg } 23462b9a7e1bSJohannes Berg 2347c9c5962bSJohannes Berg if (!sta->pcpu_rx_stats && 2348a4217750SOmer Efrat !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) { 234940d9a38aSJohannes Berg sinfo->signal_avg = 23500be6ed13SJohannes Berg -ewma_signal_read(&sta->rx_stats_avg.signal); 2351a4217750SOmer Efrat sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG); 23522b9a7e1bSJohannes Berg } 23532b9a7e1bSJohannes Berg } 23542b9a7e1bSJohannes Berg 2355c9c5962bSJohannes Berg /* for the average - if pcpu_rx_stats isn't set - rxstats must point to 2356c9c5962bSJohannes Berg * the sta->rx_stats struct, so the check here is fine with and without 2357c9c5962bSJohannes Berg * pcpu statistics 2358c9c5962bSJohannes Berg */ 2359c9c5962bSJohannes Berg if (last_rxstats->chains && 2360a4217750SOmer Efrat !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) | 2361a4217750SOmer Efrat BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) { 2362a4217750SOmer Efrat sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL); 2363c9c5962bSJohannes Berg if (!sta->pcpu_rx_stats) 2364a4217750SOmer Efrat sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG); 2365b7ffbd7eSJohannes Berg 2366c9c5962bSJohannes Berg sinfo->chains = last_rxstats->chains; 2367c9c5962bSJohannes Berg 2368b7ffbd7eSJohannes Berg for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) { 2369e5a9f8d0SJohannes Berg sinfo->chain_signal[i] = 2370c9c5962bSJohannes Berg last_rxstats->chain_signal_last[i]; 2371b7ffbd7eSJohannes Berg sinfo->chain_signal_avg[i] = 23720be6ed13SJohannes Berg -ewma_signal_read(&sta->rx_stats_avg.chain_signal[i]); 2373b7ffbd7eSJohannes Berg } 2374b7ffbd7eSJohannes Berg } 2375b7ffbd7eSJohannes Berg 2376a4217750SOmer Efrat if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) { 2377e5a9f8d0SJohannes Berg sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate, 2378e5a9f8d0SJohannes Berg &sinfo->txrate); 2379a4217750SOmer Efrat sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE); 23802b9a7e1bSJohannes Berg } 23812b9a7e1bSJohannes Berg 2382a4217750SOmer Efrat if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) { 2383a17d93ffSBen Greear if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0) 2384a4217750SOmer Efrat sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE); 23852b9a7e1bSJohannes Berg } 2386b7ffbd7eSJohannes Berg 23870fdf1493SJohannes Berg if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) { 23886af8354fSJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) 23896af8354fSJohannes Berg sta_set_tidstats(sta, &sinfo->pertid[i], i); 23908689c051SArend van Spriel } 239179c892b8SJohannes Berg 2392b7ffbd7eSJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) { 2393b7ffbd7eSJohannes Berg #ifdef CONFIG_MAC80211_MESH 2394a4217750SOmer Efrat sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) | 2395a4217750SOmer Efrat BIT_ULL(NL80211_STA_INFO_PLID) | 2396a4217750SOmer Efrat BIT_ULL(NL80211_STA_INFO_PLINK_STATE) | 2397a4217750SOmer Efrat BIT_ULL(NL80211_STA_INFO_LOCAL_PM) | 2398a4217750SOmer Efrat BIT_ULL(NL80211_STA_INFO_PEER_PM) | 2399dbdaee7aSBob Copeland BIT_ULL(NL80211_STA_INFO_NONPEER_PM) | 2400dbdaee7aSBob Copeland BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE); 2401b7ffbd7eSJohannes Berg 2402433f5bc1SJohannes Berg sinfo->llid = sta->mesh->llid; 2403433f5bc1SJohannes Berg sinfo->plid = sta->mesh->plid; 2404433f5bc1SJohannes Berg sinfo->plink_state = sta->mesh->plink_state; 2405b7ffbd7eSJohannes Berg if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) { 2406a4217750SOmer Efrat sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET); 2407433f5bc1SJohannes Berg sinfo->t_offset = sta->mesh->t_offset; 2408b7ffbd7eSJohannes Berg } 2409433f5bc1SJohannes Berg sinfo->local_pm = sta->mesh->local_pm; 2410433f5bc1SJohannes Berg sinfo->peer_pm = sta->mesh->peer_pm; 2411433f5bc1SJohannes Berg sinfo->nonpeer_pm = sta->mesh->nonpeer_pm; 2412dbdaee7aSBob Copeland sinfo->connected_to_gate = sta->mesh->connected_to_gate; 2413b7ffbd7eSJohannes Berg #endif 2414b7ffbd7eSJohannes Berg } 2415b7ffbd7eSJohannes Berg 2416b7ffbd7eSJohannes Berg sinfo->bss_param.flags = 0; 2417b7ffbd7eSJohannes Berg if (sdata->vif.bss_conf.use_cts_prot) 2418b7ffbd7eSJohannes Berg sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT; 2419b7ffbd7eSJohannes Berg if (sdata->vif.bss_conf.use_short_preamble) 2420b7ffbd7eSJohannes Berg sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE; 2421b7ffbd7eSJohannes Berg if (sdata->vif.bss_conf.use_short_slot) 2422b7ffbd7eSJohannes Berg sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME; 2423785e21a8SEmmanuel Grumbach sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period; 2424b7ffbd7eSJohannes Berg sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int; 2425b7ffbd7eSJohannes Berg 2426b7ffbd7eSJohannes Berg sinfo->sta_flags.set = 0; 2427b7ffbd7eSJohannes Berg sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) | 2428b7ffbd7eSJohannes Berg BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) | 2429b7ffbd7eSJohannes Berg BIT(NL80211_STA_FLAG_WME) | 2430b7ffbd7eSJohannes Berg BIT(NL80211_STA_FLAG_MFP) | 2431b7ffbd7eSJohannes Berg BIT(NL80211_STA_FLAG_AUTHENTICATED) | 2432b7ffbd7eSJohannes Berg BIT(NL80211_STA_FLAG_ASSOCIATED) | 2433b7ffbd7eSJohannes Berg BIT(NL80211_STA_FLAG_TDLS_PEER); 2434b7ffbd7eSJohannes Berg if (test_sta_flag(sta, WLAN_STA_AUTHORIZED)) 2435b7ffbd7eSJohannes Berg sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED); 2436b7ffbd7eSJohannes Berg if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE)) 2437b7ffbd7eSJohannes Berg sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE); 2438a74a8c84SJohannes Berg if (sta->sta.wme) 2439b7ffbd7eSJohannes Berg sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME); 2440b7ffbd7eSJohannes Berg if (test_sta_flag(sta, WLAN_STA_MFP)) 2441b7ffbd7eSJohannes Berg sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP); 2442b7ffbd7eSJohannes Berg if (test_sta_flag(sta, WLAN_STA_AUTH)) 2443b7ffbd7eSJohannes Berg sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED); 2444b7ffbd7eSJohannes Berg if (test_sta_flag(sta, WLAN_STA_ASSOC)) 2445b7ffbd7eSJohannes Berg sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED); 2446b7ffbd7eSJohannes Berg if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) 2447b7ffbd7eSJohannes Berg sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER); 2448b7ffbd7eSJohannes Berg 24493b17fbf8SMaxim Altshul thr = sta_get_expected_throughput(sta); 24503b17fbf8SMaxim Altshul 24513b17fbf8SMaxim Altshul if (thr != 0) { 2452a4217750SOmer Efrat sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT); 24533b17fbf8SMaxim Altshul sinfo->expected_throughput = thr; 24543b17fbf8SMaxim Altshul } 2455a78b26ffSVenkateswara Naralasetty 2456a78b26ffSVenkateswara Naralasetty if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) && 2457a78b26ffSVenkateswara Naralasetty sta->status_stats.ack_signal_filled) { 2458a78b26ffSVenkateswara Naralasetty sinfo->ack_signal = sta->status_stats.last_ack_signal; 2459a78b26ffSVenkateswara Naralasetty sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL); 2460a78b26ffSVenkateswara Naralasetty } 2461cc60dbbfSBalaji Pothunoori 24629c06602bSBalaji Pothunoori if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) && 24639c06602bSBalaji Pothunoori sta->status_stats.ack_signal_filled) { 2464cc60dbbfSBalaji Pothunoori sinfo->avg_ack_signal = 2465cc60dbbfSBalaji Pothunoori -(s8)ewma_avg_signal_read( 2466cc60dbbfSBalaji Pothunoori &sta->status_stats.avg_ack_signal); 2467cc60dbbfSBalaji Pothunoori sinfo->filled |= 24689c06602bSBalaji Pothunoori BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG); 2469cc60dbbfSBalaji Pothunoori } 2470ab60633cSNarayanraddi Masti 2471ab60633cSNarayanraddi Masti if (ieee80211_vif_is_mesh(&sdata->vif)) { 2472ab60633cSNarayanraddi Masti sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC); 2473ab60633cSNarayanraddi Masti sinfo->airtime_link_metric = 2474ab60633cSNarayanraddi Masti airtime_link_metric_get(local, sta); 2475ab60633cSNarayanraddi Masti } 24763b17fbf8SMaxim Altshul } 24773b17fbf8SMaxim Altshul 24783b17fbf8SMaxim Altshul u32 sta_get_expected_throughput(struct sta_info *sta) 24793b17fbf8SMaxim Altshul { 24803b17fbf8SMaxim Altshul struct ieee80211_sub_if_data *sdata = sta->sdata; 24813b17fbf8SMaxim Altshul struct ieee80211_local *local = sdata->local; 24823b17fbf8SMaxim Altshul struct rate_control_ref *ref = NULL; 24833b17fbf8SMaxim Altshul u32 thr = 0; 24843b17fbf8SMaxim Altshul 24853b17fbf8SMaxim Altshul if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) 24863b17fbf8SMaxim Altshul ref = local->rate_ctrl; 24873b17fbf8SMaxim Altshul 2488b7ffbd7eSJohannes Berg /* check if the driver has a SW RC implementation */ 2489b7ffbd7eSJohannes Berg if (ref && ref->ops->get_expected_throughput) 2490b7ffbd7eSJohannes Berg thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv); 2491b7ffbd7eSJohannes Berg else 24924fdbc67aSMaxim Altshul thr = drv_get_expected_throughput(local, sta); 2493b7ffbd7eSJohannes Berg 24943b17fbf8SMaxim Altshul return thr; 2495b7ffbd7eSJohannes Berg } 2496b8da6b6aSJohannes Berg 2497b8da6b6aSJohannes Berg unsigned long ieee80211_sta_last_active(struct sta_info *sta) 2498b8da6b6aSJohannes Berg { 2499c9c5962bSJohannes Berg struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta); 2500c9c5962bSJohannes Berg 2501285531f9SAhmed Zaki if (!sta->status_stats.last_ack || 2502285531f9SAhmed Zaki time_after(stats->last_rx, sta->status_stats.last_ack)) 2503c9c5962bSJohannes Berg return stats->last_rx; 2504b8da6b6aSJohannes Berg return sta->status_stats.last_ack; 2505b8da6b6aSJohannes Berg } 2506484a54c2SToke Høiland-Jørgensen 2507484a54c2SToke Høiland-Jørgensen static void sta_update_codel_params(struct sta_info *sta, u32 thr) 2508484a54c2SToke Høiland-Jørgensen { 2509484a54c2SToke Høiland-Jørgensen if (!sta->sdata->local->ops->wake_tx_queue) 2510484a54c2SToke Høiland-Jørgensen return; 2511484a54c2SToke Høiland-Jørgensen 2512484a54c2SToke Høiland-Jørgensen if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) { 2513484a54c2SToke Høiland-Jørgensen sta->cparams.target = MS2TIME(50); 2514484a54c2SToke Høiland-Jørgensen sta->cparams.interval = MS2TIME(300); 2515484a54c2SToke Høiland-Jørgensen sta->cparams.ecn = false; 2516484a54c2SToke Høiland-Jørgensen } else { 2517484a54c2SToke Høiland-Jørgensen sta->cparams.target = MS2TIME(20); 2518484a54c2SToke Høiland-Jørgensen sta->cparams.interval = MS2TIME(100); 2519484a54c2SToke Høiland-Jørgensen sta->cparams.ecn = true; 2520484a54c2SToke Høiland-Jørgensen } 2521484a54c2SToke Høiland-Jørgensen } 2522484a54c2SToke Høiland-Jørgensen 2523484a54c2SToke Høiland-Jørgensen void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta, 2524484a54c2SToke Høiland-Jørgensen u32 thr) 2525484a54c2SToke Høiland-Jørgensen { 2526484a54c2SToke Høiland-Jørgensen struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 2527484a54c2SToke Høiland-Jørgensen 2528484a54c2SToke Høiland-Jørgensen sta_update_codel_params(sta, thr); 2529484a54c2SToke Høiland-Jørgensen } 2530