1f0706e82SJiri Benc /* 2f0706e82SJiri Benc * Copyright 2002-2005, Instant802 Networks, Inc. 3f0706e82SJiri Benc * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> 4f0706e82SJiri Benc * 5f0706e82SJiri Benc * This program is free software; you can redistribute it and/or modify 6f0706e82SJiri Benc * it under the terms of the GNU General Public License version 2 as 7f0706e82SJiri Benc * published by the Free Software Foundation. 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 21f0706e82SJiri Benc #include <net/mac80211.h> 22f0706e82SJiri Benc #include "ieee80211_i.h" 2324487981SJohannes Berg #include "driver-ops.h" 242c8dccc7SJohannes Berg #include "rate.h" 25f0706e82SJiri Benc #include "sta_info.h" 26e9f207f0SJiri Benc #include "debugfs_sta.h" 27ee385855SLuis Carlos Cobo #include "mesh.h" 28ce662b44SJohannes Berg #include "wme.h" 29f0706e82SJiri Benc 30d0709a65SJohannes Berg /** 31d0709a65SJohannes Berg * DOC: STA information lifetime rules 32d0709a65SJohannes Berg * 33d0709a65SJohannes Berg * STA info structures (&struct sta_info) are managed in a hash table 34d0709a65SJohannes Berg * for faster lookup and a list for iteration. They are managed using 35d0709a65SJohannes Berg * RCU, i.e. access to the list and hash table is protected by RCU. 36d0709a65SJohannes Berg * 3734e89507SJohannes Berg * Upon allocating a STA info structure with sta_info_alloc(), the caller 3834e89507SJohannes Berg * owns that structure. It must then insert it into the hash table using 3934e89507SJohannes Berg * either sta_info_insert() or sta_info_insert_rcu(); only in the latter 4034e89507SJohannes Berg * case (which acquires an rcu read section but must not be called from 4134e89507SJohannes Berg * within one) will the pointer still be valid after the call. Note that 4234e89507SJohannes Berg * the caller may not do much with the STA info before inserting it, in 4334e89507SJohannes Berg * particular, it may not start any mesh peer link management or add 4434e89507SJohannes Berg * encryption keys. 4593e5deb1SJohannes Berg * 4693e5deb1SJohannes Berg * When the insertion fails (sta_info_insert()) returns non-zero), the 4793e5deb1SJohannes Berg * structure will have been freed by sta_info_insert()! 48d0709a65SJohannes Berg * 4934e89507SJohannes Berg * Station entries are added by mac80211 when you establish a link with a 507e189a12SLuis R. Rodriguez * peer. This means different things for the different type of interfaces 517e189a12SLuis R. Rodriguez * we support. For a regular station this mean we add the AP sta when we 5225985edcSLucas De Marchi * receive an association response from the AP. For IBSS this occurs when 5334e89507SJohannes Berg * get to know about a peer on the same IBSS. For WDS we add the sta for 5425985edcSLucas De Marchi * the peer immediately upon device open. When using AP mode we add stations 5534e89507SJohannes Berg * for each respective station upon request from userspace through nl80211. 567e189a12SLuis R. Rodriguez * 5734e89507SJohannes Berg * In order to remove a STA info structure, various sta_info_destroy_*() 5834e89507SJohannes Berg * calls are available. 59d0709a65SJohannes Berg * 6034e89507SJohannes Berg * There is no concept of ownership on a STA entry, each structure is 6134e89507SJohannes Berg * owned by the global hash table/list until it is removed. All users of 6234e89507SJohannes Berg * the structure need to be RCU protected so that the structure won't be 6334e89507SJohannes Berg * freed before they are done using it. 64d0709a65SJohannes Berg */ 65f0706e82SJiri Benc 664d33960bSJohannes Berg /* Caller must hold local->sta_mtx */ 67be8755e1SMichael Wu static int sta_info_hash_del(struct ieee80211_local *local, 68f0706e82SJiri Benc struct sta_info *sta) 69f0706e82SJiri Benc { 70f0706e82SJiri Benc struct sta_info *s; 71f0706e82SJiri Benc 7240b275b6SJohannes Berg s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)], 734d33960bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 74f0706e82SJiri Benc if (!s) 75be8755e1SMichael Wu return -ENOENT; 76be8755e1SMichael Wu if (s == sta) { 77cf778b00SEric Dumazet rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], 78d0709a65SJohannes Berg s->hnext); 79be8755e1SMichael Wu return 0; 80f0706e82SJiri Benc } 81f0706e82SJiri Benc 8240b275b6SJohannes Berg while (rcu_access_pointer(s->hnext) && 8340b275b6SJohannes Berg rcu_access_pointer(s->hnext) != sta) 8440b275b6SJohannes Berg s = rcu_dereference_protected(s->hnext, 854d33960bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 8640b275b6SJohannes Berg if (rcu_access_pointer(s->hnext)) { 87cf778b00SEric Dumazet rcu_assign_pointer(s->hnext, sta->hnext); 88be8755e1SMichael Wu return 0; 89f0706e82SJiri Benc } 90f0706e82SJiri Benc 91be8755e1SMichael Wu return -ENOENT; 92f0706e82SJiri Benc } 93f0706e82SJiri Benc 945108ca82SJohannes Berg static void __cleanup_single_sta(struct sta_info *sta) 95b22cfcfcSEliad Peller { 96b22cfcfcSEliad Peller int ac, i; 97b22cfcfcSEliad Peller struct tid_ampdu_tx *tid_tx; 98b22cfcfcSEliad Peller struct ieee80211_sub_if_data *sdata = sta->sdata; 99b22cfcfcSEliad Peller struct ieee80211_local *local = sdata->local; 100d012a605SMarco Porsch struct ps_data *ps; 101b22cfcfcSEliad Peller 102e3685e03SJohannes Berg if (test_sta_flag(sta, WLAN_STA_PS_STA) || 103e3685e03SJohannes Berg test_sta_flag(sta, WLAN_STA_PS_DRIVER)) { 104d012a605SMarco Porsch if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 105d012a605SMarco Porsch sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 106d012a605SMarco Porsch ps = &sdata->bss->ps; 1073f52b7e3SMarco Porsch else if (ieee80211_vif_is_mesh(&sdata->vif)) 1083f52b7e3SMarco Porsch ps = &sdata->u.mesh.ps; 109d012a605SMarco Porsch else 110d012a605SMarco Porsch return; 111b22cfcfcSEliad Peller 112b22cfcfcSEliad Peller clear_sta_flag(sta, WLAN_STA_PS_STA); 113e3685e03SJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 114b22cfcfcSEliad Peller 115d012a605SMarco Porsch atomic_dec(&ps->num_sta_ps); 116b22cfcfcSEliad Peller sta_info_recalc_tim(sta); 117b22cfcfcSEliad Peller } 118b22cfcfcSEliad Peller 119b22cfcfcSEliad Peller for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 120b22cfcfcSEliad Peller local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]); 1211f98ab7fSFelix Fietkau ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]); 1221f98ab7fSFelix Fietkau ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]); 123b22cfcfcSEliad Peller } 124b22cfcfcSEliad Peller 12545b5028eSThomas Pedersen if (ieee80211_vif_is_mesh(&sdata->vif)) 12645b5028eSThomas Pedersen mesh_sta_cleanup(sta); 127b22cfcfcSEliad Peller 128b22cfcfcSEliad Peller cancel_work_sync(&sta->drv_unblock_wk); 129b22cfcfcSEliad Peller 130b22cfcfcSEliad Peller /* 131b22cfcfcSEliad Peller * Destroy aggregation state here. It would be nice to wait for the 132b22cfcfcSEliad Peller * driver to finish aggregation stop and then clean up, but for now 133b22cfcfcSEliad Peller * drivers have to handle aggregation stop being requested, followed 134b22cfcfcSEliad Peller * directly by station destruction. 135b22cfcfcSEliad Peller */ 1365a306f58SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 137661eb381SJohannes Berg kfree(sta->ampdu_mlme.tid_start_tx[i]); 138b22cfcfcSEliad Peller tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]); 139b22cfcfcSEliad Peller if (!tid_tx) 140b22cfcfcSEliad Peller continue; 1411f98ab7fSFelix Fietkau ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending); 142b22cfcfcSEliad Peller kfree(tid_tx); 143b22cfcfcSEliad Peller } 1445108ca82SJohannes Berg } 145b22cfcfcSEliad Peller 1465108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta) 1475108ca82SJohannes Berg { 1485108ca82SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1495108ca82SJohannes Berg struct ieee80211_local *local = sdata->local; 1505108ca82SJohannes Berg 1515108ca82SJohannes Berg __cleanup_single_sta(sta); 152b22cfcfcSEliad Peller sta_info_free(local, sta); 153b22cfcfcSEliad Peller } 154b22cfcfcSEliad Peller 155d0709a65SJohannes Berg /* protected by RCU */ 156abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata, 157abe60632SJohannes Berg const u8 *addr) 15843ba7e95SJohannes Berg { 159abe60632SJohannes Berg struct ieee80211_local *local = sdata->local; 16043ba7e95SJohannes Berg struct sta_info *sta; 16143ba7e95SJohannes Berg 1620379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1630379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 16443ba7e95SJohannes Berg while (sta) { 165abe60632SJohannes Berg if (sta->sdata == sdata && 166b203ca39SJoe Perches ether_addr_equal(sta->sta.addr, addr)) 16743ba7e95SJohannes Berg break; 1680379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 1690379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 17043ba7e95SJohannes Berg } 17143ba7e95SJohannes Berg return sta; 17243ba7e95SJohannes Berg } 17343ba7e95SJohannes Berg 1740e5ded5aSFelix Fietkau /* 1750e5ded5aSFelix Fietkau * Get sta info either from the specified interface 1760e5ded5aSFelix Fietkau * or from one of its vlans 1770e5ded5aSFelix Fietkau */ 1780e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata, 1790e5ded5aSFelix Fietkau const u8 *addr) 1800e5ded5aSFelix Fietkau { 1810e5ded5aSFelix Fietkau struct ieee80211_local *local = sdata->local; 1820e5ded5aSFelix Fietkau struct sta_info *sta; 1830e5ded5aSFelix Fietkau 1840379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1850379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 1860e5ded5aSFelix Fietkau while (sta) { 1870e5ded5aSFelix Fietkau if ((sta->sdata == sdata || 188a2c1e3daSJohannes Berg (sta->sdata->bss && sta->sdata->bss == sdata->bss)) && 189b203ca39SJoe Perches ether_addr_equal(sta->sta.addr, addr)) 1900e5ded5aSFelix Fietkau break; 1910379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 1920379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 1930e5ded5aSFelix Fietkau } 1940e5ded5aSFelix Fietkau return sta; 1950e5ded5aSFelix Fietkau } 1960e5ded5aSFelix Fietkau 1973b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata, 1983b53fde8SJohannes Berg int idx) 199ee385855SLuis Carlos Cobo { 2003b53fde8SJohannes Berg struct ieee80211_local *local = sdata->local; 201ee385855SLuis Carlos Cobo struct sta_info *sta; 202ee385855SLuis Carlos Cobo int i = 0; 203ee385855SLuis Carlos Cobo 204d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) { 2053b53fde8SJohannes Berg if (sdata != sta->sdata) 2062a8ca29aSLuis Carlos Cobo continue; 207ee385855SLuis Carlos Cobo if (i < idx) { 208ee385855SLuis Carlos Cobo ++i; 209ee385855SLuis Carlos Cobo continue; 210ee385855SLuis Carlos Cobo } 2112a8ca29aSLuis Carlos Cobo return sta; 212ee385855SLuis Carlos Cobo } 213ee385855SLuis Carlos Cobo 214ee385855SLuis Carlos Cobo return NULL; 215ee385855SLuis Carlos Cobo } 216f0706e82SJiri Benc 21793e5deb1SJohannes Berg /** 218d9a7ddb0SJohannes Berg * sta_info_free - free STA 21993e5deb1SJohannes Berg * 2206ef307bcSRandy Dunlap * @local: pointer to the global information 22193e5deb1SJohannes Berg * @sta: STA info to free 22293e5deb1SJohannes Berg * 22393e5deb1SJohannes Berg * This function must undo everything done by sta_info_alloc() 224d9a7ddb0SJohannes Berg * that may happen before sta_info_insert(). It may only be 225d9a7ddb0SJohannes Berg * called when sta_info_insert() has not been attempted (and 226d9a7ddb0SJohannes Berg * if that fails, the station is freed anyway.) 22793e5deb1SJohannes Berg */ 228d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta) 22993e5deb1SJohannes Berg { 230ad38bfc9SMatti Gottlieb int i; 231ad38bfc9SMatti Gottlieb 232889cbb91SJohannes Berg if (sta->rate_ctrl) 2334b7679a5SJohannes Berg rate_control_free_sta(sta); 23493e5deb1SJohannes Berg 235ad38bfc9SMatti Gottlieb if (sta->tx_lat) { 236ad38bfc9SMatti Gottlieb for (i = 0; i < IEEE80211_NUM_TIDS; i++) 237ad38bfc9SMatti Gottlieb kfree(sta->tx_lat[i].bins); 238ad38bfc9SMatti Gottlieb kfree(sta->tx_lat); 239ad38bfc9SMatti Gottlieb } 240ad38bfc9SMatti Gottlieb 241bdcbd8e0SJohannes Berg sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr); 24293e5deb1SJohannes Berg 24393e5deb1SJohannes Berg kfree(sta); 24493e5deb1SJohannes Berg } 24593e5deb1SJohannes Berg 2464d33960bSJohannes Berg /* Caller must hold local->sta_mtx */ 247d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local, 248d0709a65SJohannes Berg struct sta_info *sta) 249f0706e82SJiri Benc { 2504d33960bSJohannes Berg lockdep_assert_held(&local->sta_mtx); 25117741cdcSJohannes Berg sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)]; 252cf778b00SEric Dumazet rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta); 253f0706e82SJiri Benc } 254f0706e82SJiri Benc 255af818581SJohannes Berg static void sta_unblock(struct work_struct *wk) 256af818581SJohannes Berg { 257af818581SJohannes Berg struct sta_info *sta; 258af818581SJohannes Berg 259af818581SJohannes Berg sta = container_of(wk, struct sta_info, drv_unblock_wk); 260af818581SJohannes Berg 261af818581SJohannes Berg if (sta->dead) 262af818581SJohannes Berg return; 263af818581SJohannes Berg 26454420473SHelmut Schaa if (!test_sta_flag(sta, WLAN_STA_PS_STA)) { 26554420473SHelmut Schaa local_bh_disable(); 266af818581SJohannes Berg ieee80211_sta_ps_deliver_wakeup(sta); 26754420473SHelmut Schaa local_bh_enable(); 26854420473SHelmut Schaa } else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) { 269c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 270ce662b44SJohannes Berg 271ce662b44SJohannes Berg local_bh_disable(); 272af818581SJohannes Berg ieee80211_sta_ps_deliver_poll_response(sta); 273ce662b44SJohannes Berg local_bh_enable(); 274c2c98fdeSJohannes Berg } else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) { 275c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 276ce662b44SJohannes Berg 277ce662b44SJohannes Berg local_bh_disable(); 27847086fc5SJohannes Berg ieee80211_sta_ps_deliver_uapsd(sta); 279ce662b44SJohannes Berg local_bh_enable(); 28050a9432dSJohannes Berg } else 281c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 282af818581SJohannes Berg } 283af818581SJohannes Berg 284af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local, 285af65cd96SJohannes Berg struct sta_info *sta, gfp_t gfp) 286af65cd96SJohannes Berg { 287af65cd96SJohannes Berg if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) 288af65cd96SJohannes Berg return 0; 289af65cd96SJohannes Berg 290889cbb91SJohannes Berg sta->rate_ctrl = local->rate_ctrl; 291af65cd96SJohannes Berg sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, 292af65cd96SJohannes Berg &sta->sta, gfp); 293889cbb91SJohannes Berg if (!sta->rate_ctrl_priv) 294af65cd96SJohannes Berg return -ENOMEM; 295af65cd96SJohannes Berg 296af65cd96SJohannes Berg return 0; 297af65cd96SJohannes Berg } 298af65cd96SJohannes Berg 29973651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, 30056544160SJohannes Berg const u8 *addr, gfp_t gfp) 301f0706e82SJiri Benc { 302d0709a65SJohannes Berg struct ieee80211_local *local = sdata->local; 303f0706e82SJiri Benc struct sta_info *sta; 304ebe27c91SMohammed Shafi Shajakhan struct timespec uptime; 305ad38bfc9SMatti Gottlieb struct ieee80211_tx_latency_bin_ranges *tx_latency; 30616c5f15cSRon Rindjunsky int i; 307f0706e82SJiri Benc 30817741cdcSJohannes Berg sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp); 309f0706e82SJiri Benc if (!sta) 31073651ee6SJohannes Berg return NULL; 311f0706e82SJiri Benc 312ef04a297SJohannes Berg rcu_read_lock(); 313ef04a297SJohannes Berg tx_latency = rcu_dereference(local->tx_latency); 314ef04a297SJohannes Berg /* init stations Tx latency statistics && TID bins */ 315ef04a297SJohannes Berg if (tx_latency) { 316ef04a297SJohannes Berg sta->tx_lat = kzalloc(IEEE80211_NUM_TIDS * 317ef04a297SJohannes Berg sizeof(struct ieee80211_tx_latency_stat), 318ef04a297SJohannes Berg GFP_ATOMIC); 319ef04a297SJohannes Berg if (!sta->tx_lat) { 320ef04a297SJohannes Berg rcu_read_unlock(); 321ef04a297SJohannes Berg goto free; 322ef04a297SJohannes Berg } 323ef04a297SJohannes Berg 324ef04a297SJohannes Berg if (tx_latency->n_ranges) { 325ef04a297SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 326ef04a297SJohannes Berg /* size of bins is size of the ranges +1 */ 327ef04a297SJohannes Berg sta->tx_lat[i].bin_count = 328ef04a297SJohannes Berg tx_latency->n_ranges + 1; 329ef04a297SJohannes Berg sta->tx_lat[i].bins = 330ef04a297SJohannes Berg kcalloc(sta->tx_lat[i].bin_count, 331ef04a297SJohannes Berg sizeof(u32), GFP_ATOMIC); 332ef04a297SJohannes Berg if (!sta->tx_lat[i].bins) { 333ef04a297SJohannes Berg rcu_read_unlock(); 334ef04a297SJohannes Berg goto free; 335ef04a297SJohannes Berg } 336ef04a297SJohannes Berg } 337ef04a297SJohannes Berg } 338ef04a297SJohannes Berg } 339ef04a297SJohannes Berg rcu_read_unlock(); 340ef04a297SJohannes Berg 34107346f81SJohannes Berg spin_lock_init(&sta->lock); 3421d147bfaSEmmanuel Grumbach spin_lock_init(&sta->ps_lock); 343af818581SJohannes Berg INIT_WORK(&sta->drv_unblock_wk, sta_unblock); 34467c282c0SJohannes Berg INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work); 345a93e3644SJohannes Berg mutex_init(&sta->ampdu_mlme.mtx); 34687f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH 34787f59c70SThomas Pedersen if (ieee80211_vif_is_mesh(&sdata->vif) && 34887f59c70SThomas Pedersen !sdata->u.mesh.user_mpm) 34987f59c70SThomas Pedersen init_timer(&sta->plink_timer); 3506c7c4cbfSThomas Pedersen sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE; 35187f59c70SThomas Pedersen #endif 35207346f81SJohannes Berg 35317741cdcSJohannes Berg memcpy(sta->sta.addr, addr, ETH_ALEN); 354d0709a65SJohannes Berg sta->local = local; 355d0709a65SJohannes Berg sta->sdata = sdata; 3568bc8aecdSFelix Fietkau sta->last_rx = jiffies; 357f0706e82SJiri Benc 35871ec375cSJohannes Berg sta->sta_state = IEEE80211_STA_NONE; 35971ec375cSJohannes Berg 360ebe27c91SMohammed Shafi Shajakhan do_posix_clock_monotonic_gettime(&uptime); 361ebe27c91SMohammed Shafi Shajakhan sta->last_connected = uptime.tv_sec; 362541a45a1SBruno Randolf ewma_init(&sta->avg_signal, 1024, 8); 363ef0621e8SFelix Fietkau for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++) 364ef0621e8SFelix Fietkau ewma_init(&sta->chain_signal_avg[i], 1024, 8); 365541a45a1SBruno Randolf 366ef04a297SJohannes Berg if (sta_prepare_rate_control(local, sta, gfp)) 367ef04a297SJohannes Berg goto free; 368f0706e82SJiri Benc 3695a306f58SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 370a622ab72SJohannes Berg /* 371a622ab72SJohannes Berg * timer_to_tid must be initialized with identity mapping 372a622ab72SJohannes Berg * to enable session_timer's data differentiation. See 373a622ab72SJohannes Berg * sta_rx_agg_session_timer_expired for usage. 374a622ab72SJohannes Berg */ 37516c5f15cSRon Rindjunsky sta->timer_to_tid[i] = i; 37616c5f15cSRon Rindjunsky } 377948d887dSJohannes Berg for (i = 0; i < IEEE80211_NUM_ACS; i++) { 378948d887dSJohannes Berg skb_queue_head_init(&sta->ps_tx_buf[i]); 379948d887dSJohannes Berg skb_queue_head_init(&sta->tx_filtered[i]); 380948d887dSJohannes Berg } 38173651ee6SJohannes Berg 3825a306f58SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) 3834be929beSAlexey Dobriyan sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX); 384cccaec98SSenthil Balasubramanian 385af0ed69bSJohannes Berg sta->sta.smps_mode = IEEE80211_SMPS_OFF; 386687da132SEmmanuel Grumbach if (sdata->vif.type == NL80211_IFTYPE_AP || 387687da132SEmmanuel Grumbach sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 388687da132SEmmanuel Grumbach struct ieee80211_supported_band *sband = 389687da132SEmmanuel Grumbach local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)]; 390687da132SEmmanuel Grumbach u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 391687da132SEmmanuel Grumbach IEEE80211_HT_CAP_SM_PS_SHIFT; 392687da132SEmmanuel Grumbach /* 393687da132SEmmanuel Grumbach * Assume that hostapd advertises our caps in the beacon and 394687da132SEmmanuel Grumbach * this is the known_smps_mode for a station that just assciated 395687da132SEmmanuel Grumbach */ 396687da132SEmmanuel Grumbach switch (smps) { 397687da132SEmmanuel Grumbach case WLAN_HT_SMPS_CONTROL_DISABLED: 398687da132SEmmanuel Grumbach sta->known_smps_mode = IEEE80211_SMPS_OFF; 399687da132SEmmanuel Grumbach break; 400687da132SEmmanuel Grumbach case WLAN_HT_SMPS_CONTROL_STATIC: 401687da132SEmmanuel Grumbach sta->known_smps_mode = IEEE80211_SMPS_STATIC; 402687da132SEmmanuel Grumbach break; 403687da132SEmmanuel Grumbach case WLAN_HT_SMPS_CONTROL_DYNAMIC: 404687da132SEmmanuel Grumbach sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC; 405687da132SEmmanuel Grumbach break; 406687da132SEmmanuel Grumbach default: 407687da132SEmmanuel Grumbach WARN_ON(1); 408687da132SEmmanuel Grumbach } 409687da132SEmmanuel Grumbach } 410af0ed69bSJohannes Berg 411bdcbd8e0SJohannes Berg sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr); 41273651ee6SJohannes Berg return sta; 413ef04a297SJohannes Berg 414ef04a297SJohannes Berg free: 415ef04a297SJohannes Berg if (sta->tx_lat) { 416ef04a297SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) 417ef04a297SJohannes Berg kfree(sta->tx_lat[i].bins); 418ef04a297SJohannes Berg kfree(sta->tx_lat); 419ef04a297SJohannes Berg } 420ef04a297SJohannes Berg kfree(sta); 421ef04a297SJohannes Berg return NULL; 42273651ee6SJohannes Berg } 42373651ee6SJohannes Berg 4248c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta) 42534e89507SJohannes Berg { 42634e89507SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 42734e89507SJohannes Berg 42803e4497eSJohannes Berg /* 42903e4497eSJohannes Berg * Can't be a WARN_ON because it can be triggered through a race: 43003e4497eSJohannes Berg * something inserts a STA (on one CPU) without holding the RTNL 43103e4497eSJohannes Berg * and another CPU turns off the net device. 43203e4497eSJohannes Berg */ 4338c71df7aSGuy Eilam if (unlikely(!ieee80211_sdata_running(sdata))) 4348c71df7aSGuy Eilam return -ENETDOWN; 43503e4497eSJohannes Berg 436b203ca39SJoe Perches if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) || 4378c71df7aSGuy Eilam is_multicast_ether_addr(sta->sta.addr))) 4388c71df7aSGuy Eilam return -EINVAL; 4398c71df7aSGuy Eilam 4408c71df7aSGuy Eilam return 0; 44193e5deb1SJohannes Berg } 44244213b5eSJohannes Berg 443f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local, 444f09603a2SJohannes Berg struct ieee80211_sub_if_data *sdata, 445f09603a2SJohannes Berg struct sta_info *sta) 446f09603a2SJohannes Berg { 447f09603a2SJohannes Berg enum ieee80211_sta_state state; 448f09603a2SJohannes Berg int err = 0; 449f09603a2SJohannes Berg 450f09603a2SJohannes Berg for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) { 451f09603a2SJohannes Berg err = drv_sta_state(local, sdata, sta, state, state + 1); 452f09603a2SJohannes Berg if (err) 453f09603a2SJohannes Berg break; 454f09603a2SJohannes Berg } 455f09603a2SJohannes Berg 456f09603a2SJohannes Berg if (!err) { 457a4ec45a4SJohannes Berg /* 458a4ec45a4SJohannes Berg * Drivers using legacy sta_add/sta_remove callbacks only 459a4ec45a4SJohannes Berg * get uploaded set to true after sta_add is called. 460a4ec45a4SJohannes Berg */ 461a4ec45a4SJohannes Berg if (!local->ops->sta_add) 462f09603a2SJohannes Berg sta->uploaded = true; 463f09603a2SJohannes Berg return 0; 464f09603a2SJohannes Berg } 465f09603a2SJohannes Berg 466f09603a2SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { 467bdcbd8e0SJohannes Berg sdata_info(sdata, 468bdcbd8e0SJohannes Berg "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n", 469bdcbd8e0SJohannes Berg sta->sta.addr, state + 1, err); 470f09603a2SJohannes Berg err = 0; 471f09603a2SJohannes Berg } 472f09603a2SJohannes Berg 473f09603a2SJohannes Berg /* unwind on error */ 474f09603a2SJohannes Berg for (; state > IEEE80211_STA_NOTEXIST; state--) 475f09603a2SJohannes Berg WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1)); 476f09603a2SJohannes Berg 477f09603a2SJohannes Berg return err; 478f09603a2SJohannes Berg } 479f09603a2SJohannes Berg 48034e89507SJohannes Berg /* 4818c71df7aSGuy Eilam * should be called with sta_mtx locked 4828c71df7aSGuy Eilam * this function replaces the mutex lock 4838c71df7aSGuy Eilam * with a RCU lock 4848c71df7aSGuy Eilam */ 4854d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU) 4868c71df7aSGuy Eilam { 4878c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 4888c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 4897852e361SJohannes Berg struct station_info sinfo; 4908c71df7aSGuy Eilam int err = 0; 4918c71df7aSGuy Eilam 4928c71df7aSGuy Eilam lockdep_assert_held(&local->sta_mtx); 4938c71df7aSGuy Eilam 4947852e361SJohannes Berg /* check if STA exists already */ 4957852e361SJohannes Berg if (sta_info_get_bss(sdata, sta->sta.addr)) { 4964d33960bSJohannes Berg err = -EEXIST; 4974d33960bSJohannes Berg goto out_err; 49834e89507SJohannes Berg } 49934e89507SJohannes Berg 5004d33960bSJohannes Berg local->num_sta++; 5014d33960bSJohannes Berg local->sta_generation++; 5024d33960bSJohannes Berg smp_mb(); 5034d33960bSJohannes Berg 5045108ca82SJohannes Berg /* simplify things and don't accept BA sessions yet */ 5055108ca82SJohannes Berg set_sta_flag(sta, WLAN_STA_BLOCK_BA); 5065108ca82SJohannes Berg 5074d33960bSJohannes Berg /* make the station visible */ 5084d33960bSJohannes Berg sta_info_hash_add(local, sta); 5094d33960bSJohannes Berg 510794454ceSArik Nemtsov list_add_rcu(&sta->list, &local->sta_list); 51183d5cc01SJohannes Berg 5125108ca82SJohannes Berg /* notify driver */ 5135108ca82SJohannes Berg err = sta_info_insert_drv_state(local, sdata, sta); 5145108ca82SJohannes Berg if (err) 5155108ca82SJohannes Berg goto out_remove; 5165108ca82SJohannes Berg 51783d5cc01SJohannes Berg set_sta_flag(sta, WLAN_STA_INSERTED); 5185108ca82SJohannes Berg /* accept BA sessions now */ 5195108ca82SJohannes Berg clear_sta_flag(sta, WLAN_STA_BLOCK_BA); 5204d33960bSJohannes Berg 52121f659bfSEliad Peller ieee80211_recalc_min_chandef(sdata); 5224d33960bSJohannes Berg ieee80211_sta_debugfs_add(sta); 5234d33960bSJohannes Berg rate_control_add_sta_debugfs(sta); 5244d33960bSJohannes Berg 5254d33960bSJohannes Berg memset(&sinfo, 0, sizeof(sinfo)); 5264d33960bSJohannes Berg sinfo.filled = 0; 5274d33960bSJohannes Berg sinfo.generation = local->sta_generation; 5284d33960bSJohannes Berg cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL); 529d0709a65SJohannes Berg 530bdcbd8e0SJohannes Berg sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr); 531f0706e82SJiri Benc 53234e89507SJohannes Berg /* move reference to rcu-protected */ 53334e89507SJohannes Berg rcu_read_lock(); 53434e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 535e9f207f0SJiri Benc 53673651ee6SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 53773651ee6SJohannes Berg mesh_accept_plinks_update(sdata); 53873651ee6SJohannes Berg 53973651ee6SJohannes Berg return 0; 5405108ca82SJohannes Berg out_remove: 5415108ca82SJohannes Berg sta_info_hash_del(local, sta); 5425108ca82SJohannes Berg list_del_rcu(&sta->list); 5435108ca82SJohannes Berg local->num_sta--; 5445108ca82SJohannes Berg synchronize_net(); 5455108ca82SJohannes Berg __cleanup_single_sta(sta); 5464d33960bSJohannes Berg out_err: 5474d33960bSJohannes Berg mutex_unlock(&local->sta_mtx); 5484d33960bSJohannes Berg rcu_read_lock(); 5494d33960bSJohannes Berg return err; 5508c71df7aSGuy Eilam } 5518c71df7aSGuy Eilam 5528c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) 5538c71df7aSGuy Eilam { 5548c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 555*308f7fcfSZhao, Gang int err; 5568c71df7aSGuy Eilam 5574d33960bSJohannes Berg might_sleep(); 5584d33960bSJohannes Berg 5598c71df7aSGuy Eilam err = sta_info_insert_check(sta); 5608c71df7aSGuy Eilam if (err) { 5618c71df7aSGuy Eilam rcu_read_lock(); 5628c71df7aSGuy Eilam goto out_free; 5638c71df7aSGuy Eilam } 5648c71df7aSGuy Eilam 565004c872eSJohannes Berg mutex_lock(&local->sta_mtx); 566004c872eSJohannes Berg 5674d33960bSJohannes Berg err = sta_info_insert_finish(sta); 5688c71df7aSGuy Eilam if (err) 569004c872eSJohannes Berg goto out_free; 570d0709a65SJohannes Berg 571004c872eSJohannes Berg return 0; 57293e5deb1SJohannes Berg out_free: 573d9a7ddb0SJohannes Berg sta_info_free(local, sta); 57493e5deb1SJohannes Berg return err; 575f0706e82SJiri Benc } 576f0706e82SJiri Benc 57734e89507SJohannes Berg int sta_info_insert(struct sta_info *sta) 57834e89507SJohannes Berg { 57934e89507SJohannes Berg int err = sta_info_insert_rcu(sta); 58034e89507SJohannes Berg 58134e89507SJohannes Berg rcu_read_unlock(); 58234e89507SJohannes Berg 58334e89507SJohannes Berg return err; 58434e89507SJohannes Berg } 58534e89507SJohannes Berg 586d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id) 587004c872eSJohannes Berg { 588004c872eSJohannes Berg /* 589004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 590004c872eSJohannes Berg * so this line may not be changed to use the __set_bit() format. 591004c872eSJohannes Berg */ 592d012a605SMarco Porsch tim[id / 8] |= (1 << (id % 8)); 593004c872eSJohannes Berg } 594004c872eSJohannes Berg 595d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id) 596004c872eSJohannes Berg { 597004c872eSJohannes Berg /* 598004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 599004c872eSJohannes Berg * so this line may not be changed to use the __clear_bit() format. 600004c872eSJohannes Berg */ 601d012a605SMarco Porsch tim[id / 8] &= ~(1 << (id % 8)); 602004c872eSJohannes Berg } 603004c872eSJohannes Berg 6043d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id) 6053d5839b6SIlan Peer { 6063d5839b6SIlan Peer /* 6073d5839b6SIlan Peer * This format has been mandated by the IEEE specifications, 6083d5839b6SIlan Peer * so this line may not be changed to use the test_bit() format. 6093d5839b6SIlan Peer */ 6103d5839b6SIlan Peer return tim[id / 8] & (1 << (id % 8)); 6113d5839b6SIlan Peer } 6123d5839b6SIlan Peer 613948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac) 614004c872eSJohannes Berg { 615948d887dSJohannes Berg /* If we ever support TIDs > 7, this obviously needs to be adjusted */ 616948d887dSJohannes Berg switch (ac) { 617948d887dSJohannes Berg case IEEE80211_AC_VO: 618948d887dSJohannes Berg return BIT(6) | BIT(7); 619948d887dSJohannes Berg case IEEE80211_AC_VI: 620948d887dSJohannes Berg return BIT(4) | BIT(5); 621948d887dSJohannes Berg case IEEE80211_AC_BE: 622948d887dSJohannes Berg return BIT(0) | BIT(3); 623948d887dSJohannes Berg case IEEE80211_AC_BK: 624948d887dSJohannes Berg return BIT(1) | BIT(2); 625948d887dSJohannes Berg default: 626948d887dSJohannes Berg WARN_ON(1); 627948d887dSJohannes Berg return 0; 628d0709a65SJohannes Berg } 629004c872eSJohannes Berg } 630004c872eSJohannes Berg 631c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta) 632004c872eSJohannes Berg { 633c868cb35SJohannes Berg struct ieee80211_local *local = sta->local; 634d012a605SMarco Porsch struct ps_data *ps; 635948d887dSJohannes Berg bool indicate_tim = false; 636948d887dSJohannes Berg u8 ignore_for_tim = sta->sta.uapsd_queues; 637948d887dSJohannes Berg int ac; 638d012a605SMarco Porsch u16 id; 639004c872eSJohannes Berg 640d012a605SMarco Porsch if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 641d012a605SMarco Porsch sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 642c868cb35SJohannes Berg if (WARN_ON_ONCE(!sta->sdata->bss)) 643c868cb35SJohannes Berg return; 6443e122be0SJohannes Berg 645d012a605SMarco Porsch ps = &sta->sdata->bss->ps; 646d012a605SMarco Porsch id = sta->sta.aid; 6473f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH 6483f52b7e3SMarco Porsch } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) { 6493f52b7e3SMarco Porsch ps = &sta->sdata->u.mesh.ps; 650204d1304SThomas Pedersen /* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */ 6516f101ef0SChun-Yeow Yeoh id = sta->plid % (IEEE80211_MAX_AID + 1); 6523f52b7e3SMarco Porsch #endif 653d012a605SMarco Porsch } else { 654d012a605SMarco Porsch return; 655d012a605SMarco Porsch } 656d012a605SMarco Porsch 657c868cb35SJohannes Berg /* No need to do anything if the driver does all */ 658c868cb35SJohannes Berg if (local->hw.flags & IEEE80211_HW_AP_LINK_PS) 659c868cb35SJohannes Berg return; 660004c872eSJohannes Berg 661c868cb35SJohannes Berg if (sta->dead) 662c868cb35SJohannes Berg goto done; 6633e122be0SJohannes Berg 664948d887dSJohannes Berg /* 665948d887dSJohannes Berg * If all ACs are delivery-enabled then we should build 666948d887dSJohannes Berg * the TIM bit for all ACs anyway; if only some are then 667948d887dSJohannes Berg * we ignore those and build the TIM bit using only the 668948d887dSJohannes Berg * non-enabled ones. 669948d887dSJohannes Berg */ 670948d887dSJohannes Berg if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1) 671948d887dSJohannes Berg ignore_for_tim = 0; 672948d887dSJohannes Berg 673948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 674948d887dSJohannes Berg unsigned long tids; 675948d887dSJohannes Berg 676948d887dSJohannes Berg if (ignore_for_tim & BIT(ac)) 677948d887dSJohannes Berg continue; 678948d887dSJohannes Berg 679948d887dSJohannes Berg indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) || 680948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac]); 681948d887dSJohannes Berg if (indicate_tim) 682948d887dSJohannes Berg break; 683948d887dSJohannes Berg 684948d887dSJohannes Berg tids = ieee80211_tids_for_ac(ac); 685948d887dSJohannes Berg 686948d887dSJohannes Berg indicate_tim |= 687948d887dSJohannes Berg sta->driver_buffered_tids & tids; 688004c872eSJohannes Berg } 689004c872eSJohannes Berg 690c868cb35SJohannes Berg done: 69165f704a5SJohannes Berg spin_lock_bh(&local->tim_lock); 692004c872eSJohannes Berg 6933d5839b6SIlan Peer if (indicate_tim == __bss_tim_get(ps->tim, id)) 6943d5839b6SIlan Peer goto out_unlock; 6953d5839b6SIlan Peer 696948d887dSJohannes Berg if (indicate_tim) 697d012a605SMarco Porsch __bss_tim_set(ps->tim, id); 698c868cb35SJohannes Berg else 699d012a605SMarco Porsch __bss_tim_clear(ps->tim, id); 7003e122be0SJohannes Berg 701c868cb35SJohannes Berg if (local->ops->set_tim) { 702c868cb35SJohannes Berg local->tim_in_locked_section = true; 703948d887dSJohannes Berg drv_set_tim(local, &sta->sta, indicate_tim); 704c868cb35SJohannes Berg local->tim_in_locked_section = false; 705004c872eSJohannes Berg } 706004c872eSJohannes Berg 7073d5839b6SIlan Peer out_unlock: 70865f704a5SJohannes Berg spin_unlock_bh(&local->tim_lock); 709004c872eSJohannes Berg } 710004c872eSJohannes Berg 711cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb) 712f0706e82SJiri Benc { 713e039fa4aSJohannes Berg struct ieee80211_tx_info *info; 714f0706e82SJiri Benc int timeout; 715f0706e82SJiri Benc 716f0706e82SJiri Benc if (!skb) 717cd0b8d89SJohannes Berg return false; 718f0706e82SJiri Benc 719e039fa4aSJohannes Berg info = IEEE80211_SKB_CB(skb); 720f0706e82SJiri Benc 721f0706e82SJiri Benc /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ 72257c4d7b4SJohannes Berg timeout = (sta->listen_interval * 72357c4d7b4SJohannes Berg sta->sdata->vif.bss_conf.beacon_int * 72457c4d7b4SJohannes Berg 32 / 15625) * HZ; 725f0706e82SJiri Benc if (timeout < STA_TX_BUFFER_EXPIRE) 726f0706e82SJiri Benc timeout = STA_TX_BUFFER_EXPIRE; 727e039fa4aSJohannes Berg return time_after(jiffies, info->control.jiffies + timeout); 728f0706e82SJiri Benc } 729f0706e82SJiri Benc 730f0706e82SJiri Benc 731948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local, 732948d887dSJohannes Berg struct sta_info *sta, int ac) 733f0706e82SJiri Benc { 734f0706e82SJiri Benc unsigned long flags; 735f0706e82SJiri Benc struct sk_buff *skb; 736f0706e82SJiri Benc 73760750397SJohannes Berg /* 73860750397SJohannes Berg * First check for frames that should expire on the filtered 73960750397SJohannes Berg * queue. Frames here were rejected by the driver and are on 74060750397SJohannes Berg * a separate queue to avoid reordering with normal PS-buffered 74160750397SJohannes Berg * frames. They also aren't accounted for right now in the 74260750397SJohannes Berg * total_ps_buffered counter. 74360750397SJohannes Berg */ 744f0706e82SJiri Benc for (;;) { 745948d887dSJohannes Berg spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 746948d887dSJohannes Berg skb = skb_peek(&sta->tx_filtered[ac]); 74757c4d7b4SJohannes Berg if (sta_info_buffer_expired(sta, skb)) 748948d887dSJohannes Berg skb = __skb_dequeue(&sta->tx_filtered[ac]); 749836341a7SJohannes Berg else 750f0706e82SJiri Benc skb = NULL; 751948d887dSJohannes Berg spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 752f0706e82SJiri Benc 75360750397SJohannes Berg /* 75460750397SJohannes Berg * Frames are queued in order, so if this one 75560750397SJohannes Berg * hasn't expired yet we can stop testing. If 75660750397SJohannes Berg * we actually reached the end of the queue we 75760750397SJohannes Berg * also need to stop, of course. 75860750397SJohannes Berg */ 75960750397SJohannes Berg if (!skb) 76060750397SJohannes Berg break; 761d4fa14cdSFelix Fietkau ieee80211_free_txskb(&local->hw, skb); 76260750397SJohannes Berg } 76360750397SJohannes Berg 76460750397SJohannes Berg /* 76560750397SJohannes Berg * Now also check the normal PS-buffered queue, this will 76660750397SJohannes Berg * only find something if the filtered queue was emptied 76760750397SJohannes Berg * since the filtered frames are all before the normal PS 76860750397SJohannes Berg * buffered frames. 76960750397SJohannes Berg */ 770f0706e82SJiri Benc for (;;) { 771948d887dSJohannes Berg spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 772948d887dSJohannes Berg skb = skb_peek(&sta->ps_tx_buf[ac]); 773f0706e82SJiri Benc if (sta_info_buffer_expired(sta, skb)) 774948d887dSJohannes Berg skb = __skb_dequeue(&sta->ps_tx_buf[ac]); 775f0706e82SJiri Benc else 776f0706e82SJiri Benc skb = NULL; 777948d887dSJohannes Berg spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 778f0706e82SJiri Benc 77960750397SJohannes Berg /* 78060750397SJohannes Berg * frames are queued in order, so if this one 78160750397SJohannes Berg * hasn't expired yet (or we reached the end of 78260750397SJohannes Berg * the queue) we can stop testing 78360750397SJohannes Berg */ 784836341a7SJohannes Berg if (!skb) 785836341a7SJohannes Berg break; 786836341a7SJohannes Berg 787f0706e82SJiri Benc local->total_ps_buffered--; 788bdcbd8e0SJohannes Berg ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n", 789bdcbd8e0SJohannes Berg sta->sta.addr); 790d4fa14cdSFelix Fietkau ieee80211_free_txskb(&local->hw, skb); 791f0706e82SJiri Benc } 7923393a608SJuuso Oikarinen 79360750397SJohannes Berg /* 79460750397SJohannes Berg * Finally, recalculate the TIM bit for this station -- it might 79560750397SJohannes Berg * now be clear because the station was too slow to retrieve its 79660750397SJohannes Berg * frames. 79760750397SJohannes Berg */ 79860750397SJohannes Berg sta_info_recalc_tim(sta); 79960750397SJohannes Berg 80060750397SJohannes Berg /* 80160750397SJohannes Berg * Return whether there are any frames still buffered, this is 80260750397SJohannes Berg * used to check whether the cleanup timer still needs to run, 80360750397SJohannes Berg * if there are no frames we don't need to rearm the timer. 80460750397SJohannes Berg */ 805948d887dSJohannes Berg return !(skb_queue_empty(&sta->ps_tx_buf[ac]) && 806948d887dSJohannes Berg skb_queue_empty(&sta->tx_filtered[ac])); 807948d887dSJohannes Berg } 808948d887dSJohannes Berg 809948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local, 810948d887dSJohannes Berg struct sta_info *sta) 811948d887dSJohannes Berg { 812948d887dSJohannes Berg bool have_buffered = false; 813948d887dSJohannes Berg int ac; 814948d887dSJohannes Berg 8153f52b7e3SMarco Porsch /* This is only necessary for stations on BSS/MBSS interfaces */ 8163f52b7e3SMarco Porsch if (!sta->sdata->bss && 8173f52b7e3SMarco Porsch !ieee80211_vif_is_mesh(&sta->sdata->vif)) 818948d887dSJohannes Berg return false; 819948d887dSJohannes Berg 820948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 821948d887dSJohannes Berg have_buffered |= 822948d887dSJohannes Berg sta_info_cleanup_expire_buffered_ac(local, sta, ac); 823948d887dSJohannes Berg 824948d887dSJohannes Berg return have_buffered; 825f0706e82SJiri Benc } 826f0706e82SJiri Benc 827d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta) 82834e89507SJohannes Berg { 82934e89507SJohannes Berg struct ieee80211_local *local; 83034e89507SJohannes Berg struct ieee80211_sub_if_data *sdata; 8316d10e46bSJohannes Berg int ret; 83234e89507SJohannes Berg 83334e89507SJohannes Berg might_sleep(); 83434e89507SJohannes Berg 83534e89507SJohannes Berg if (!sta) 83634e89507SJohannes Berg return -ENOENT; 83734e89507SJohannes Berg 83834e89507SJohannes Berg local = sta->local; 83934e89507SJohannes Berg sdata = sta->sdata; 84034e89507SJohannes Berg 84183d5cc01SJohannes Berg lockdep_assert_held(&local->sta_mtx); 84283d5cc01SJohannes Berg 843098a6070SJohannes Berg /* 844098a6070SJohannes Berg * Before removing the station from the driver and 845098a6070SJohannes Berg * rate control, it might still start new aggregation 846098a6070SJohannes Berg * sessions -- block that to make sure the tear-down 847098a6070SJohannes Berg * will be sufficient. 848098a6070SJohannes Berg */ 849c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_BLOCK_BA); 850c82c4a80SJohannes Berg ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA); 851098a6070SJohannes Berg 85234e89507SJohannes Berg ret = sta_info_hash_del(local, sta); 853b01711beSJohannes Berg if (WARN_ON(ret)) 85434e89507SJohannes Berg return ret; 85534e89507SJohannes Berg 856794454ceSArik Nemtsov list_del_rcu(&sta->list); 8574d33960bSJohannes Berg 8586a9d1b91SJohannes Berg drv_sta_pre_rcu_remove(local, sta->sdata, sta); 8596a9d1b91SJohannes Berg 860a710c816SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 861a710c816SJohannes Berg rcu_access_pointer(sdata->u.vlan.sta) == sta) 862a710c816SJohannes Berg RCU_INIT_POINTER(sdata->u.vlan.sta, NULL); 863a710c816SJohannes Berg 864d778207bSJohannes Berg return 0; 865d778207bSJohannes Berg } 866d778207bSJohannes Berg 867d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta) 868d778207bSJohannes Berg { 869d778207bSJohannes Berg struct ieee80211_local *local = sta->local; 870d778207bSJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 871d778207bSJohannes Berg int ret; 872d778207bSJohannes Berg 873d778207bSJohannes Berg /* 874d778207bSJohannes Berg * NOTE: This assumes at least synchronize_net() was done 875d778207bSJohannes Berg * after _part1 and before _part2! 876d778207bSJohannes Berg */ 877d778207bSJohannes Berg 878d778207bSJohannes Berg might_sleep(); 879d778207bSJohannes Berg lockdep_assert_held(&local->sta_mtx); 880d778207bSJohannes Berg 881c8782078SJohannes Berg /* now keys can no longer be reached */ 8826d10e46bSJohannes Berg ieee80211_free_sta_keys(local, sta); 88334e89507SJohannes Berg 88434e89507SJohannes Berg sta->dead = true; 88534e89507SJohannes Berg 88634e89507SJohannes Berg local->num_sta--; 88734e89507SJohannes Berg local->sta_generation++; 88834e89507SJohannes Berg 88983d5cc01SJohannes Berg while (sta->sta_state > IEEE80211_STA_NONE) { 890f09603a2SJohannes Berg ret = sta_info_move_state(sta, sta->sta_state - 1); 891f09603a2SJohannes Berg if (ret) { 89283d5cc01SJohannes Berg WARN_ON_ONCE(1); 89383d5cc01SJohannes Berg break; 89483d5cc01SJohannes Berg } 89583d5cc01SJohannes Berg } 896d9a7ddb0SJohannes Berg 897f09603a2SJohannes Berg if (sta->uploaded) { 898f09603a2SJohannes Berg ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE, 899f09603a2SJohannes Berg IEEE80211_STA_NOTEXIST); 900f09603a2SJohannes Berg WARN_ON_ONCE(ret != 0); 901f09603a2SJohannes Berg } 90234e89507SJohannes Berg 903bdcbd8e0SJohannes Berg sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr); 904bdcbd8e0SJohannes Berg 905ec15e68bSJouni Malinen cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL); 906ec15e68bSJouni Malinen 90734e89507SJohannes Berg rate_control_remove_sta_debugfs(sta); 90834e89507SJohannes Berg ieee80211_sta_debugfs_remove(sta); 90921f659bfSEliad Peller ieee80211_recalc_min_chandef(sdata); 91034e89507SJohannes Berg 911d34ba216SJohannes Berg cleanup_single_sta(sta); 912d778207bSJohannes Berg } 913d778207bSJohannes Berg 914d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta) 915d778207bSJohannes Berg { 916d778207bSJohannes Berg int err = __sta_info_destroy_part1(sta); 917d778207bSJohannes Berg 918d778207bSJohannes Berg if (err) 919d778207bSJohannes Berg return err; 920d778207bSJohannes Berg 921d778207bSJohannes Berg synchronize_net(); 922d778207bSJohannes Berg 923d778207bSJohannes Berg __sta_info_destroy_part2(sta); 92434e89507SJohannes Berg 92534e89507SJohannes Berg return 0; 92634e89507SJohannes Berg } 92734e89507SJohannes Berg 92834e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr) 92934e89507SJohannes Berg { 93034e89507SJohannes Berg struct sta_info *sta; 93134e89507SJohannes Berg int ret; 93234e89507SJohannes Berg 93334e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 9347852e361SJohannes Berg sta = sta_info_get(sdata, addr); 93534e89507SJohannes Berg ret = __sta_info_destroy(sta); 93634e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 93734e89507SJohannes Berg 93834e89507SJohannes Berg return ret; 93934e89507SJohannes Berg } 94034e89507SJohannes Berg 94134e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata, 94234e89507SJohannes Berg const u8 *addr) 94334e89507SJohannes Berg { 94434e89507SJohannes Berg struct sta_info *sta; 94534e89507SJohannes Berg int ret; 94634e89507SJohannes Berg 94734e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 9487852e361SJohannes Berg sta = sta_info_get_bss(sdata, addr); 94934e89507SJohannes Berg ret = __sta_info_destroy(sta); 95034e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 95134e89507SJohannes Berg 95234e89507SJohannes Berg return ret; 95334e89507SJohannes Berg } 954f0706e82SJiri Benc 955f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data) 956f0706e82SJiri Benc { 957f0706e82SJiri Benc struct ieee80211_local *local = (struct ieee80211_local *) data; 958f0706e82SJiri Benc struct sta_info *sta; 9593393a608SJuuso Oikarinen bool timer_needed = false; 960f0706e82SJiri Benc 961d0709a65SJohannes Berg rcu_read_lock(); 962d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) 9633393a608SJuuso Oikarinen if (sta_info_cleanup_expire_buffered(local, sta)) 9643393a608SJuuso Oikarinen timer_needed = true; 965d0709a65SJohannes Berg rcu_read_unlock(); 966f0706e82SJiri Benc 9675bb644a0SJohannes Berg if (local->quiescing) 9685bb644a0SJohannes Berg return; 9695bb644a0SJohannes Berg 9703393a608SJuuso Oikarinen if (!timer_needed) 9713393a608SJuuso Oikarinen return; 9723393a608SJuuso Oikarinen 97326d59535SJohannes Berg mod_timer(&local->sta_cleanup, 97426d59535SJohannes Berg round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); 975f0706e82SJiri Benc } 976f0706e82SJiri Benc 977f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local) 978f0706e82SJiri Benc { 9794d33960bSJohannes Berg spin_lock_init(&local->tim_lock); 98034e89507SJohannes Berg mutex_init(&local->sta_mtx); 981f0706e82SJiri Benc INIT_LIST_HEAD(&local->sta_list); 982f0706e82SJiri Benc 983b24b8a24SPavel Emelyanov setup_timer(&local->sta_cleanup, sta_info_cleanup, 984b24b8a24SPavel Emelyanov (unsigned long)local); 985f0706e82SJiri Benc } 986f0706e82SJiri Benc 987f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local) 988f0706e82SJiri Benc { 989a56f992cSJohannes Berg del_timer_sync(&local->sta_cleanup); 990f0706e82SJiri Benc } 991f0706e82SJiri Benc 992051007d9SJohannes Berg 993e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans) 994f0706e82SJiri Benc { 995b998e8bbSJohannes Berg struct ieee80211_local *local = sdata->local; 996f0706e82SJiri Benc struct sta_info *sta, *tmp; 997d778207bSJohannes Berg LIST_HEAD(free_list); 99844213b5eSJohannes Berg int ret = 0; 999f0706e82SJiri Benc 1000d0709a65SJohannes Berg might_sleep(); 1001d0709a65SJohannes Berg 1002e716251dSJohannes Berg WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP); 1003e716251dSJohannes Berg WARN_ON(vlans && !sdata->bss); 1004e716251dSJohannes Berg 100534e89507SJohannes Berg mutex_lock(&local->sta_mtx); 100634e89507SJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 1007e716251dSJohannes Berg if (sdata == sta->sdata || 1008e716251dSJohannes Berg (vlans && sdata->bss == sta->sdata->bss)) { 1009d778207bSJohannes Berg if (!WARN_ON(__sta_info_destroy_part1(sta))) 1010d778207bSJohannes Berg list_add(&sta->free_list, &free_list); 101134316837SJohannes Berg ret++; 101234316837SJohannes Berg } 101334e89507SJohannes Berg } 1014d778207bSJohannes Berg 1015d778207bSJohannes Berg if (!list_empty(&free_list)) { 1016d778207bSJohannes Berg synchronize_net(); 1017d778207bSJohannes Berg list_for_each_entry_safe(sta, tmp, &free_list, free_list) 1018d778207bSJohannes Berg __sta_info_destroy_part2(sta); 1019d778207bSJohannes Berg } 102034e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 102144213b5eSJohannes Berg 1022051007d9SJohannes Berg return ret; 1023051007d9SJohannes Berg } 1024051007d9SJohannes Berg 102524723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, 102624723d1bSJohannes Berg unsigned long exp_time) 102724723d1bSJohannes Berg { 102824723d1bSJohannes Berg struct ieee80211_local *local = sdata->local; 102924723d1bSJohannes Berg struct sta_info *sta, *tmp; 103024723d1bSJohannes Berg 103134e89507SJohannes Berg mutex_lock(&local->sta_mtx); 1032e46a2cf9SMohammed Shafi Shajakhan 1033e46a2cf9SMohammed Shafi Shajakhan list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 1034ec2b774eSMarek Lindner if (sdata != sta->sdata) 1035ec2b774eSMarek Lindner continue; 1036ec2b774eSMarek Lindner 103724723d1bSJohannes Berg if (time_after(jiffies, sta->last_rx + exp_time)) { 1038eea57d42SMohammed Shafi Shajakhan sta_dbg(sta->sdata, "expiring inactive STA %pM\n", 1039bdcbd8e0SJohannes Berg sta->sta.addr); 10403f52b7e3SMarco Porsch 10413f52b7e3SMarco Porsch if (ieee80211_vif_is_mesh(&sdata->vif) && 10423f52b7e3SMarco Porsch test_sta_flag(sta, WLAN_STA_PS_STA)) 10433f52b7e3SMarco Porsch atomic_dec(&sdata->u.mesh.ps.num_sta_ps); 10443f52b7e3SMarco Porsch 104534e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 104624723d1bSJohannes Berg } 1047e46a2cf9SMohammed Shafi Shajakhan } 1048e46a2cf9SMohammed Shafi Shajakhan 104934e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 105024723d1bSJohannes Berg } 105117741cdcSJohannes Berg 1052686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, 1053686b9cb9SBen Greear const u8 *addr, 1054686b9cb9SBen Greear const u8 *localaddr) 105517741cdcSJohannes Berg { 1056abe60632SJohannes Berg struct sta_info *sta, *nxt; 105717741cdcSJohannes Berg 1058686b9cb9SBen Greear /* 1059686b9cb9SBen Greear * Just return a random station if localaddr is NULL 1060686b9cb9SBen Greear * ... first in list. 1061686b9cb9SBen Greear */ 1062f7c65594SJohannes Berg for_each_sta_info(hw_to_local(hw), addr, sta, nxt) { 1063686b9cb9SBen Greear if (localaddr && 1064b203ca39SJoe Perches !ether_addr_equal(sta->sdata->vif.addr, localaddr)) 1065686b9cb9SBen Greear continue; 1066f7c65594SJohannes Berg if (!sta->uploaded) 1067f7c65594SJohannes Berg return NULL; 106817741cdcSJohannes Berg return &sta->sta; 1069f7c65594SJohannes Berg } 1070f7c65594SJohannes Berg 1071abe60632SJohannes Berg return NULL; 107217741cdcSJohannes Berg } 1073686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr); 10745ed176e1SJohannes Berg 10755ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif, 10765ed176e1SJohannes Berg const u8 *addr) 10775ed176e1SJohannes Berg { 1078f7c65594SJohannes Berg struct sta_info *sta; 10795ed176e1SJohannes Berg 10805ed176e1SJohannes Berg if (!vif) 10815ed176e1SJohannes Berg return NULL; 10825ed176e1SJohannes Berg 1083f7c65594SJohannes Berg sta = sta_info_get_bss(vif_to_sdata(vif), addr); 1084f7c65594SJohannes Berg if (!sta) 1085f7c65594SJohannes Berg return NULL; 10865ed176e1SJohannes Berg 1087f7c65594SJohannes Berg if (!sta->uploaded) 1088f7c65594SJohannes Berg return NULL; 1089f7c65594SJohannes Berg 1090f7c65594SJohannes Berg return &sta->sta; 10915ed176e1SJohannes Berg } 109217741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta); 1093af818581SJohannes Berg 1094e3685e03SJohannes Berg /* powersave support code */ 1095e3685e03SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) 109650a9432dSJohannes Berg { 1097608383bfSHelmut Schaa struct ieee80211_sub_if_data *sdata = sta->sdata; 1098e3685e03SJohannes Berg struct ieee80211_local *local = sdata->local; 1099e3685e03SJohannes Berg struct sk_buff_head pending; 1100e3685e03SJohannes Berg int filtered = 0, buffered = 0, ac; 1101e3685e03SJohannes Berg unsigned long flags; 1102d012a605SMarco Porsch struct ps_data *ps; 1103d012a605SMarco Porsch 1104d012a605SMarco Porsch if (sdata->vif.type == NL80211_IFTYPE_AP || 1105d012a605SMarco Porsch sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 1106d012a605SMarco Porsch ps = &sdata->bss->ps; 11073f52b7e3SMarco Porsch else if (ieee80211_vif_is_mesh(&sdata->vif)) 11083f52b7e3SMarco Porsch ps = &sdata->u.mesh.ps; 1109d012a605SMarco Porsch else 1110d012a605SMarco Porsch return; 111150a9432dSJohannes Berg 1112c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_SP); 111347086fc5SJohannes Berg 11145a306f58SJohannes Berg BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1); 1115948d887dSJohannes Berg sta->driver_buffered_tids = 0; 1116948d887dSJohannes Berg 1117d057e5a3SArik Nemtsov if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS)) 111812375ef9SJohannes Berg drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); 1119af818581SJohannes Berg 1120948d887dSJohannes Berg skb_queue_head_init(&pending); 1121af818581SJohannes Berg 11221d147bfaSEmmanuel Grumbach /* sync with ieee80211_tx_h_unicast_ps_buf */ 11231d147bfaSEmmanuel Grumbach spin_lock(&sta->ps_lock); 1124af818581SJohannes Berg /* Send all buffered frames to the station */ 1125948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1126948d887dSJohannes Berg int count = skb_queue_len(&pending), tmp; 1127948d887dSJohannes Berg 1128987c285cSArik Nemtsov spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 1129948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending); 1130987c285cSArik Nemtsov spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 1131948d887dSJohannes Berg tmp = skb_queue_len(&pending); 1132948d887dSJohannes Berg filtered += tmp - count; 1133948d887dSJohannes Berg count = tmp; 1134948d887dSJohannes Berg 1135987c285cSArik Nemtsov spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 1136948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending); 1137987c285cSArik Nemtsov spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 1138948d887dSJohannes Berg tmp = skb_queue_len(&pending); 1139948d887dSJohannes Berg buffered += tmp - count; 1140948d887dSJohannes Berg } 1141948d887dSJohannes Berg 1142e3685e03SJohannes Berg ieee80211_add_pending_skbs(local, &pending); 1143e3685e03SJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 1144e3685e03SJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_STA); 11451d147bfaSEmmanuel Grumbach spin_unlock(&sta->ps_lock); 1146948d887dSJohannes Berg 1147e3685e03SJohannes Berg atomic_dec(&ps->num_sta_ps); 1148e3685e03SJohannes Berg 1149687da132SEmmanuel Grumbach /* This station just woke up and isn't aware of our SMPS state */ 1150687da132SEmmanuel Grumbach if (!ieee80211_smps_is_restrictive(sta->known_smps_mode, 1151687da132SEmmanuel Grumbach sdata->smps_mode) && 1152687da132SEmmanuel Grumbach sta->known_smps_mode != sdata->bss->req_smps && 1153687da132SEmmanuel Grumbach sta_info_tx_streams(sta) != 1) { 1154687da132SEmmanuel Grumbach ht_dbg(sdata, 1155687da132SEmmanuel Grumbach "%pM just woke up and MIMO capable - update SMPS\n", 1156687da132SEmmanuel Grumbach sta->sta.addr); 1157687da132SEmmanuel Grumbach ieee80211_send_smps_action(sdata, sdata->bss->req_smps, 1158687da132SEmmanuel Grumbach sta->sta.addr, 1159687da132SEmmanuel Grumbach sdata->vif.bss_conf.bssid); 1160687da132SEmmanuel Grumbach } 1161687da132SEmmanuel Grumbach 1162af818581SJohannes Berg local->total_ps_buffered -= buffered; 1163af818581SJohannes Berg 1164c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1165c868cb35SJohannes Berg 1166bdcbd8e0SJohannes Berg ps_dbg(sdata, 1167bdcbd8e0SJohannes Berg "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n", 1168bdcbd8e0SJohannes Berg sta->sta.addr, sta->sta.aid, filtered, buffered); 1169af818581SJohannes Berg } 1170af818581SJohannes Berg 1171ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata, 1172ce662b44SJohannes Berg struct sta_info *sta, int tid, 1173b77cf4f8SJohannes Berg enum ieee80211_frame_release_type reason, 1174b77cf4f8SJohannes Berg bool call_driver) 1175ce662b44SJohannes Berg { 1176ce662b44SJohannes Berg struct ieee80211_local *local = sdata->local; 1177ce662b44SJohannes Berg struct ieee80211_qos_hdr *nullfunc; 1178ce662b44SJohannes Berg struct sk_buff *skb; 1179ce662b44SJohannes Berg int size = sizeof(*nullfunc); 1180ce662b44SJohannes Berg __le16 fc; 1181c2c98fdeSJohannes Berg bool qos = test_sta_flag(sta, WLAN_STA_WME); 1182ce662b44SJohannes Berg struct ieee80211_tx_info *info; 118355de908aSJohannes Berg struct ieee80211_chanctx_conf *chanctx_conf; 1184ce662b44SJohannes Berg 1185ce662b44SJohannes Berg if (qos) { 1186ce662b44SJohannes Berg fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1187ce662b44SJohannes Berg IEEE80211_STYPE_QOS_NULLFUNC | 1188ce662b44SJohannes Berg IEEE80211_FCTL_FROMDS); 1189ce662b44SJohannes Berg } else { 1190ce662b44SJohannes Berg size -= 2; 1191ce662b44SJohannes Berg fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1192ce662b44SJohannes Berg IEEE80211_STYPE_NULLFUNC | 1193ce662b44SJohannes Berg IEEE80211_FCTL_FROMDS); 1194ce662b44SJohannes Berg } 1195ce662b44SJohannes Berg 1196ce662b44SJohannes Berg skb = dev_alloc_skb(local->hw.extra_tx_headroom + size); 1197ce662b44SJohannes Berg if (!skb) 1198ce662b44SJohannes Berg return; 1199ce662b44SJohannes Berg 1200ce662b44SJohannes Berg skb_reserve(skb, local->hw.extra_tx_headroom); 1201ce662b44SJohannes Berg 1202ce662b44SJohannes Berg nullfunc = (void *) skb_put(skb, size); 1203ce662b44SJohannes Berg nullfunc->frame_control = fc; 1204ce662b44SJohannes Berg nullfunc->duration_id = 0; 1205ce662b44SJohannes Berg memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN); 1206ce662b44SJohannes Berg memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN); 1207ce662b44SJohannes Berg memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN); 1208864a6040SJohannes Berg nullfunc->seq_ctrl = 0; 1209ce662b44SJohannes Berg 1210ce662b44SJohannes Berg skb->priority = tid; 1211ce662b44SJohannes Berg skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]); 121259b66255SJohannes Berg if (qos) { 1213ce662b44SJohannes Berg nullfunc->qos_ctrl = cpu_to_le16(tid); 1214ce662b44SJohannes Berg 121540b96408SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_UAPSD) 1216ce662b44SJohannes Berg nullfunc->qos_ctrl |= 1217ce662b44SJohannes Berg cpu_to_le16(IEEE80211_QOS_CTL_EOSP); 1218ce662b44SJohannes Berg } 1219ce662b44SJohannes Berg 1220ce662b44SJohannes Berg info = IEEE80211_SKB_CB(skb); 1221ce662b44SJohannes Berg 1222ce662b44SJohannes Berg /* 1223ce662b44SJohannes Berg * Tell TX path to send this frame even though the 1224ce662b44SJohannes Berg * STA may still remain is PS mode after this frame 1225deeaee19SJohannes Berg * exchange. Also set EOSP to indicate this packet 1226deeaee19SJohannes Berg * ends the poll/service period. 1227ce662b44SJohannes Berg */ 122802f2f1a9SJohannes Berg info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER | 1229d6d23de2SFelix Fietkau IEEE80211_TX_CTL_PS_RESPONSE | 1230deeaee19SJohannes Berg IEEE80211_TX_STATUS_EOSP | 1231ce662b44SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 1232ce662b44SJohannes Berg 1233b77cf4f8SJohannes Berg if (call_driver) 1234b77cf4f8SJohannes Berg drv_allow_buffered_frames(local, sta, BIT(tid), 1, 1235b77cf4f8SJohannes Berg reason, false); 123640b96408SJohannes Berg 123789afe614SJohannes Berg skb->dev = sdata->dev; 123889afe614SJohannes Berg 123955de908aSJohannes Berg rcu_read_lock(); 124055de908aSJohannes Berg chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf); 124155de908aSJohannes Berg if (WARN_ON(!chanctx_conf)) { 124255de908aSJohannes Berg rcu_read_unlock(); 124355de908aSJohannes Berg kfree_skb(skb); 124455de908aSJohannes Berg return; 124555de908aSJohannes Berg } 124655de908aSJohannes Berg 12474bf88530SJohannes Berg ieee80211_xmit(sdata, skb, chanctx_conf->def.chan->band); 124855de908aSJohannes Berg rcu_read_unlock(); 1249ce662b44SJohannes Berg } 1250ce662b44SJohannes Berg 12510a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids) 12520a1cb809SJohannes Berg { 12530a1cb809SJohannes Berg /* lower 3 TIDs aren't ordered perfectly */ 12540a1cb809SJohannes Berg if (tids & 0xF8) 12550a1cb809SJohannes Berg return fls(tids) - 1; 12560a1cb809SJohannes Berg /* TID 0 is BE just like TID 3 */ 12570a1cb809SJohannes Berg if (tids & BIT(0)) 12580a1cb809SJohannes Berg return 0; 12590a1cb809SJohannes Berg return fls(tids) - 1; 12600a1cb809SJohannes Berg } 12610a1cb809SJohannes Berg 126247086fc5SJohannes Berg static void 126347086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta, 126447086fc5SJohannes Berg int n_frames, u8 ignored_acs, 126547086fc5SJohannes Berg enum ieee80211_frame_release_type reason) 1266af818581SJohannes Berg { 1267af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1268af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 1269948d887dSJohannes Berg bool more_data = false; 1270948d887dSJohannes Berg int ac; 12714049e09aSJohannes Berg unsigned long driver_release_tids = 0; 127247086fc5SJohannes Berg struct sk_buff_head frames; 127347086fc5SJohannes Berg 1274deeaee19SJohannes Berg /* Service or PS-Poll period starts */ 1275c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_SP); 1276deeaee19SJohannes Berg 127747086fc5SJohannes Berg __skb_queue_head_init(&frames); 1278af818581SJohannes Berg 1279f9f760b4SJohannes Berg /* Get response frame(s) and more data bit for the last one. */ 1280948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 12814049e09aSJohannes Berg unsigned long tids; 12824049e09aSJohannes Berg 128347086fc5SJohannes Berg if (ignored_acs & BIT(ac)) 1284948d887dSJohannes Berg continue; 1285948d887dSJohannes Berg 12864049e09aSJohannes Berg tids = ieee80211_tids_for_ac(ac); 12874049e09aSJohannes Berg 1288f9f760b4SJohannes Berg /* if we already have frames from software, then we can't also 1289f9f760b4SJohannes Berg * release from hardware queues 1290f9f760b4SJohannes Berg */ 1291f9f760b4SJohannes Berg if (skb_queue_empty(&frames)) 1292f9f760b4SJohannes Berg driver_release_tids |= sta->driver_buffered_tids & tids; 1293f9f760b4SJohannes Berg 12944049e09aSJohannes Berg if (driver_release_tids) { 1295f9f760b4SJohannes Berg /* If the driver has data on more than one TID then 1296f9f760b4SJohannes Berg * certainly there's more data if we release just a 1297f9f760b4SJohannes Berg * single frame now (from a single TID). This will 1298f9f760b4SJohannes Berg * only happen for PS-Poll. 1299f9f760b4SJohannes Berg */ 1300f9f760b4SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_PSPOLL && 1301f9f760b4SJohannes Berg hweight16(driver_release_tids) > 1) { 1302f9f760b4SJohannes Berg more_data = true; 1303f9f760b4SJohannes Berg driver_release_tids = 1304f9f760b4SJohannes Berg BIT(find_highest_prio_tid( 1305f9f760b4SJohannes Berg driver_release_tids)); 1306f9f760b4SJohannes Berg break; 1307f9f760b4SJohannes Berg } 13084049e09aSJohannes Berg } else { 130947086fc5SJohannes Berg struct sk_buff *skb; 131047086fc5SJohannes Berg 131147086fc5SJohannes Berg while (n_frames > 0) { 1312948d887dSJohannes Berg skb = skb_dequeue(&sta->tx_filtered[ac]); 1313948d887dSJohannes Berg if (!skb) { 131447086fc5SJohannes Berg skb = skb_dequeue( 131547086fc5SJohannes Berg &sta->ps_tx_buf[ac]); 1316af818581SJohannes Berg if (skb) 1317af818581SJohannes Berg local->total_ps_buffered--; 1318af818581SJohannes Berg } 131947086fc5SJohannes Berg if (!skb) 132047086fc5SJohannes Berg break; 132147086fc5SJohannes Berg n_frames--; 132247086fc5SJohannes Berg __skb_queue_tail(&frames, skb); 132347086fc5SJohannes Berg } 1324948d887dSJohannes Berg } 1325948d887dSJohannes Berg 1326f9f760b4SJohannes Berg /* If we have more frames buffered on this AC, then set the 1327f9f760b4SJohannes Berg * more-data bit and abort the loop since we can't send more 1328f9f760b4SJohannes Berg * data from other ACs before the buffered frames from this. 13294049e09aSJohannes Berg */ 1330948d887dSJohannes Berg if (!skb_queue_empty(&sta->tx_filtered[ac]) || 1331948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac])) { 1332948d887dSJohannes Berg more_data = true; 1333948d887dSJohannes Berg break; 1334948d887dSJohannes Berg } 1335948d887dSJohannes Berg } 1336af818581SJohannes Berg 1337f9f760b4SJohannes Berg if (skb_queue_empty(&frames) && !driver_release_tids) { 1338ce662b44SJohannes Berg int tid; 13394049e09aSJohannes Berg 1340ce662b44SJohannes Berg /* 1341ce662b44SJohannes Berg * For PS-Poll, this can only happen due to a race condition 1342ce662b44SJohannes Berg * when we set the TIM bit and the station notices it, but 1343ce662b44SJohannes Berg * before it can poll for the frame we expire it. 1344ce662b44SJohannes Berg * 1345ce662b44SJohannes Berg * For uAPSD, this is said in the standard (11.2.1.5 h): 1346ce662b44SJohannes Berg * At each unscheduled SP for a non-AP STA, the AP shall 1347ce662b44SJohannes Berg * attempt to transmit at least one MSDU or MMPDU, but no 1348ce662b44SJohannes Berg * more than the value specified in the Max SP Length field 1349ce662b44SJohannes Berg * in the QoS Capability element from delivery-enabled ACs, 1350ce662b44SJohannes Berg * that are destined for the non-AP STA. 1351ce662b44SJohannes Berg * 1352ce662b44SJohannes Berg * Since we have no other MSDU/MMPDU, transmit a QoS null frame. 1353ce662b44SJohannes Berg */ 1354ce662b44SJohannes Berg 1355ce662b44SJohannes Berg /* This will evaluate to 1, 3, 5 or 7. */ 1356ce662b44SJohannes Berg tid = 7 - ((ffs(~ignored_acs) - 1) << 1); 1357ce662b44SJohannes Berg 1358b77cf4f8SJohannes Berg ieee80211_send_null_response(sdata, sta, tid, reason, true); 1359f9f760b4SJohannes Berg } else if (!driver_release_tids) { 136047086fc5SJohannes Berg struct sk_buff_head pending; 136147086fc5SJohannes Berg struct sk_buff *skb; 136240b96408SJohannes Berg int num = 0; 136340b96408SJohannes Berg u16 tids = 0; 1364b77cf4f8SJohannes Berg bool need_null = false; 136547086fc5SJohannes Berg 136647086fc5SJohannes Berg skb_queue_head_init(&pending); 136747086fc5SJohannes Berg 136847086fc5SJohannes Berg while ((skb = __skb_dequeue(&frames))) { 1369af818581SJohannes Berg struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 137047086fc5SJohannes Berg struct ieee80211_hdr *hdr = (void *) skb->data; 137140b96408SJohannes Berg u8 *qoshdr = NULL; 137240b96408SJohannes Berg 137340b96408SJohannes Berg num++; 1374af818581SJohannes Berg 1375af818581SJohannes Berg /* 137647086fc5SJohannes Berg * Tell TX path to send this frame even though the 137747086fc5SJohannes Berg * STA may still remain is PS mode after this frame 137847086fc5SJohannes Berg * exchange. 1379af818581SJohannes Berg */ 1380d6d23de2SFelix Fietkau info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER | 1381d6d23de2SFelix Fietkau IEEE80211_TX_CTL_PS_RESPONSE; 1382af818581SJohannes Berg 138347086fc5SJohannes Berg /* 138447086fc5SJohannes Berg * Use MoreData flag to indicate whether there are 138547086fc5SJohannes Berg * more buffered frames for this STA 138647086fc5SJohannes Berg */ 138724b9c373SJanusz.Dziedzic@tieto.com if (more_data || !skb_queue_empty(&frames)) 138847086fc5SJohannes Berg hdr->frame_control |= 138947086fc5SJohannes Berg cpu_to_le16(IEEE80211_FCTL_MOREDATA); 139024b9c373SJanusz.Dziedzic@tieto.com else 139124b9c373SJanusz.Dziedzic@tieto.com hdr->frame_control &= 139224b9c373SJanusz.Dziedzic@tieto.com cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 1393af818581SJohannes Berg 139440b96408SJohannes Berg if (ieee80211_is_data_qos(hdr->frame_control) || 139540b96408SJohannes Berg ieee80211_is_qos_nullfunc(hdr->frame_control)) 139640b96408SJohannes Berg qoshdr = ieee80211_get_qos_ctl(hdr); 139740b96408SJohannes Berg 1398b77cf4f8SJohannes Berg tids |= BIT(skb->priority); 1399b77cf4f8SJohannes Berg 1400b77cf4f8SJohannes Berg __skb_queue_tail(&pending, skb); 1401b77cf4f8SJohannes Berg 1402b77cf4f8SJohannes Berg /* end service period after last frame or add one */ 1403b77cf4f8SJohannes Berg if (!skb_queue_empty(&frames)) 1404b77cf4f8SJohannes Berg continue; 1405b77cf4f8SJohannes Berg 1406b77cf4f8SJohannes Berg if (reason != IEEE80211_FRAME_RELEASE_UAPSD) { 1407b77cf4f8SJohannes Berg /* for PS-Poll, there's only one frame */ 1408b77cf4f8SJohannes Berg info->flags |= IEEE80211_TX_STATUS_EOSP | 1409b77cf4f8SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 1410b77cf4f8SJohannes Berg break; 1411b77cf4f8SJohannes Berg } 1412b77cf4f8SJohannes Berg 1413b77cf4f8SJohannes Berg /* For uAPSD, things are a bit more complicated. If the 1414b77cf4f8SJohannes Berg * last frame has a QoS header (i.e. is a QoS-data or 1415b77cf4f8SJohannes Berg * QoS-nulldata frame) then just set the EOSP bit there 1416b77cf4f8SJohannes Berg * and be done. 1417b77cf4f8SJohannes Berg * If the frame doesn't have a QoS header (which means 1418b77cf4f8SJohannes Berg * it should be a bufferable MMPDU) then we can't set 1419b77cf4f8SJohannes Berg * the EOSP bit in the QoS header; add a QoS-nulldata 1420b77cf4f8SJohannes Berg * frame to the list to send it after the MMPDU. 1421b77cf4f8SJohannes Berg * 1422b77cf4f8SJohannes Berg * Note that this code is only in the mac80211-release 1423b77cf4f8SJohannes Berg * code path, we assume that the driver will not buffer 1424b77cf4f8SJohannes Berg * anything but QoS-data frames, or if it does, will 1425b77cf4f8SJohannes Berg * create the QoS-nulldata frame by itself if needed. 1426b77cf4f8SJohannes Berg * 1427b77cf4f8SJohannes Berg * Cf. 802.11-2012 10.2.1.10 (c). 1428b77cf4f8SJohannes Berg */ 1429b77cf4f8SJohannes Berg if (qoshdr) { 143040b96408SJohannes Berg *qoshdr |= IEEE80211_QOS_CTL_EOSP; 1431deeaee19SJohannes Berg 143247086fc5SJohannes Berg info->flags |= IEEE80211_TX_STATUS_EOSP | 143347086fc5SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 1434b77cf4f8SJohannes Berg } else { 1435b77cf4f8SJohannes Berg /* The standard isn't completely clear on this 1436b77cf4f8SJohannes Berg * as it says the more-data bit should be set 1437b77cf4f8SJohannes Berg * if there are more BUs. The QoS-Null frame 1438b77cf4f8SJohannes Berg * we're about to send isn't buffered yet, we 1439b77cf4f8SJohannes Berg * only create it below, but let's pretend it 1440b77cf4f8SJohannes Berg * was buffered just in case some clients only 1441b77cf4f8SJohannes Berg * expect more-data=0 when eosp=1. 1442b77cf4f8SJohannes Berg */ 1443b77cf4f8SJohannes Berg hdr->frame_control |= 1444b77cf4f8SJohannes Berg cpu_to_le16(IEEE80211_FCTL_MOREDATA); 1445b77cf4f8SJohannes Berg need_null = true; 1446b77cf4f8SJohannes Berg num++; 144752a3f20cSMarco Porsch } 1448b77cf4f8SJohannes Berg break; 144947086fc5SJohannes Berg } 145047086fc5SJohannes Berg 145140b96408SJohannes Berg drv_allow_buffered_frames(local, sta, tids, num, 145240b96408SJohannes Berg reason, more_data); 145340b96408SJohannes Berg 145447086fc5SJohannes Berg ieee80211_add_pending_skbs(local, &pending); 1455af818581SJohannes Berg 1456b77cf4f8SJohannes Berg if (need_null) 1457b77cf4f8SJohannes Berg ieee80211_send_null_response( 1458b77cf4f8SJohannes Berg sdata, sta, find_highest_prio_tid(tids), 1459b77cf4f8SJohannes Berg reason, false); 1460b77cf4f8SJohannes Berg 1461c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1462af818581SJohannes Berg } else { 1463af818581SJohannes Berg /* 14644049e09aSJohannes Berg * We need to release a frame that is buffered somewhere in the 14654049e09aSJohannes Berg * driver ... it'll have to handle that. 1466f9f760b4SJohannes Berg * Note that the driver also has to check the number of frames 1467f9f760b4SJohannes Berg * on the TIDs we're releasing from - if there are more than 1468f9f760b4SJohannes Berg * n_frames it has to set the more-data bit (if we didn't ask 1469f9f760b4SJohannes Berg * it to set it anyway due to other buffered frames); if there 1470f9f760b4SJohannes Berg * are fewer than n_frames it has to make sure to adjust that 1471f9f760b4SJohannes Berg * to allow the service period to end properly. 1472af818581SJohannes Berg */ 14734049e09aSJohannes Berg drv_release_buffered_frames(local, sta, driver_release_tids, 147447086fc5SJohannes Berg n_frames, reason, more_data); 14754049e09aSJohannes Berg 14764049e09aSJohannes Berg /* 14774049e09aSJohannes Berg * Note that we don't recalculate the TIM bit here as it would 14784049e09aSJohannes Berg * most likely have no effect at all unless the driver told us 1479f9f760b4SJohannes Berg * that the TID(s) became empty before returning here from the 14804049e09aSJohannes Berg * release function. 1481f9f760b4SJohannes Berg * Either way, however, when the driver tells us that the TID(s) 14824049e09aSJohannes Berg * became empty we'll do the TIM recalculation. 14834049e09aSJohannes Berg */ 1484af818581SJohannes Berg } 1485af818581SJohannes Berg } 1486af818581SJohannes Berg 1487af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) 1488af818581SJohannes Berg { 148947086fc5SJohannes Berg u8 ignore_for_response = sta->sta.uapsd_queues; 1490af818581SJohannes Berg 1491af818581SJohannes Berg /* 149247086fc5SJohannes Berg * If all ACs are delivery-enabled then we should reply 149347086fc5SJohannes Berg * from any of them, if only some are enabled we reply 149447086fc5SJohannes Berg * only from the non-enabled ones. 1495af818581SJohannes Berg */ 149647086fc5SJohannes Berg if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1) 149747086fc5SJohannes Berg ignore_for_response = 0; 1498af818581SJohannes Berg 149947086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response, 150047086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_PSPOLL); 1501af818581SJohannes Berg } 150247086fc5SJohannes Berg 150347086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta) 150447086fc5SJohannes Berg { 150547086fc5SJohannes Berg int n_frames = sta->sta.max_sp; 150647086fc5SJohannes Berg u8 delivery_enabled = sta->sta.uapsd_queues; 150747086fc5SJohannes Berg 150847086fc5SJohannes Berg /* 150947086fc5SJohannes Berg * If we ever grow support for TSPEC this might happen if 151047086fc5SJohannes Berg * the TSPEC update from hostapd comes in between a trigger 151147086fc5SJohannes Berg * frame setting WLAN_STA_UAPSD in the RX path and this 151247086fc5SJohannes Berg * actually getting called. 151347086fc5SJohannes Berg */ 151447086fc5SJohannes Berg if (!delivery_enabled) 151547086fc5SJohannes Berg return; 151647086fc5SJohannes Berg 151747086fc5SJohannes Berg switch (sta->sta.max_sp) { 151847086fc5SJohannes Berg case 1: 151947086fc5SJohannes Berg n_frames = 2; 152047086fc5SJohannes Berg break; 152147086fc5SJohannes Berg case 2: 152247086fc5SJohannes Berg n_frames = 4; 152347086fc5SJohannes Berg break; 152447086fc5SJohannes Berg case 3: 152547086fc5SJohannes Berg n_frames = 6; 152647086fc5SJohannes Berg break; 152747086fc5SJohannes Berg case 0: 152847086fc5SJohannes Berg /* XXX: what is a good value? */ 152947086fc5SJohannes Berg n_frames = 8; 153047086fc5SJohannes Berg break; 153147086fc5SJohannes Berg } 153247086fc5SJohannes Berg 153347086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled, 153447086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_UAPSD); 1535af818581SJohannes Berg } 1536af818581SJohannes Berg 1537af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw, 1538af818581SJohannes Berg struct ieee80211_sta *pubsta, bool block) 1539af818581SJohannes Berg { 1540af818581SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1541af818581SJohannes Berg 1542b5878a2dSJohannes Berg trace_api_sta_block_awake(sta->local, pubsta, block); 1543b5878a2dSJohannes Berg 1544af818581SJohannes Berg if (block) 1545c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_PS_DRIVER); 1546c2c98fdeSJohannes Berg else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) 1547af818581SJohannes Berg ieee80211_queue_work(hw, &sta->drv_unblock_wk); 1548af818581SJohannes Berg } 1549af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake); 1550dcf55fb5SFelix Fietkau 1551e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta) 155237fbd908SJohannes Berg { 155337fbd908SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 155437fbd908SJohannes Berg struct ieee80211_local *local = sta->local; 155537fbd908SJohannes Berg 155637fbd908SJohannes Berg trace_api_eosp(local, pubsta); 155737fbd908SJohannes Berg 155837fbd908SJohannes Berg clear_sta_flag(sta, WLAN_STA_SP); 155937fbd908SJohannes Berg } 1560e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp); 156137fbd908SJohannes Berg 1562042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta, 1563042ec453SJohannes Berg u8 tid, bool buffered) 1564dcf55fb5SFelix Fietkau { 1565dcf55fb5SFelix Fietkau struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1566dcf55fb5SFelix Fietkau 15675a306f58SJohannes Berg if (WARN_ON(tid >= IEEE80211_NUM_TIDS)) 1568042ec453SJohannes Berg return; 1569042ec453SJohannes Berg 15701b000789SJohannes Berg trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered); 15711b000789SJohannes Berg 1572948d887dSJohannes Berg if (buffered) 1573948d887dSJohannes Berg set_bit(tid, &sta->driver_buffered_tids); 1574948d887dSJohannes Berg else 1575948d887dSJohannes Berg clear_bit(tid, &sta->driver_buffered_tids); 1576948d887dSJohannes Berg 1577c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1578dcf55fb5SFelix Fietkau } 1579042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered); 1580d9a7ddb0SJohannes Berg 158183d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta, 1582d9a7ddb0SJohannes Berg enum ieee80211_sta_state new_state) 1583d9a7ddb0SJohannes Berg { 15848bf11d8dSJohannes Berg might_sleep(); 1585d9a7ddb0SJohannes Berg 1586d9a7ddb0SJohannes Berg if (sta->sta_state == new_state) 1587d9a7ddb0SJohannes Berg return 0; 1588d9a7ddb0SJohannes Berg 1589f09603a2SJohannes Berg /* check allowed transitions first */ 1590f09603a2SJohannes Berg 1591d9a7ddb0SJohannes Berg switch (new_state) { 1592d9a7ddb0SJohannes Berg case IEEE80211_STA_NONE: 1593f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_AUTH) 1594d9a7ddb0SJohannes Berg return -EINVAL; 1595d9a7ddb0SJohannes Berg break; 1596d9a7ddb0SJohannes Berg case IEEE80211_STA_AUTH: 1597f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_NONE && 1598f09603a2SJohannes Berg sta->sta_state != IEEE80211_STA_ASSOC) 1599d9a7ddb0SJohannes Berg return -EINVAL; 1600d9a7ddb0SJohannes Berg break; 1601d9a7ddb0SJohannes Berg case IEEE80211_STA_ASSOC: 1602f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_AUTH && 1603f09603a2SJohannes Berg sta->sta_state != IEEE80211_STA_AUTHORIZED) 1604d9a7ddb0SJohannes Berg return -EINVAL; 1605d9a7ddb0SJohannes Berg break; 1606d9a7ddb0SJohannes Berg case IEEE80211_STA_AUTHORIZED: 1607f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_ASSOC) 1608d9a7ddb0SJohannes Berg return -EINVAL; 1609d9a7ddb0SJohannes Berg break; 1610d9a7ddb0SJohannes Berg default: 1611d9a7ddb0SJohannes Berg WARN(1, "invalid state %d", new_state); 1612d9a7ddb0SJohannes Berg return -EINVAL; 1613d9a7ddb0SJohannes Berg } 1614d9a7ddb0SJohannes Berg 1615bdcbd8e0SJohannes Berg sta_dbg(sta->sdata, "moving STA %pM to state %d\n", 1616bdcbd8e0SJohannes Berg sta->sta.addr, new_state); 1617f09603a2SJohannes Berg 1618f09603a2SJohannes Berg /* 1619f09603a2SJohannes Berg * notify the driver before the actual changes so it can 1620f09603a2SJohannes Berg * fail the transition 1621f09603a2SJohannes Berg */ 1622f09603a2SJohannes Berg if (test_sta_flag(sta, WLAN_STA_INSERTED)) { 1623f09603a2SJohannes Berg int err = drv_sta_state(sta->local, sta->sdata, sta, 1624f09603a2SJohannes Berg sta->sta_state, new_state); 1625f09603a2SJohannes Berg if (err) 1626f09603a2SJohannes Berg return err; 1627f09603a2SJohannes Berg } 1628f09603a2SJohannes Berg 1629f09603a2SJohannes Berg /* reflect the change in all state variables */ 1630f09603a2SJohannes Berg 1631f09603a2SJohannes Berg switch (new_state) { 1632f09603a2SJohannes Berg case IEEE80211_STA_NONE: 1633f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_AUTH) 1634f09603a2SJohannes Berg clear_bit(WLAN_STA_AUTH, &sta->_flags); 1635f09603a2SJohannes Berg break; 1636f09603a2SJohannes Berg case IEEE80211_STA_AUTH: 1637f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_NONE) 1638f09603a2SJohannes Berg set_bit(WLAN_STA_AUTH, &sta->_flags); 1639f09603a2SJohannes Berg else if (sta->sta_state == IEEE80211_STA_ASSOC) 1640f09603a2SJohannes Berg clear_bit(WLAN_STA_ASSOC, &sta->_flags); 1641f09603a2SJohannes Berg break; 1642f09603a2SJohannes Berg case IEEE80211_STA_ASSOC: 1643f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_AUTH) { 1644f09603a2SJohannes Berg set_bit(WLAN_STA_ASSOC, &sta->_flags); 1645f09603a2SJohannes Berg } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) { 16467e3ed02cSFelix Fietkau if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 16477e3ed02cSFelix Fietkau (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 16487e3ed02cSFelix Fietkau !sta->sdata->u.vlan.sta)) 16497e3ed02cSFelix Fietkau atomic_dec(&sta->sdata->bss->num_mcast_sta); 1650f09603a2SJohannes Berg clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 1651f09603a2SJohannes Berg } 1652f09603a2SJohannes Berg break; 1653f09603a2SJohannes Berg case IEEE80211_STA_AUTHORIZED: 1654f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_ASSOC) { 16557e3ed02cSFelix Fietkau if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 16567e3ed02cSFelix Fietkau (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 16577e3ed02cSFelix Fietkau !sta->sdata->u.vlan.sta)) 16587e3ed02cSFelix Fietkau atomic_inc(&sta->sdata->bss->num_mcast_sta); 1659f09603a2SJohannes Berg set_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 1660f09603a2SJohannes Berg } 1661f09603a2SJohannes Berg break; 1662f09603a2SJohannes Berg default: 1663f09603a2SJohannes Berg break; 1664f09603a2SJohannes Berg } 1665f09603a2SJohannes Berg 1666d9a7ddb0SJohannes Berg sta->sta_state = new_state; 1667d9a7ddb0SJohannes Berg 1668d9a7ddb0SJohannes Berg return 0; 1669d9a7ddb0SJohannes Berg } 1670687da132SEmmanuel Grumbach 1671687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta) 1672687da132SEmmanuel Grumbach { 1673687da132SEmmanuel Grumbach struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap; 1674687da132SEmmanuel Grumbach u8 rx_streams; 1675687da132SEmmanuel Grumbach 1676687da132SEmmanuel Grumbach if (!sta->sta.ht_cap.ht_supported) 1677687da132SEmmanuel Grumbach return 1; 1678687da132SEmmanuel Grumbach 1679687da132SEmmanuel Grumbach if (sta->sta.vht_cap.vht_supported) { 1680687da132SEmmanuel Grumbach int i; 1681687da132SEmmanuel Grumbach u16 tx_mcs_map = 1682687da132SEmmanuel Grumbach le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map); 1683687da132SEmmanuel Grumbach 1684687da132SEmmanuel Grumbach for (i = 7; i >= 0; i--) 1685687da132SEmmanuel Grumbach if ((tx_mcs_map & (0x3 << (i * 2))) != 1686687da132SEmmanuel Grumbach IEEE80211_VHT_MCS_NOT_SUPPORTED) 1687687da132SEmmanuel Grumbach return i + 1; 1688687da132SEmmanuel Grumbach } 1689687da132SEmmanuel Grumbach 1690687da132SEmmanuel Grumbach if (ht_cap->mcs.rx_mask[3]) 1691687da132SEmmanuel Grumbach rx_streams = 4; 1692687da132SEmmanuel Grumbach else if (ht_cap->mcs.rx_mask[2]) 1693687da132SEmmanuel Grumbach rx_streams = 3; 1694687da132SEmmanuel Grumbach else if (ht_cap->mcs.rx_mask[1]) 1695687da132SEmmanuel Grumbach rx_streams = 2; 1696687da132SEmmanuel Grumbach else 1697687da132SEmmanuel Grumbach rx_streams = 1; 1698687da132SEmmanuel Grumbach 1699687da132SEmmanuel Grumbach if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF)) 1700687da132SEmmanuel Grumbach return rx_streams; 1701687da132SEmmanuel Grumbach 1702687da132SEmmanuel Grumbach return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK) 1703687da132SEmmanuel Grumbach >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1; 1704687da132SEmmanuel Grumbach } 1705