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 94*5108ca82SJohannes 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 102b22cfcfcSEliad Peller if (test_sta_flag(sta, WLAN_STA_PS_STA)) { 103d012a605SMarco Porsch if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 104d012a605SMarco Porsch sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 105d012a605SMarco Porsch ps = &sdata->bss->ps; 1063f52b7e3SMarco Porsch else if (ieee80211_vif_is_mesh(&sdata->vif)) 1073f52b7e3SMarco Porsch ps = &sdata->u.mesh.ps; 108d012a605SMarco Porsch else 109d012a605SMarco Porsch return; 110b22cfcfcSEliad Peller 111b22cfcfcSEliad Peller clear_sta_flag(sta, WLAN_STA_PS_STA); 112b22cfcfcSEliad Peller 113d012a605SMarco Porsch atomic_dec(&ps->num_sta_ps); 114b22cfcfcSEliad Peller sta_info_recalc_tim(sta); 115b22cfcfcSEliad Peller } 116b22cfcfcSEliad Peller 117b22cfcfcSEliad Peller for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 118b22cfcfcSEliad Peller local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]); 1191f98ab7fSFelix Fietkau ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]); 1201f98ab7fSFelix Fietkau ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]); 121b22cfcfcSEliad Peller } 122b22cfcfcSEliad Peller 12345b5028eSThomas Pedersen if (ieee80211_vif_is_mesh(&sdata->vif)) 12445b5028eSThomas Pedersen mesh_sta_cleanup(sta); 125b22cfcfcSEliad Peller 126b22cfcfcSEliad Peller cancel_work_sync(&sta->drv_unblock_wk); 127b22cfcfcSEliad Peller 128b22cfcfcSEliad Peller /* 129b22cfcfcSEliad Peller * Destroy aggregation state here. It would be nice to wait for the 130b22cfcfcSEliad Peller * driver to finish aggregation stop and then clean up, but for now 131b22cfcfcSEliad Peller * drivers have to handle aggregation stop being requested, followed 132b22cfcfcSEliad Peller * directly by station destruction. 133b22cfcfcSEliad Peller */ 1345a306f58SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 135661eb381SJohannes Berg kfree(sta->ampdu_mlme.tid_start_tx[i]); 136b22cfcfcSEliad Peller tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]); 137b22cfcfcSEliad Peller if (!tid_tx) 138b22cfcfcSEliad Peller continue; 1391f98ab7fSFelix Fietkau ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending); 140b22cfcfcSEliad Peller kfree(tid_tx); 141b22cfcfcSEliad Peller } 142*5108ca82SJohannes Berg } 143b22cfcfcSEliad Peller 144*5108ca82SJohannes Berg static void cleanup_single_sta(struct sta_info *sta) 145*5108ca82SJohannes Berg { 146*5108ca82SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 147*5108ca82SJohannes Berg struct ieee80211_local *local = sdata->local; 148*5108ca82SJohannes Berg 149*5108ca82SJohannes Berg __cleanup_single_sta(sta); 150b22cfcfcSEliad Peller sta_info_free(local, sta); 151b22cfcfcSEliad Peller } 152b22cfcfcSEliad Peller 153d0709a65SJohannes Berg /* protected by RCU */ 154abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata, 155abe60632SJohannes Berg const u8 *addr) 15643ba7e95SJohannes Berg { 157abe60632SJohannes Berg struct ieee80211_local *local = sdata->local; 15843ba7e95SJohannes Berg struct sta_info *sta; 15943ba7e95SJohannes Berg 1600379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1610379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 16243ba7e95SJohannes Berg while (sta) { 163abe60632SJohannes Berg if (sta->sdata == sdata && 164b203ca39SJoe Perches ether_addr_equal(sta->sta.addr, addr)) 16543ba7e95SJohannes Berg break; 1660379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 1670379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 16843ba7e95SJohannes Berg } 16943ba7e95SJohannes Berg return sta; 17043ba7e95SJohannes Berg } 17143ba7e95SJohannes Berg 1720e5ded5aSFelix Fietkau /* 1730e5ded5aSFelix Fietkau * Get sta info either from the specified interface 1740e5ded5aSFelix Fietkau * or from one of its vlans 1750e5ded5aSFelix Fietkau */ 1760e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata, 1770e5ded5aSFelix Fietkau const u8 *addr) 1780e5ded5aSFelix Fietkau { 1790e5ded5aSFelix Fietkau struct ieee80211_local *local = sdata->local; 1800e5ded5aSFelix Fietkau struct sta_info *sta; 1810e5ded5aSFelix Fietkau 1820379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1830379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 1840e5ded5aSFelix Fietkau while (sta) { 1850e5ded5aSFelix Fietkau if ((sta->sdata == sdata || 186a2c1e3daSJohannes Berg (sta->sdata->bss && sta->sdata->bss == sdata->bss)) && 187b203ca39SJoe Perches ether_addr_equal(sta->sta.addr, addr)) 1880e5ded5aSFelix Fietkau break; 1890379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 1900379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 1910e5ded5aSFelix Fietkau } 1920e5ded5aSFelix Fietkau return sta; 1930e5ded5aSFelix Fietkau } 1940e5ded5aSFelix Fietkau 1953b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata, 1963b53fde8SJohannes Berg int idx) 197ee385855SLuis Carlos Cobo { 1983b53fde8SJohannes Berg struct ieee80211_local *local = sdata->local; 199ee385855SLuis Carlos Cobo struct sta_info *sta; 200ee385855SLuis Carlos Cobo int i = 0; 201ee385855SLuis Carlos Cobo 202d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) { 2033b53fde8SJohannes Berg if (sdata != sta->sdata) 2042a8ca29aSLuis Carlos Cobo continue; 205ee385855SLuis Carlos Cobo if (i < idx) { 206ee385855SLuis Carlos Cobo ++i; 207ee385855SLuis Carlos Cobo continue; 208ee385855SLuis Carlos Cobo } 2092a8ca29aSLuis Carlos Cobo return sta; 210ee385855SLuis Carlos Cobo } 211ee385855SLuis Carlos Cobo 212ee385855SLuis Carlos Cobo return NULL; 213ee385855SLuis Carlos Cobo } 214f0706e82SJiri Benc 21593e5deb1SJohannes Berg /** 216d9a7ddb0SJohannes Berg * sta_info_free - free STA 21793e5deb1SJohannes Berg * 2186ef307bcSRandy Dunlap * @local: pointer to the global information 21993e5deb1SJohannes Berg * @sta: STA info to free 22093e5deb1SJohannes Berg * 22193e5deb1SJohannes Berg * This function must undo everything done by sta_info_alloc() 222d9a7ddb0SJohannes Berg * that may happen before sta_info_insert(). It may only be 223d9a7ddb0SJohannes Berg * called when sta_info_insert() has not been attempted (and 224d9a7ddb0SJohannes Berg * if that fails, the station is freed anyway.) 22593e5deb1SJohannes Berg */ 226d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta) 22793e5deb1SJohannes Berg { 228ad38bfc9SMatti Gottlieb int i; 229ad38bfc9SMatti Gottlieb 230889cbb91SJohannes Berg if (sta->rate_ctrl) 2314b7679a5SJohannes Berg rate_control_free_sta(sta); 23293e5deb1SJohannes Berg 233ad38bfc9SMatti Gottlieb if (sta->tx_lat) { 234ad38bfc9SMatti Gottlieb for (i = 0; i < IEEE80211_NUM_TIDS; i++) 235ad38bfc9SMatti Gottlieb kfree(sta->tx_lat[i].bins); 236ad38bfc9SMatti Gottlieb kfree(sta->tx_lat); 237ad38bfc9SMatti Gottlieb } 238ad38bfc9SMatti Gottlieb 239bdcbd8e0SJohannes Berg sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr); 24093e5deb1SJohannes Berg 24193e5deb1SJohannes Berg kfree(sta); 24293e5deb1SJohannes Berg } 24393e5deb1SJohannes Berg 2444d33960bSJohannes Berg /* Caller must hold local->sta_mtx */ 245d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local, 246d0709a65SJohannes Berg struct sta_info *sta) 247f0706e82SJiri Benc { 2484d33960bSJohannes Berg lockdep_assert_held(&local->sta_mtx); 24917741cdcSJohannes Berg sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)]; 250cf778b00SEric Dumazet rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta); 251f0706e82SJiri Benc } 252f0706e82SJiri Benc 253af818581SJohannes Berg static void sta_unblock(struct work_struct *wk) 254af818581SJohannes Berg { 255af818581SJohannes Berg struct sta_info *sta; 256af818581SJohannes Berg 257af818581SJohannes Berg sta = container_of(wk, struct sta_info, drv_unblock_wk); 258af818581SJohannes Berg 259af818581SJohannes Berg if (sta->dead) 260af818581SJohannes Berg return; 261af818581SJohannes Berg 26254420473SHelmut Schaa if (!test_sta_flag(sta, WLAN_STA_PS_STA)) { 26354420473SHelmut Schaa local_bh_disable(); 264af818581SJohannes Berg ieee80211_sta_ps_deliver_wakeup(sta); 26554420473SHelmut Schaa local_bh_enable(); 26654420473SHelmut Schaa } else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) { 267c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 268ce662b44SJohannes Berg 269ce662b44SJohannes Berg local_bh_disable(); 270af818581SJohannes Berg ieee80211_sta_ps_deliver_poll_response(sta); 271ce662b44SJohannes Berg local_bh_enable(); 272c2c98fdeSJohannes Berg } else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) { 273c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 274ce662b44SJohannes Berg 275ce662b44SJohannes Berg local_bh_disable(); 27647086fc5SJohannes Berg ieee80211_sta_ps_deliver_uapsd(sta); 277ce662b44SJohannes Berg local_bh_enable(); 27850a9432dSJohannes Berg } else 279c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 280af818581SJohannes Berg } 281af818581SJohannes Berg 282af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local, 283af65cd96SJohannes Berg struct sta_info *sta, gfp_t gfp) 284af65cd96SJohannes Berg { 285af65cd96SJohannes Berg if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) 286af65cd96SJohannes Berg return 0; 287af65cd96SJohannes Berg 288889cbb91SJohannes Berg sta->rate_ctrl = local->rate_ctrl; 289af65cd96SJohannes Berg sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, 290af65cd96SJohannes Berg &sta->sta, gfp); 291889cbb91SJohannes Berg if (!sta->rate_ctrl_priv) 292af65cd96SJohannes Berg return -ENOMEM; 293af65cd96SJohannes Berg 294af65cd96SJohannes Berg return 0; 295af65cd96SJohannes Berg } 296af65cd96SJohannes Berg 29773651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, 29856544160SJohannes Berg const u8 *addr, gfp_t gfp) 299f0706e82SJiri Benc { 300d0709a65SJohannes Berg struct ieee80211_local *local = sdata->local; 301f0706e82SJiri Benc struct sta_info *sta; 302ebe27c91SMohammed Shafi Shajakhan struct timespec uptime; 303ad38bfc9SMatti Gottlieb struct ieee80211_tx_latency_bin_ranges *tx_latency; 30416c5f15cSRon Rindjunsky int i; 305f0706e82SJiri Benc 30617741cdcSJohannes Berg sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp); 307f0706e82SJiri Benc if (!sta) 30873651ee6SJohannes Berg return NULL; 309f0706e82SJiri Benc 310ef04a297SJohannes Berg rcu_read_lock(); 311ef04a297SJohannes Berg tx_latency = rcu_dereference(local->tx_latency); 312ef04a297SJohannes Berg /* init stations Tx latency statistics && TID bins */ 313ef04a297SJohannes Berg if (tx_latency) { 314ef04a297SJohannes Berg sta->tx_lat = kzalloc(IEEE80211_NUM_TIDS * 315ef04a297SJohannes Berg sizeof(struct ieee80211_tx_latency_stat), 316ef04a297SJohannes Berg GFP_ATOMIC); 317ef04a297SJohannes Berg if (!sta->tx_lat) { 318ef04a297SJohannes Berg rcu_read_unlock(); 319ef04a297SJohannes Berg goto free; 320ef04a297SJohannes Berg } 321ef04a297SJohannes Berg 322ef04a297SJohannes Berg if (tx_latency->n_ranges) { 323ef04a297SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 324ef04a297SJohannes Berg /* size of bins is size of the ranges +1 */ 325ef04a297SJohannes Berg sta->tx_lat[i].bin_count = 326ef04a297SJohannes Berg tx_latency->n_ranges + 1; 327ef04a297SJohannes Berg sta->tx_lat[i].bins = 328ef04a297SJohannes Berg kcalloc(sta->tx_lat[i].bin_count, 329ef04a297SJohannes Berg sizeof(u32), GFP_ATOMIC); 330ef04a297SJohannes Berg if (!sta->tx_lat[i].bins) { 331ef04a297SJohannes Berg rcu_read_unlock(); 332ef04a297SJohannes Berg goto free; 333ef04a297SJohannes Berg } 334ef04a297SJohannes Berg } 335ef04a297SJohannes Berg } 336ef04a297SJohannes Berg } 337ef04a297SJohannes Berg rcu_read_unlock(); 338ef04a297SJohannes Berg 33907346f81SJohannes Berg spin_lock_init(&sta->lock); 3401d147bfaSEmmanuel Grumbach spin_lock_init(&sta->ps_lock); 341af818581SJohannes Berg INIT_WORK(&sta->drv_unblock_wk, sta_unblock); 34267c282c0SJohannes Berg INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work); 343a93e3644SJohannes Berg mutex_init(&sta->ampdu_mlme.mtx); 34487f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH 34587f59c70SThomas Pedersen if (ieee80211_vif_is_mesh(&sdata->vif) && 34687f59c70SThomas Pedersen !sdata->u.mesh.user_mpm) 34787f59c70SThomas Pedersen init_timer(&sta->plink_timer); 3486c7c4cbfSThomas Pedersen sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE; 34987f59c70SThomas Pedersen #endif 35007346f81SJohannes Berg 35117741cdcSJohannes Berg memcpy(sta->sta.addr, addr, ETH_ALEN); 352d0709a65SJohannes Berg sta->local = local; 353d0709a65SJohannes Berg sta->sdata = sdata; 3548bc8aecdSFelix Fietkau sta->last_rx = jiffies; 355f0706e82SJiri Benc 35671ec375cSJohannes Berg sta->sta_state = IEEE80211_STA_NONE; 35771ec375cSJohannes Berg 358ebe27c91SMohammed Shafi Shajakhan do_posix_clock_monotonic_gettime(&uptime); 359ebe27c91SMohammed Shafi Shajakhan sta->last_connected = uptime.tv_sec; 360541a45a1SBruno Randolf ewma_init(&sta->avg_signal, 1024, 8); 361ef0621e8SFelix Fietkau for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++) 362ef0621e8SFelix Fietkau ewma_init(&sta->chain_signal_avg[i], 1024, 8); 363541a45a1SBruno Randolf 364ef04a297SJohannes Berg if (sta_prepare_rate_control(local, sta, gfp)) 365ef04a297SJohannes Berg goto free; 366f0706e82SJiri Benc 3675a306f58SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 368a622ab72SJohannes Berg /* 369a622ab72SJohannes Berg * timer_to_tid must be initialized with identity mapping 370a622ab72SJohannes Berg * to enable session_timer's data differentiation. See 371a622ab72SJohannes Berg * sta_rx_agg_session_timer_expired for usage. 372a622ab72SJohannes Berg */ 37316c5f15cSRon Rindjunsky sta->timer_to_tid[i] = i; 37416c5f15cSRon Rindjunsky } 375948d887dSJohannes Berg for (i = 0; i < IEEE80211_NUM_ACS; i++) { 376948d887dSJohannes Berg skb_queue_head_init(&sta->ps_tx_buf[i]); 377948d887dSJohannes Berg skb_queue_head_init(&sta->tx_filtered[i]); 378948d887dSJohannes Berg } 37973651ee6SJohannes Berg 3805a306f58SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) 3814be929beSAlexey Dobriyan sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX); 382cccaec98SSenthil Balasubramanian 383af0ed69bSJohannes Berg sta->sta.smps_mode = IEEE80211_SMPS_OFF; 384687da132SEmmanuel Grumbach if (sdata->vif.type == NL80211_IFTYPE_AP || 385687da132SEmmanuel Grumbach sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 386687da132SEmmanuel Grumbach struct ieee80211_supported_band *sband = 387687da132SEmmanuel Grumbach local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)]; 388687da132SEmmanuel Grumbach u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 389687da132SEmmanuel Grumbach IEEE80211_HT_CAP_SM_PS_SHIFT; 390687da132SEmmanuel Grumbach /* 391687da132SEmmanuel Grumbach * Assume that hostapd advertises our caps in the beacon and 392687da132SEmmanuel Grumbach * this is the known_smps_mode for a station that just assciated 393687da132SEmmanuel Grumbach */ 394687da132SEmmanuel Grumbach switch (smps) { 395687da132SEmmanuel Grumbach case WLAN_HT_SMPS_CONTROL_DISABLED: 396687da132SEmmanuel Grumbach sta->known_smps_mode = IEEE80211_SMPS_OFF; 397687da132SEmmanuel Grumbach break; 398687da132SEmmanuel Grumbach case WLAN_HT_SMPS_CONTROL_STATIC: 399687da132SEmmanuel Grumbach sta->known_smps_mode = IEEE80211_SMPS_STATIC; 400687da132SEmmanuel Grumbach break; 401687da132SEmmanuel Grumbach case WLAN_HT_SMPS_CONTROL_DYNAMIC: 402687da132SEmmanuel Grumbach sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC; 403687da132SEmmanuel Grumbach break; 404687da132SEmmanuel Grumbach default: 405687da132SEmmanuel Grumbach WARN_ON(1); 406687da132SEmmanuel Grumbach } 407687da132SEmmanuel Grumbach } 408af0ed69bSJohannes Berg 409bdcbd8e0SJohannes Berg sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr); 41073651ee6SJohannes Berg return sta; 411ef04a297SJohannes Berg 412ef04a297SJohannes Berg free: 413ef04a297SJohannes Berg if (sta->tx_lat) { 414ef04a297SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) 415ef04a297SJohannes Berg kfree(sta->tx_lat[i].bins); 416ef04a297SJohannes Berg kfree(sta->tx_lat); 417ef04a297SJohannes Berg } 418ef04a297SJohannes Berg kfree(sta); 419ef04a297SJohannes Berg return NULL; 42073651ee6SJohannes Berg } 42173651ee6SJohannes Berg 4228c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta) 42334e89507SJohannes Berg { 42434e89507SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 42534e89507SJohannes Berg 42603e4497eSJohannes Berg /* 42703e4497eSJohannes Berg * Can't be a WARN_ON because it can be triggered through a race: 42803e4497eSJohannes Berg * something inserts a STA (on one CPU) without holding the RTNL 42903e4497eSJohannes Berg * and another CPU turns off the net device. 43003e4497eSJohannes Berg */ 4318c71df7aSGuy Eilam if (unlikely(!ieee80211_sdata_running(sdata))) 4328c71df7aSGuy Eilam return -ENETDOWN; 43303e4497eSJohannes Berg 434b203ca39SJoe Perches if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) || 4358c71df7aSGuy Eilam is_multicast_ether_addr(sta->sta.addr))) 4368c71df7aSGuy Eilam return -EINVAL; 4378c71df7aSGuy Eilam 4388c71df7aSGuy Eilam return 0; 43993e5deb1SJohannes Berg } 44044213b5eSJohannes Berg 441f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local, 442f09603a2SJohannes Berg struct ieee80211_sub_if_data *sdata, 443f09603a2SJohannes Berg struct sta_info *sta) 444f09603a2SJohannes Berg { 445f09603a2SJohannes Berg enum ieee80211_sta_state state; 446f09603a2SJohannes Berg int err = 0; 447f09603a2SJohannes Berg 448f09603a2SJohannes Berg for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) { 449f09603a2SJohannes Berg err = drv_sta_state(local, sdata, sta, state, state + 1); 450f09603a2SJohannes Berg if (err) 451f09603a2SJohannes Berg break; 452f09603a2SJohannes Berg } 453f09603a2SJohannes Berg 454f09603a2SJohannes Berg if (!err) { 455a4ec45a4SJohannes Berg /* 456a4ec45a4SJohannes Berg * Drivers using legacy sta_add/sta_remove callbacks only 457a4ec45a4SJohannes Berg * get uploaded set to true after sta_add is called. 458a4ec45a4SJohannes Berg */ 459a4ec45a4SJohannes Berg if (!local->ops->sta_add) 460f09603a2SJohannes Berg sta->uploaded = true; 461f09603a2SJohannes Berg return 0; 462f09603a2SJohannes Berg } 463f09603a2SJohannes Berg 464f09603a2SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { 465bdcbd8e0SJohannes Berg sdata_info(sdata, 466bdcbd8e0SJohannes Berg "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n", 467bdcbd8e0SJohannes Berg sta->sta.addr, state + 1, err); 468f09603a2SJohannes Berg err = 0; 469f09603a2SJohannes Berg } 470f09603a2SJohannes Berg 471f09603a2SJohannes Berg /* unwind on error */ 472f09603a2SJohannes Berg for (; state > IEEE80211_STA_NOTEXIST; state--) 473f09603a2SJohannes Berg WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1)); 474f09603a2SJohannes Berg 475f09603a2SJohannes Berg return err; 476f09603a2SJohannes Berg } 477f09603a2SJohannes Berg 47834e89507SJohannes Berg /* 4798c71df7aSGuy Eilam * should be called with sta_mtx locked 4808c71df7aSGuy Eilam * this function replaces the mutex lock 4818c71df7aSGuy Eilam * with a RCU lock 4828c71df7aSGuy Eilam */ 4834d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU) 4848c71df7aSGuy Eilam { 4858c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 4868c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 4877852e361SJohannes Berg struct station_info sinfo; 4888c71df7aSGuy Eilam int err = 0; 4898c71df7aSGuy Eilam 4908c71df7aSGuy Eilam lockdep_assert_held(&local->sta_mtx); 4918c71df7aSGuy Eilam 4927852e361SJohannes Berg /* check if STA exists already */ 4937852e361SJohannes Berg if (sta_info_get_bss(sdata, sta->sta.addr)) { 4944d33960bSJohannes Berg err = -EEXIST; 4954d33960bSJohannes Berg goto out_err; 49634e89507SJohannes Berg } 49734e89507SJohannes Berg 4984d33960bSJohannes Berg local->num_sta++; 4994d33960bSJohannes Berg local->sta_generation++; 5004d33960bSJohannes Berg smp_mb(); 5014d33960bSJohannes Berg 502*5108ca82SJohannes Berg /* simplify things and don't accept BA sessions yet */ 503*5108ca82SJohannes Berg set_sta_flag(sta, WLAN_STA_BLOCK_BA); 504*5108ca82SJohannes Berg 5054d33960bSJohannes Berg /* make the station visible */ 5064d33960bSJohannes Berg sta_info_hash_add(local, sta); 5074d33960bSJohannes Berg 508794454ceSArik Nemtsov list_add_rcu(&sta->list, &local->sta_list); 50983d5cc01SJohannes Berg 510*5108ca82SJohannes Berg /* notify driver */ 511*5108ca82SJohannes Berg err = sta_info_insert_drv_state(local, sdata, sta); 512*5108ca82SJohannes Berg if (err) 513*5108ca82SJohannes Berg goto out_remove; 514*5108ca82SJohannes Berg 51583d5cc01SJohannes Berg set_sta_flag(sta, WLAN_STA_INSERTED); 516*5108ca82SJohannes Berg /* accept BA sessions now */ 517*5108ca82SJohannes Berg clear_sta_flag(sta, WLAN_STA_BLOCK_BA); 5184d33960bSJohannes Berg 51921f659bfSEliad Peller ieee80211_recalc_min_chandef(sdata); 5204d33960bSJohannes Berg ieee80211_sta_debugfs_add(sta); 5214d33960bSJohannes Berg rate_control_add_sta_debugfs(sta); 5224d33960bSJohannes Berg 5234d33960bSJohannes Berg memset(&sinfo, 0, sizeof(sinfo)); 5244d33960bSJohannes Berg sinfo.filled = 0; 5254d33960bSJohannes Berg sinfo.generation = local->sta_generation; 5264d33960bSJohannes Berg cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL); 527d0709a65SJohannes Berg 528bdcbd8e0SJohannes Berg sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr); 529f0706e82SJiri Benc 53034e89507SJohannes Berg /* move reference to rcu-protected */ 53134e89507SJohannes Berg rcu_read_lock(); 53234e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 533e9f207f0SJiri Benc 53473651ee6SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 53573651ee6SJohannes Berg mesh_accept_plinks_update(sdata); 53673651ee6SJohannes Berg 53773651ee6SJohannes Berg return 0; 538*5108ca82SJohannes Berg out_remove: 539*5108ca82SJohannes Berg sta_info_hash_del(local, sta); 540*5108ca82SJohannes Berg list_del_rcu(&sta->list); 541*5108ca82SJohannes Berg local->num_sta--; 542*5108ca82SJohannes Berg synchronize_net(); 543*5108ca82SJohannes Berg __cleanup_single_sta(sta); 5444d33960bSJohannes Berg out_err: 5454d33960bSJohannes Berg mutex_unlock(&local->sta_mtx); 5464d33960bSJohannes Berg rcu_read_lock(); 5474d33960bSJohannes Berg return err; 5488c71df7aSGuy Eilam } 5498c71df7aSGuy Eilam 5508c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) 5518c71df7aSGuy Eilam { 5528c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 5538c71df7aSGuy Eilam int err = 0; 5548c71df7aSGuy Eilam 5554d33960bSJohannes Berg might_sleep(); 5564d33960bSJohannes Berg 5578c71df7aSGuy Eilam err = sta_info_insert_check(sta); 5588c71df7aSGuy Eilam if (err) { 5598c71df7aSGuy Eilam rcu_read_lock(); 5608c71df7aSGuy Eilam goto out_free; 5618c71df7aSGuy Eilam } 5628c71df7aSGuy Eilam 563004c872eSJohannes Berg mutex_lock(&local->sta_mtx); 564004c872eSJohannes Berg 5654d33960bSJohannes Berg err = sta_info_insert_finish(sta); 5668c71df7aSGuy Eilam if (err) 567004c872eSJohannes Berg goto out_free; 568d0709a65SJohannes Berg 569004c872eSJohannes Berg return 0; 57093e5deb1SJohannes Berg out_free: 57193e5deb1SJohannes Berg BUG_ON(!err); 572d9a7ddb0SJohannes Berg sta_info_free(local, sta); 57393e5deb1SJohannes Berg return err; 574f0706e82SJiri Benc } 575f0706e82SJiri Benc 57634e89507SJohannes Berg int sta_info_insert(struct sta_info *sta) 57734e89507SJohannes Berg { 57834e89507SJohannes Berg int err = sta_info_insert_rcu(sta); 57934e89507SJohannes Berg 58034e89507SJohannes Berg rcu_read_unlock(); 58134e89507SJohannes Berg 58234e89507SJohannes Berg return err; 58334e89507SJohannes Berg } 58434e89507SJohannes Berg 585d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id) 586004c872eSJohannes Berg { 587004c872eSJohannes Berg /* 588004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 589004c872eSJohannes Berg * so this line may not be changed to use the __set_bit() format. 590004c872eSJohannes Berg */ 591d012a605SMarco Porsch tim[id / 8] |= (1 << (id % 8)); 592004c872eSJohannes Berg } 593004c872eSJohannes Berg 594d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id) 595004c872eSJohannes Berg { 596004c872eSJohannes Berg /* 597004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 598004c872eSJohannes Berg * so this line may not be changed to use the __clear_bit() format. 599004c872eSJohannes Berg */ 600d012a605SMarco Porsch tim[id / 8] &= ~(1 << (id % 8)); 601004c872eSJohannes Berg } 602004c872eSJohannes Berg 6033d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id) 6043d5839b6SIlan Peer { 6053d5839b6SIlan Peer /* 6063d5839b6SIlan Peer * This format has been mandated by the IEEE specifications, 6073d5839b6SIlan Peer * so this line may not be changed to use the test_bit() format. 6083d5839b6SIlan Peer */ 6093d5839b6SIlan Peer return tim[id / 8] & (1 << (id % 8)); 6103d5839b6SIlan Peer } 6113d5839b6SIlan Peer 612948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac) 613004c872eSJohannes Berg { 614948d887dSJohannes Berg /* If we ever support TIDs > 7, this obviously needs to be adjusted */ 615948d887dSJohannes Berg switch (ac) { 616948d887dSJohannes Berg case IEEE80211_AC_VO: 617948d887dSJohannes Berg return BIT(6) | BIT(7); 618948d887dSJohannes Berg case IEEE80211_AC_VI: 619948d887dSJohannes Berg return BIT(4) | BIT(5); 620948d887dSJohannes Berg case IEEE80211_AC_BE: 621948d887dSJohannes Berg return BIT(0) | BIT(3); 622948d887dSJohannes Berg case IEEE80211_AC_BK: 623948d887dSJohannes Berg return BIT(1) | BIT(2); 624948d887dSJohannes Berg default: 625948d887dSJohannes Berg WARN_ON(1); 626948d887dSJohannes Berg return 0; 627d0709a65SJohannes Berg } 628004c872eSJohannes Berg } 629004c872eSJohannes Berg 630c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta) 631004c872eSJohannes Berg { 632c868cb35SJohannes Berg struct ieee80211_local *local = sta->local; 633d012a605SMarco Porsch struct ps_data *ps; 634948d887dSJohannes Berg bool indicate_tim = false; 635948d887dSJohannes Berg u8 ignore_for_tim = sta->sta.uapsd_queues; 636948d887dSJohannes Berg int ac; 637d012a605SMarco Porsch u16 id; 638004c872eSJohannes Berg 639d012a605SMarco Porsch if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 640d012a605SMarco Porsch sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 641c868cb35SJohannes Berg if (WARN_ON_ONCE(!sta->sdata->bss)) 642c868cb35SJohannes Berg return; 6433e122be0SJohannes Berg 644d012a605SMarco Porsch ps = &sta->sdata->bss->ps; 645d012a605SMarco Porsch id = sta->sta.aid; 6463f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH 6473f52b7e3SMarco Porsch } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) { 6483f52b7e3SMarco Porsch ps = &sta->sdata->u.mesh.ps; 649204d1304SThomas Pedersen /* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */ 6506f101ef0SChun-Yeow Yeoh id = sta->plid % (IEEE80211_MAX_AID + 1); 6513f52b7e3SMarco Porsch #endif 652d012a605SMarco Porsch } else { 653d012a605SMarco Porsch return; 654d012a605SMarco Porsch } 655d012a605SMarco Porsch 656c868cb35SJohannes Berg /* No need to do anything if the driver does all */ 657c868cb35SJohannes Berg if (local->hw.flags & IEEE80211_HW_AP_LINK_PS) 658c868cb35SJohannes Berg return; 659004c872eSJohannes Berg 660c868cb35SJohannes Berg if (sta->dead) 661c868cb35SJohannes Berg goto done; 6623e122be0SJohannes Berg 663948d887dSJohannes Berg /* 664948d887dSJohannes Berg * If all ACs are delivery-enabled then we should build 665948d887dSJohannes Berg * the TIM bit for all ACs anyway; if only some are then 666948d887dSJohannes Berg * we ignore those and build the TIM bit using only the 667948d887dSJohannes Berg * non-enabled ones. 668948d887dSJohannes Berg */ 669948d887dSJohannes Berg if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1) 670948d887dSJohannes Berg ignore_for_tim = 0; 671948d887dSJohannes Berg 672948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 673948d887dSJohannes Berg unsigned long tids; 674948d887dSJohannes Berg 675948d887dSJohannes Berg if (ignore_for_tim & BIT(ac)) 676948d887dSJohannes Berg continue; 677948d887dSJohannes Berg 678948d887dSJohannes Berg indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) || 679948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac]); 680948d887dSJohannes Berg if (indicate_tim) 681948d887dSJohannes Berg break; 682948d887dSJohannes Berg 683948d887dSJohannes Berg tids = ieee80211_tids_for_ac(ac); 684948d887dSJohannes Berg 685948d887dSJohannes Berg indicate_tim |= 686948d887dSJohannes Berg sta->driver_buffered_tids & tids; 687004c872eSJohannes Berg } 688004c872eSJohannes Berg 689c868cb35SJohannes Berg done: 69065f704a5SJohannes Berg spin_lock_bh(&local->tim_lock); 691004c872eSJohannes Berg 6923d5839b6SIlan Peer if (indicate_tim == __bss_tim_get(ps->tim, id)) 6933d5839b6SIlan Peer goto out_unlock; 6943d5839b6SIlan Peer 695948d887dSJohannes Berg if (indicate_tim) 696d012a605SMarco Porsch __bss_tim_set(ps->tim, id); 697c868cb35SJohannes Berg else 698d012a605SMarco Porsch __bss_tim_clear(ps->tim, id); 6993e122be0SJohannes Berg 700c868cb35SJohannes Berg if (local->ops->set_tim) { 701c868cb35SJohannes Berg local->tim_in_locked_section = true; 702948d887dSJohannes Berg drv_set_tim(local, &sta->sta, indicate_tim); 703c868cb35SJohannes Berg local->tim_in_locked_section = false; 704004c872eSJohannes Berg } 705004c872eSJohannes Berg 7063d5839b6SIlan Peer out_unlock: 70765f704a5SJohannes Berg spin_unlock_bh(&local->tim_lock); 708004c872eSJohannes Berg } 709004c872eSJohannes Berg 710cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb) 711f0706e82SJiri Benc { 712e039fa4aSJohannes Berg struct ieee80211_tx_info *info; 713f0706e82SJiri Benc int timeout; 714f0706e82SJiri Benc 715f0706e82SJiri Benc if (!skb) 716cd0b8d89SJohannes Berg return false; 717f0706e82SJiri Benc 718e039fa4aSJohannes Berg info = IEEE80211_SKB_CB(skb); 719f0706e82SJiri Benc 720f0706e82SJiri Benc /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ 72157c4d7b4SJohannes Berg timeout = (sta->listen_interval * 72257c4d7b4SJohannes Berg sta->sdata->vif.bss_conf.beacon_int * 72357c4d7b4SJohannes Berg 32 / 15625) * HZ; 724f0706e82SJiri Benc if (timeout < STA_TX_BUFFER_EXPIRE) 725f0706e82SJiri Benc timeout = STA_TX_BUFFER_EXPIRE; 726e039fa4aSJohannes Berg return time_after(jiffies, info->control.jiffies + timeout); 727f0706e82SJiri Benc } 728f0706e82SJiri Benc 729f0706e82SJiri Benc 730948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local, 731948d887dSJohannes Berg struct sta_info *sta, int ac) 732f0706e82SJiri Benc { 733f0706e82SJiri Benc unsigned long flags; 734f0706e82SJiri Benc struct sk_buff *skb; 735f0706e82SJiri Benc 73660750397SJohannes Berg /* 73760750397SJohannes Berg * First check for frames that should expire on the filtered 73860750397SJohannes Berg * queue. Frames here were rejected by the driver and are on 73960750397SJohannes Berg * a separate queue to avoid reordering with normal PS-buffered 74060750397SJohannes Berg * frames. They also aren't accounted for right now in the 74160750397SJohannes Berg * total_ps_buffered counter. 74260750397SJohannes Berg */ 743f0706e82SJiri Benc for (;;) { 744948d887dSJohannes Berg spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 745948d887dSJohannes Berg skb = skb_peek(&sta->tx_filtered[ac]); 74657c4d7b4SJohannes Berg if (sta_info_buffer_expired(sta, skb)) 747948d887dSJohannes Berg skb = __skb_dequeue(&sta->tx_filtered[ac]); 748836341a7SJohannes Berg else 749f0706e82SJiri Benc skb = NULL; 750948d887dSJohannes Berg spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 751f0706e82SJiri Benc 75260750397SJohannes Berg /* 75360750397SJohannes Berg * Frames are queued in order, so if this one 75460750397SJohannes Berg * hasn't expired yet we can stop testing. If 75560750397SJohannes Berg * we actually reached the end of the queue we 75660750397SJohannes Berg * also need to stop, of course. 75760750397SJohannes Berg */ 75860750397SJohannes Berg if (!skb) 75960750397SJohannes Berg break; 760d4fa14cdSFelix Fietkau ieee80211_free_txskb(&local->hw, skb); 76160750397SJohannes Berg } 76260750397SJohannes Berg 76360750397SJohannes Berg /* 76460750397SJohannes Berg * Now also check the normal PS-buffered queue, this will 76560750397SJohannes Berg * only find something if the filtered queue was emptied 76660750397SJohannes Berg * since the filtered frames are all before the normal PS 76760750397SJohannes Berg * buffered frames. 76860750397SJohannes Berg */ 769f0706e82SJiri Benc for (;;) { 770948d887dSJohannes Berg spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 771948d887dSJohannes Berg skb = skb_peek(&sta->ps_tx_buf[ac]); 772f0706e82SJiri Benc if (sta_info_buffer_expired(sta, skb)) 773948d887dSJohannes Berg skb = __skb_dequeue(&sta->ps_tx_buf[ac]); 774f0706e82SJiri Benc else 775f0706e82SJiri Benc skb = NULL; 776948d887dSJohannes Berg spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 777f0706e82SJiri Benc 77860750397SJohannes Berg /* 77960750397SJohannes Berg * frames are queued in order, so if this one 78060750397SJohannes Berg * hasn't expired yet (or we reached the end of 78160750397SJohannes Berg * the queue) we can stop testing 78260750397SJohannes Berg */ 783836341a7SJohannes Berg if (!skb) 784836341a7SJohannes Berg break; 785836341a7SJohannes Berg 786f0706e82SJiri Benc local->total_ps_buffered--; 787bdcbd8e0SJohannes Berg ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n", 788bdcbd8e0SJohannes Berg sta->sta.addr); 789d4fa14cdSFelix Fietkau ieee80211_free_txskb(&local->hw, skb); 790f0706e82SJiri Benc } 7913393a608SJuuso Oikarinen 79260750397SJohannes Berg /* 79360750397SJohannes Berg * Finally, recalculate the TIM bit for this station -- it might 79460750397SJohannes Berg * now be clear because the station was too slow to retrieve its 79560750397SJohannes Berg * frames. 79660750397SJohannes Berg */ 79760750397SJohannes Berg sta_info_recalc_tim(sta); 79860750397SJohannes Berg 79960750397SJohannes Berg /* 80060750397SJohannes Berg * Return whether there are any frames still buffered, this is 80160750397SJohannes Berg * used to check whether the cleanup timer still needs to run, 80260750397SJohannes Berg * if there are no frames we don't need to rearm the timer. 80360750397SJohannes Berg */ 804948d887dSJohannes Berg return !(skb_queue_empty(&sta->ps_tx_buf[ac]) && 805948d887dSJohannes Berg skb_queue_empty(&sta->tx_filtered[ac])); 806948d887dSJohannes Berg } 807948d887dSJohannes Berg 808948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local, 809948d887dSJohannes Berg struct sta_info *sta) 810948d887dSJohannes Berg { 811948d887dSJohannes Berg bool have_buffered = false; 812948d887dSJohannes Berg int ac; 813948d887dSJohannes Berg 8143f52b7e3SMarco Porsch /* This is only necessary for stations on BSS/MBSS interfaces */ 8153f52b7e3SMarco Porsch if (!sta->sdata->bss && 8163f52b7e3SMarco Porsch !ieee80211_vif_is_mesh(&sta->sdata->vif)) 817948d887dSJohannes Berg return false; 818948d887dSJohannes Berg 819948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 820948d887dSJohannes Berg have_buffered |= 821948d887dSJohannes Berg sta_info_cleanup_expire_buffered_ac(local, sta, ac); 822948d887dSJohannes Berg 823948d887dSJohannes Berg return have_buffered; 824f0706e82SJiri Benc } 825f0706e82SJiri Benc 826d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta) 82734e89507SJohannes Berg { 82834e89507SJohannes Berg struct ieee80211_local *local; 82934e89507SJohannes Berg struct ieee80211_sub_if_data *sdata; 8306d10e46bSJohannes Berg int ret; 83134e89507SJohannes Berg 83234e89507SJohannes Berg might_sleep(); 83334e89507SJohannes Berg 83434e89507SJohannes Berg if (!sta) 83534e89507SJohannes Berg return -ENOENT; 83634e89507SJohannes Berg 83734e89507SJohannes Berg local = sta->local; 83834e89507SJohannes Berg sdata = sta->sdata; 83934e89507SJohannes Berg 84083d5cc01SJohannes Berg lockdep_assert_held(&local->sta_mtx); 84183d5cc01SJohannes Berg 842098a6070SJohannes Berg /* 843098a6070SJohannes Berg * Before removing the station from the driver and 844098a6070SJohannes Berg * rate control, it might still start new aggregation 845098a6070SJohannes Berg * sessions -- block that to make sure the tear-down 846098a6070SJohannes Berg * will be sufficient. 847098a6070SJohannes Berg */ 848c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_BLOCK_BA); 849c82c4a80SJohannes Berg ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA); 850098a6070SJohannes Berg 85134e89507SJohannes Berg ret = sta_info_hash_del(local, sta); 852b01711beSJohannes Berg if (WARN_ON(ret)) 85334e89507SJohannes Berg return ret; 85434e89507SJohannes Berg 855794454ceSArik Nemtsov list_del_rcu(&sta->list); 8564d33960bSJohannes Berg 8576a9d1b91SJohannes Berg drv_sta_pre_rcu_remove(local, sta->sdata, sta); 8586a9d1b91SJohannes Berg 859a710c816SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 860a710c816SJohannes Berg rcu_access_pointer(sdata->u.vlan.sta) == sta) 861a710c816SJohannes Berg RCU_INIT_POINTER(sdata->u.vlan.sta, NULL); 862a710c816SJohannes Berg 863d778207bSJohannes Berg return 0; 864d778207bSJohannes Berg } 865d778207bSJohannes Berg 866d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta) 867d778207bSJohannes Berg { 868d778207bSJohannes Berg struct ieee80211_local *local = sta->local; 869d778207bSJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 870d778207bSJohannes Berg int ret; 871d778207bSJohannes Berg 872d778207bSJohannes Berg /* 873d778207bSJohannes Berg * NOTE: This assumes at least synchronize_net() was done 874d778207bSJohannes Berg * after _part1 and before _part2! 875d778207bSJohannes Berg */ 876d778207bSJohannes Berg 877d778207bSJohannes Berg might_sleep(); 878d778207bSJohannes Berg lockdep_assert_held(&local->sta_mtx); 879d778207bSJohannes Berg 880c8782078SJohannes Berg /* now keys can no longer be reached */ 8816d10e46bSJohannes Berg ieee80211_free_sta_keys(local, sta); 88234e89507SJohannes Berg 88334e89507SJohannes Berg sta->dead = true; 88434e89507SJohannes Berg 88534e89507SJohannes Berg local->num_sta--; 88634e89507SJohannes Berg local->sta_generation++; 88734e89507SJohannes Berg 88883d5cc01SJohannes Berg while (sta->sta_state > IEEE80211_STA_NONE) { 889f09603a2SJohannes Berg ret = sta_info_move_state(sta, sta->sta_state - 1); 890f09603a2SJohannes Berg if (ret) { 89183d5cc01SJohannes Berg WARN_ON_ONCE(1); 89283d5cc01SJohannes Berg break; 89383d5cc01SJohannes Berg } 89483d5cc01SJohannes Berg } 895d9a7ddb0SJohannes Berg 896f09603a2SJohannes Berg if (sta->uploaded) { 897f09603a2SJohannes Berg ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE, 898f09603a2SJohannes Berg IEEE80211_STA_NOTEXIST); 899f09603a2SJohannes Berg WARN_ON_ONCE(ret != 0); 900f09603a2SJohannes Berg } 90134e89507SJohannes Berg 902bdcbd8e0SJohannes Berg sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr); 903bdcbd8e0SJohannes Berg 904ec15e68bSJouni Malinen cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL); 905ec15e68bSJouni Malinen 90634e89507SJohannes Berg rate_control_remove_sta_debugfs(sta); 90734e89507SJohannes Berg ieee80211_sta_debugfs_remove(sta); 90821f659bfSEliad Peller ieee80211_recalc_min_chandef(sdata); 90934e89507SJohannes Berg 910d34ba216SJohannes Berg cleanup_single_sta(sta); 911d778207bSJohannes Berg } 912d778207bSJohannes Berg 913d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta) 914d778207bSJohannes Berg { 915d778207bSJohannes Berg int err = __sta_info_destroy_part1(sta); 916d778207bSJohannes Berg 917d778207bSJohannes Berg if (err) 918d778207bSJohannes Berg return err; 919d778207bSJohannes Berg 920d778207bSJohannes Berg synchronize_net(); 921d778207bSJohannes Berg 922d778207bSJohannes Berg __sta_info_destroy_part2(sta); 92334e89507SJohannes Berg 92434e89507SJohannes Berg return 0; 92534e89507SJohannes Berg } 92634e89507SJohannes Berg 92734e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr) 92834e89507SJohannes Berg { 92934e89507SJohannes Berg struct sta_info *sta; 93034e89507SJohannes Berg int ret; 93134e89507SJohannes Berg 93234e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 9337852e361SJohannes Berg sta = sta_info_get(sdata, addr); 93434e89507SJohannes Berg ret = __sta_info_destroy(sta); 93534e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 93634e89507SJohannes Berg 93734e89507SJohannes Berg return ret; 93834e89507SJohannes Berg } 93934e89507SJohannes Berg 94034e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata, 94134e89507SJohannes Berg const u8 *addr) 94234e89507SJohannes Berg { 94334e89507SJohannes Berg struct sta_info *sta; 94434e89507SJohannes Berg int ret; 94534e89507SJohannes Berg 94634e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 9477852e361SJohannes Berg sta = sta_info_get_bss(sdata, addr); 94834e89507SJohannes Berg ret = __sta_info_destroy(sta); 94934e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 95034e89507SJohannes Berg 95134e89507SJohannes Berg return ret; 95234e89507SJohannes Berg } 953f0706e82SJiri Benc 954f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data) 955f0706e82SJiri Benc { 956f0706e82SJiri Benc struct ieee80211_local *local = (struct ieee80211_local *) data; 957f0706e82SJiri Benc struct sta_info *sta; 9583393a608SJuuso Oikarinen bool timer_needed = false; 959f0706e82SJiri Benc 960d0709a65SJohannes Berg rcu_read_lock(); 961d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) 9623393a608SJuuso Oikarinen if (sta_info_cleanup_expire_buffered(local, sta)) 9633393a608SJuuso Oikarinen timer_needed = true; 964d0709a65SJohannes Berg rcu_read_unlock(); 965f0706e82SJiri Benc 9665bb644a0SJohannes Berg if (local->quiescing) 9675bb644a0SJohannes Berg return; 9685bb644a0SJohannes Berg 9693393a608SJuuso Oikarinen if (!timer_needed) 9703393a608SJuuso Oikarinen return; 9713393a608SJuuso Oikarinen 97226d59535SJohannes Berg mod_timer(&local->sta_cleanup, 97326d59535SJohannes Berg round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); 974f0706e82SJiri Benc } 975f0706e82SJiri Benc 976f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local) 977f0706e82SJiri Benc { 9784d33960bSJohannes Berg spin_lock_init(&local->tim_lock); 97934e89507SJohannes Berg mutex_init(&local->sta_mtx); 980f0706e82SJiri Benc INIT_LIST_HEAD(&local->sta_list); 981f0706e82SJiri Benc 982b24b8a24SPavel Emelyanov setup_timer(&local->sta_cleanup, sta_info_cleanup, 983b24b8a24SPavel Emelyanov (unsigned long)local); 984f0706e82SJiri Benc } 985f0706e82SJiri Benc 986f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local) 987f0706e82SJiri Benc { 988a56f992cSJohannes Berg del_timer_sync(&local->sta_cleanup); 989f0706e82SJiri Benc } 990f0706e82SJiri Benc 991051007d9SJohannes Berg 992e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans) 993f0706e82SJiri Benc { 994b998e8bbSJohannes Berg struct ieee80211_local *local = sdata->local; 995f0706e82SJiri Benc struct sta_info *sta, *tmp; 996d778207bSJohannes Berg LIST_HEAD(free_list); 99744213b5eSJohannes Berg int ret = 0; 998f0706e82SJiri Benc 999d0709a65SJohannes Berg might_sleep(); 1000d0709a65SJohannes Berg 1001e716251dSJohannes Berg WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP); 1002e716251dSJohannes Berg WARN_ON(vlans && !sdata->bss); 1003e716251dSJohannes Berg 100434e89507SJohannes Berg mutex_lock(&local->sta_mtx); 100534e89507SJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 1006e716251dSJohannes Berg if (sdata == sta->sdata || 1007e716251dSJohannes Berg (vlans && sdata->bss == sta->sdata->bss)) { 1008d778207bSJohannes Berg if (!WARN_ON(__sta_info_destroy_part1(sta))) 1009d778207bSJohannes Berg list_add(&sta->free_list, &free_list); 101034316837SJohannes Berg ret++; 101134316837SJohannes Berg } 101234e89507SJohannes Berg } 1013d778207bSJohannes Berg 1014d778207bSJohannes Berg if (!list_empty(&free_list)) { 1015d778207bSJohannes Berg synchronize_net(); 1016d778207bSJohannes Berg list_for_each_entry_safe(sta, tmp, &free_list, free_list) 1017d778207bSJohannes Berg __sta_info_destroy_part2(sta); 1018d778207bSJohannes Berg } 101934e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 102044213b5eSJohannes Berg 1021051007d9SJohannes Berg return ret; 1022051007d9SJohannes Berg } 1023051007d9SJohannes Berg 102424723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, 102524723d1bSJohannes Berg unsigned long exp_time) 102624723d1bSJohannes Berg { 102724723d1bSJohannes Berg struct ieee80211_local *local = sdata->local; 102824723d1bSJohannes Berg struct sta_info *sta, *tmp; 102924723d1bSJohannes Berg 103034e89507SJohannes Berg mutex_lock(&local->sta_mtx); 1031e46a2cf9SMohammed Shafi Shajakhan 1032e46a2cf9SMohammed Shafi Shajakhan list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 1033ec2b774eSMarek Lindner if (sdata != sta->sdata) 1034ec2b774eSMarek Lindner continue; 1035ec2b774eSMarek Lindner 103624723d1bSJohannes Berg if (time_after(jiffies, sta->last_rx + exp_time)) { 1037eea57d42SMohammed Shafi Shajakhan sta_dbg(sta->sdata, "expiring inactive STA %pM\n", 1038bdcbd8e0SJohannes Berg sta->sta.addr); 10393f52b7e3SMarco Porsch 10403f52b7e3SMarco Porsch if (ieee80211_vif_is_mesh(&sdata->vif) && 10413f52b7e3SMarco Porsch test_sta_flag(sta, WLAN_STA_PS_STA)) 10423f52b7e3SMarco Porsch atomic_dec(&sdata->u.mesh.ps.num_sta_ps); 10433f52b7e3SMarco Porsch 104434e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 104524723d1bSJohannes Berg } 1046e46a2cf9SMohammed Shafi Shajakhan } 1047e46a2cf9SMohammed Shafi Shajakhan 104834e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 104924723d1bSJohannes Berg } 105017741cdcSJohannes Berg 1051686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, 1052686b9cb9SBen Greear const u8 *addr, 1053686b9cb9SBen Greear const u8 *localaddr) 105417741cdcSJohannes Berg { 1055abe60632SJohannes Berg struct sta_info *sta, *nxt; 105617741cdcSJohannes Berg 1057686b9cb9SBen Greear /* 1058686b9cb9SBen Greear * Just return a random station if localaddr is NULL 1059686b9cb9SBen Greear * ... first in list. 1060686b9cb9SBen Greear */ 1061f7c65594SJohannes Berg for_each_sta_info(hw_to_local(hw), addr, sta, nxt) { 1062686b9cb9SBen Greear if (localaddr && 1063b203ca39SJoe Perches !ether_addr_equal(sta->sdata->vif.addr, localaddr)) 1064686b9cb9SBen Greear continue; 1065f7c65594SJohannes Berg if (!sta->uploaded) 1066f7c65594SJohannes Berg return NULL; 106717741cdcSJohannes Berg return &sta->sta; 1068f7c65594SJohannes Berg } 1069f7c65594SJohannes Berg 1070abe60632SJohannes Berg return NULL; 107117741cdcSJohannes Berg } 1072686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr); 10735ed176e1SJohannes Berg 10745ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif, 10755ed176e1SJohannes Berg const u8 *addr) 10765ed176e1SJohannes Berg { 1077f7c65594SJohannes Berg struct sta_info *sta; 10785ed176e1SJohannes Berg 10795ed176e1SJohannes Berg if (!vif) 10805ed176e1SJohannes Berg return NULL; 10815ed176e1SJohannes Berg 1082f7c65594SJohannes Berg sta = sta_info_get_bss(vif_to_sdata(vif), addr); 1083f7c65594SJohannes Berg if (!sta) 1084f7c65594SJohannes Berg return NULL; 10855ed176e1SJohannes Berg 1086f7c65594SJohannes Berg if (!sta->uploaded) 1087f7c65594SJohannes Berg return NULL; 1088f7c65594SJohannes Berg 1089f7c65594SJohannes Berg return &sta->sta; 10905ed176e1SJohannes Berg } 109117741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta); 1092af818581SJohannes Berg 109350a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta) 109450a9432dSJohannes Berg { 109550a9432dSJohannes Berg struct sta_info *sta = _sta; 1096608383bfSHelmut Schaa struct ieee80211_sub_if_data *sdata = sta->sdata; 1097d012a605SMarco Porsch struct ps_data *ps; 1098d012a605SMarco Porsch 1099d012a605SMarco Porsch if (sdata->vif.type == NL80211_IFTYPE_AP || 1100d012a605SMarco Porsch sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 1101d012a605SMarco Porsch ps = &sdata->bss->ps; 11023f52b7e3SMarco Porsch else if (ieee80211_vif_is_mesh(&sdata->vif)) 11033f52b7e3SMarco Porsch ps = &sdata->u.mesh.ps; 1104d012a605SMarco Porsch else 1105d012a605SMarco Porsch return; 110650a9432dSJohannes Berg 1107c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 1108608383bfSHelmut Schaa if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA)) 1109d012a605SMarco Porsch atomic_dec(&ps->num_sta_ps); 111050a9432dSJohannes Berg } 111150a9432dSJohannes Berg 1112af818581SJohannes Berg /* powersave support code */ 1113af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) 1114af818581SJohannes Berg { 1115af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1116af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 1117948d887dSJohannes Berg struct sk_buff_head pending; 1118948d887dSJohannes Berg int filtered = 0, buffered = 0, ac; 1119987c285cSArik Nemtsov unsigned long flags; 1120af818581SJohannes Berg 1121c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_SP); 112247086fc5SJohannes Berg 11235a306f58SJohannes Berg BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1); 1124948d887dSJohannes Berg sta->driver_buffered_tids = 0; 1125948d887dSJohannes Berg 1126d057e5a3SArik Nemtsov if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS)) 112712375ef9SJohannes Berg drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); 1128af818581SJohannes Berg 1129948d887dSJohannes Berg skb_queue_head_init(&pending); 1130af818581SJohannes Berg 11311d147bfaSEmmanuel Grumbach /* sync with ieee80211_tx_h_unicast_ps_buf */ 11321d147bfaSEmmanuel Grumbach spin_lock(&sta->ps_lock); 1133af818581SJohannes Berg /* Send all buffered frames to the station */ 1134948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1135948d887dSJohannes Berg int count = skb_queue_len(&pending), tmp; 1136948d887dSJohannes Berg 1137987c285cSArik Nemtsov spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 1138948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending); 1139987c285cSArik Nemtsov spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 1140948d887dSJohannes Berg tmp = skb_queue_len(&pending); 1141948d887dSJohannes Berg filtered += tmp - count; 1142948d887dSJohannes Berg count = tmp; 1143948d887dSJohannes Berg 1144987c285cSArik Nemtsov spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 1145948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending); 1146987c285cSArik Nemtsov spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 1147948d887dSJohannes Berg tmp = skb_queue_len(&pending); 1148948d887dSJohannes Berg buffered += tmp - count; 1149948d887dSJohannes Berg } 1150948d887dSJohannes Berg 1151948d887dSJohannes Berg ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta); 11521d147bfaSEmmanuel Grumbach spin_unlock(&sta->ps_lock); 1153948d887dSJohannes Berg 1154687da132SEmmanuel Grumbach /* This station just woke up and isn't aware of our SMPS state */ 1155687da132SEmmanuel Grumbach if (!ieee80211_smps_is_restrictive(sta->known_smps_mode, 1156687da132SEmmanuel Grumbach sdata->smps_mode) && 1157687da132SEmmanuel Grumbach sta->known_smps_mode != sdata->bss->req_smps && 1158687da132SEmmanuel Grumbach sta_info_tx_streams(sta) != 1) { 1159687da132SEmmanuel Grumbach ht_dbg(sdata, 1160687da132SEmmanuel Grumbach "%pM just woke up and MIMO capable - update SMPS\n", 1161687da132SEmmanuel Grumbach sta->sta.addr); 1162687da132SEmmanuel Grumbach ieee80211_send_smps_action(sdata, sdata->bss->req_smps, 1163687da132SEmmanuel Grumbach sta->sta.addr, 1164687da132SEmmanuel Grumbach sdata->vif.bss_conf.bssid); 1165687da132SEmmanuel Grumbach } 1166687da132SEmmanuel Grumbach 1167af818581SJohannes Berg local->total_ps_buffered -= buffered; 1168af818581SJohannes Berg 1169c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1170c868cb35SJohannes Berg 1171bdcbd8e0SJohannes Berg ps_dbg(sdata, 1172bdcbd8e0SJohannes Berg "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n", 1173bdcbd8e0SJohannes Berg sta->sta.addr, sta->sta.aid, filtered, buffered); 1174af818581SJohannes Berg } 1175af818581SJohannes Berg 1176ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata, 1177ce662b44SJohannes Berg struct sta_info *sta, int tid, 1178b77cf4f8SJohannes Berg enum ieee80211_frame_release_type reason, 1179b77cf4f8SJohannes Berg bool call_driver) 1180ce662b44SJohannes Berg { 1181ce662b44SJohannes Berg struct ieee80211_local *local = sdata->local; 1182ce662b44SJohannes Berg struct ieee80211_qos_hdr *nullfunc; 1183ce662b44SJohannes Berg struct sk_buff *skb; 1184ce662b44SJohannes Berg int size = sizeof(*nullfunc); 1185ce662b44SJohannes Berg __le16 fc; 1186c2c98fdeSJohannes Berg bool qos = test_sta_flag(sta, WLAN_STA_WME); 1187ce662b44SJohannes Berg struct ieee80211_tx_info *info; 118855de908aSJohannes Berg struct ieee80211_chanctx_conf *chanctx_conf; 1189ce662b44SJohannes Berg 1190ce662b44SJohannes Berg if (qos) { 1191ce662b44SJohannes Berg fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1192ce662b44SJohannes Berg IEEE80211_STYPE_QOS_NULLFUNC | 1193ce662b44SJohannes Berg IEEE80211_FCTL_FROMDS); 1194ce662b44SJohannes Berg } else { 1195ce662b44SJohannes Berg size -= 2; 1196ce662b44SJohannes Berg fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1197ce662b44SJohannes Berg IEEE80211_STYPE_NULLFUNC | 1198ce662b44SJohannes Berg IEEE80211_FCTL_FROMDS); 1199ce662b44SJohannes Berg } 1200ce662b44SJohannes Berg 1201ce662b44SJohannes Berg skb = dev_alloc_skb(local->hw.extra_tx_headroom + size); 1202ce662b44SJohannes Berg if (!skb) 1203ce662b44SJohannes Berg return; 1204ce662b44SJohannes Berg 1205ce662b44SJohannes Berg skb_reserve(skb, local->hw.extra_tx_headroom); 1206ce662b44SJohannes Berg 1207ce662b44SJohannes Berg nullfunc = (void *) skb_put(skb, size); 1208ce662b44SJohannes Berg nullfunc->frame_control = fc; 1209ce662b44SJohannes Berg nullfunc->duration_id = 0; 1210ce662b44SJohannes Berg memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN); 1211ce662b44SJohannes Berg memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN); 1212ce662b44SJohannes Berg memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN); 1213ce662b44SJohannes Berg 1214ce662b44SJohannes Berg skb->priority = tid; 1215ce662b44SJohannes Berg skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]); 121659b66255SJohannes Berg if (qos) { 1217ce662b44SJohannes Berg nullfunc->qos_ctrl = cpu_to_le16(tid); 1218ce662b44SJohannes Berg 121940b96408SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_UAPSD) 1220ce662b44SJohannes Berg nullfunc->qos_ctrl |= 1221ce662b44SJohannes Berg cpu_to_le16(IEEE80211_QOS_CTL_EOSP); 1222ce662b44SJohannes Berg } 1223ce662b44SJohannes Berg 1224ce662b44SJohannes Berg info = IEEE80211_SKB_CB(skb); 1225ce662b44SJohannes Berg 1226ce662b44SJohannes Berg /* 1227ce662b44SJohannes Berg * Tell TX path to send this frame even though the 1228ce662b44SJohannes Berg * STA may still remain is PS mode after this frame 1229deeaee19SJohannes Berg * exchange. Also set EOSP to indicate this packet 1230deeaee19SJohannes Berg * ends the poll/service period. 1231ce662b44SJohannes Berg */ 123202f2f1a9SJohannes Berg info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER | 1233d6d23de2SFelix Fietkau IEEE80211_TX_CTL_PS_RESPONSE | 1234deeaee19SJohannes Berg IEEE80211_TX_STATUS_EOSP | 1235ce662b44SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 1236ce662b44SJohannes Berg 1237b77cf4f8SJohannes Berg if (call_driver) 1238b77cf4f8SJohannes Berg drv_allow_buffered_frames(local, sta, BIT(tid), 1, 1239b77cf4f8SJohannes Berg reason, false); 124040b96408SJohannes Berg 124189afe614SJohannes Berg skb->dev = sdata->dev; 124289afe614SJohannes Berg 124355de908aSJohannes Berg rcu_read_lock(); 124455de908aSJohannes Berg chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf); 124555de908aSJohannes Berg if (WARN_ON(!chanctx_conf)) { 124655de908aSJohannes Berg rcu_read_unlock(); 124755de908aSJohannes Berg kfree_skb(skb); 124855de908aSJohannes Berg return; 124955de908aSJohannes Berg } 125055de908aSJohannes Berg 12514bf88530SJohannes Berg ieee80211_xmit(sdata, skb, chanctx_conf->def.chan->band); 125255de908aSJohannes Berg rcu_read_unlock(); 1253ce662b44SJohannes Berg } 1254ce662b44SJohannes Berg 12550a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids) 12560a1cb809SJohannes Berg { 12570a1cb809SJohannes Berg /* lower 3 TIDs aren't ordered perfectly */ 12580a1cb809SJohannes Berg if (tids & 0xF8) 12590a1cb809SJohannes Berg return fls(tids) - 1; 12600a1cb809SJohannes Berg /* TID 0 is BE just like TID 3 */ 12610a1cb809SJohannes Berg if (tids & BIT(0)) 12620a1cb809SJohannes Berg return 0; 12630a1cb809SJohannes Berg return fls(tids) - 1; 12640a1cb809SJohannes Berg } 12650a1cb809SJohannes Berg 126647086fc5SJohannes Berg static void 126747086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta, 126847086fc5SJohannes Berg int n_frames, u8 ignored_acs, 126947086fc5SJohannes Berg enum ieee80211_frame_release_type reason) 1270af818581SJohannes Berg { 1271af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1272af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 1273948d887dSJohannes Berg bool more_data = false; 1274948d887dSJohannes Berg int ac; 12754049e09aSJohannes Berg unsigned long driver_release_tids = 0; 127647086fc5SJohannes Berg struct sk_buff_head frames; 127747086fc5SJohannes Berg 1278deeaee19SJohannes Berg /* Service or PS-Poll period starts */ 1279c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_SP); 1280deeaee19SJohannes Berg 128147086fc5SJohannes Berg __skb_queue_head_init(&frames); 1282af818581SJohannes Berg 1283f9f760b4SJohannes Berg /* Get response frame(s) and more data bit for the last one. */ 1284948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 12854049e09aSJohannes Berg unsigned long tids; 12864049e09aSJohannes Berg 128747086fc5SJohannes Berg if (ignored_acs & BIT(ac)) 1288948d887dSJohannes Berg continue; 1289948d887dSJohannes Berg 12904049e09aSJohannes Berg tids = ieee80211_tids_for_ac(ac); 12914049e09aSJohannes Berg 1292f9f760b4SJohannes Berg /* if we already have frames from software, then we can't also 1293f9f760b4SJohannes Berg * release from hardware queues 1294f9f760b4SJohannes Berg */ 1295f9f760b4SJohannes Berg if (skb_queue_empty(&frames)) 1296f9f760b4SJohannes Berg driver_release_tids |= sta->driver_buffered_tids & tids; 1297f9f760b4SJohannes Berg 12984049e09aSJohannes Berg if (driver_release_tids) { 1299f9f760b4SJohannes Berg /* If the driver has data on more than one TID then 1300f9f760b4SJohannes Berg * certainly there's more data if we release just a 1301f9f760b4SJohannes Berg * single frame now (from a single TID). This will 1302f9f760b4SJohannes Berg * only happen for PS-Poll. 1303f9f760b4SJohannes Berg */ 1304f9f760b4SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_PSPOLL && 1305f9f760b4SJohannes Berg hweight16(driver_release_tids) > 1) { 1306f9f760b4SJohannes Berg more_data = true; 1307f9f760b4SJohannes Berg driver_release_tids = 1308f9f760b4SJohannes Berg BIT(find_highest_prio_tid( 1309f9f760b4SJohannes Berg driver_release_tids)); 1310f9f760b4SJohannes Berg break; 1311f9f760b4SJohannes Berg } 13124049e09aSJohannes Berg } else { 131347086fc5SJohannes Berg struct sk_buff *skb; 131447086fc5SJohannes Berg 131547086fc5SJohannes Berg while (n_frames > 0) { 1316948d887dSJohannes Berg skb = skb_dequeue(&sta->tx_filtered[ac]); 1317948d887dSJohannes Berg if (!skb) { 131847086fc5SJohannes Berg skb = skb_dequeue( 131947086fc5SJohannes Berg &sta->ps_tx_buf[ac]); 1320af818581SJohannes Berg if (skb) 1321af818581SJohannes Berg local->total_ps_buffered--; 1322af818581SJohannes Berg } 132347086fc5SJohannes Berg if (!skb) 132447086fc5SJohannes Berg break; 132547086fc5SJohannes Berg n_frames--; 132647086fc5SJohannes Berg __skb_queue_tail(&frames, skb); 132747086fc5SJohannes Berg } 1328948d887dSJohannes Berg } 1329948d887dSJohannes Berg 1330f9f760b4SJohannes Berg /* If we have more frames buffered on this AC, then set the 1331f9f760b4SJohannes Berg * more-data bit and abort the loop since we can't send more 1332f9f760b4SJohannes Berg * data from other ACs before the buffered frames from this. 13334049e09aSJohannes Berg */ 1334948d887dSJohannes Berg if (!skb_queue_empty(&sta->tx_filtered[ac]) || 1335948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac])) { 1336948d887dSJohannes Berg more_data = true; 1337948d887dSJohannes Berg break; 1338948d887dSJohannes Berg } 1339948d887dSJohannes Berg } 1340af818581SJohannes Berg 1341f9f760b4SJohannes Berg if (skb_queue_empty(&frames) && !driver_release_tids) { 1342ce662b44SJohannes Berg int tid; 13434049e09aSJohannes Berg 1344ce662b44SJohannes Berg /* 1345ce662b44SJohannes Berg * For PS-Poll, this can only happen due to a race condition 1346ce662b44SJohannes Berg * when we set the TIM bit and the station notices it, but 1347ce662b44SJohannes Berg * before it can poll for the frame we expire it. 1348ce662b44SJohannes Berg * 1349ce662b44SJohannes Berg * For uAPSD, this is said in the standard (11.2.1.5 h): 1350ce662b44SJohannes Berg * At each unscheduled SP for a non-AP STA, the AP shall 1351ce662b44SJohannes Berg * attempt to transmit at least one MSDU or MMPDU, but no 1352ce662b44SJohannes Berg * more than the value specified in the Max SP Length field 1353ce662b44SJohannes Berg * in the QoS Capability element from delivery-enabled ACs, 1354ce662b44SJohannes Berg * that are destined for the non-AP STA. 1355ce662b44SJohannes Berg * 1356ce662b44SJohannes Berg * Since we have no other MSDU/MMPDU, transmit a QoS null frame. 1357ce662b44SJohannes Berg */ 1358ce662b44SJohannes Berg 1359ce662b44SJohannes Berg /* This will evaluate to 1, 3, 5 or 7. */ 1360ce662b44SJohannes Berg tid = 7 - ((ffs(~ignored_acs) - 1) << 1); 1361ce662b44SJohannes Berg 1362b77cf4f8SJohannes Berg ieee80211_send_null_response(sdata, sta, tid, reason, true); 1363f9f760b4SJohannes Berg } else if (!driver_release_tids) { 136447086fc5SJohannes Berg struct sk_buff_head pending; 136547086fc5SJohannes Berg struct sk_buff *skb; 136640b96408SJohannes Berg int num = 0; 136740b96408SJohannes Berg u16 tids = 0; 1368b77cf4f8SJohannes Berg bool need_null = false; 136947086fc5SJohannes Berg 137047086fc5SJohannes Berg skb_queue_head_init(&pending); 137147086fc5SJohannes Berg 137247086fc5SJohannes Berg while ((skb = __skb_dequeue(&frames))) { 1373af818581SJohannes Berg struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 137447086fc5SJohannes Berg struct ieee80211_hdr *hdr = (void *) skb->data; 137540b96408SJohannes Berg u8 *qoshdr = NULL; 137640b96408SJohannes Berg 137740b96408SJohannes Berg num++; 1378af818581SJohannes Berg 1379af818581SJohannes Berg /* 138047086fc5SJohannes Berg * Tell TX path to send this frame even though the 138147086fc5SJohannes Berg * STA may still remain is PS mode after this frame 138247086fc5SJohannes Berg * exchange. 1383af818581SJohannes Berg */ 1384d6d23de2SFelix Fietkau info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER | 1385d6d23de2SFelix Fietkau IEEE80211_TX_CTL_PS_RESPONSE; 1386af818581SJohannes Berg 138747086fc5SJohannes Berg /* 138847086fc5SJohannes Berg * Use MoreData flag to indicate whether there are 138947086fc5SJohannes Berg * more buffered frames for this STA 139047086fc5SJohannes Berg */ 139124b9c373SJanusz.Dziedzic@tieto.com if (more_data || !skb_queue_empty(&frames)) 139247086fc5SJohannes Berg hdr->frame_control |= 139347086fc5SJohannes Berg cpu_to_le16(IEEE80211_FCTL_MOREDATA); 139424b9c373SJanusz.Dziedzic@tieto.com else 139524b9c373SJanusz.Dziedzic@tieto.com hdr->frame_control &= 139624b9c373SJanusz.Dziedzic@tieto.com cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 1397af818581SJohannes Berg 139840b96408SJohannes Berg if (ieee80211_is_data_qos(hdr->frame_control) || 139940b96408SJohannes Berg ieee80211_is_qos_nullfunc(hdr->frame_control)) 140040b96408SJohannes Berg qoshdr = ieee80211_get_qos_ctl(hdr); 140140b96408SJohannes Berg 1402b77cf4f8SJohannes Berg tids |= BIT(skb->priority); 1403b77cf4f8SJohannes Berg 1404b77cf4f8SJohannes Berg __skb_queue_tail(&pending, skb); 1405b77cf4f8SJohannes Berg 1406b77cf4f8SJohannes Berg /* end service period after last frame or add one */ 1407b77cf4f8SJohannes Berg if (!skb_queue_empty(&frames)) 1408b77cf4f8SJohannes Berg continue; 1409b77cf4f8SJohannes Berg 1410b77cf4f8SJohannes Berg if (reason != IEEE80211_FRAME_RELEASE_UAPSD) { 1411b77cf4f8SJohannes Berg /* for PS-Poll, there's only one frame */ 1412b77cf4f8SJohannes Berg info->flags |= IEEE80211_TX_STATUS_EOSP | 1413b77cf4f8SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 1414b77cf4f8SJohannes Berg break; 1415b77cf4f8SJohannes Berg } 1416b77cf4f8SJohannes Berg 1417b77cf4f8SJohannes Berg /* For uAPSD, things are a bit more complicated. If the 1418b77cf4f8SJohannes Berg * last frame has a QoS header (i.e. is a QoS-data or 1419b77cf4f8SJohannes Berg * QoS-nulldata frame) then just set the EOSP bit there 1420b77cf4f8SJohannes Berg * and be done. 1421b77cf4f8SJohannes Berg * If the frame doesn't have a QoS header (which means 1422b77cf4f8SJohannes Berg * it should be a bufferable MMPDU) then we can't set 1423b77cf4f8SJohannes Berg * the EOSP bit in the QoS header; add a QoS-nulldata 1424b77cf4f8SJohannes Berg * frame to the list to send it after the MMPDU. 1425b77cf4f8SJohannes Berg * 1426b77cf4f8SJohannes Berg * Note that this code is only in the mac80211-release 1427b77cf4f8SJohannes Berg * code path, we assume that the driver will not buffer 1428b77cf4f8SJohannes Berg * anything but QoS-data frames, or if it does, will 1429b77cf4f8SJohannes Berg * create the QoS-nulldata frame by itself if needed. 1430b77cf4f8SJohannes Berg * 1431b77cf4f8SJohannes Berg * Cf. 802.11-2012 10.2.1.10 (c). 1432b77cf4f8SJohannes Berg */ 1433b77cf4f8SJohannes Berg if (qoshdr) { 143440b96408SJohannes Berg *qoshdr |= IEEE80211_QOS_CTL_EOSP; 1435deeaee19SJohannes Berg 143647086fc5SJohannes Berg info->flags |= IEEE80211_TX_STATUS_EOSP | 143747086fc5SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 1438b77cf4f8SJohannes Berg } else { 1439b77cf4f8SJohannes Berg /* The standard isn't completely clear on this 1440b77cf4f8SJohannes Berg * as it says the more-data bit should be set 1441b77cf4f8SJohannes Berg * if there are more BUs. The QoS-Null frame 1442b77cf4f8SJohannes Berg * we're about to send isn't buffered yet, we 1443b77cf4f8SJohannes Berg * only create it below, but let's pretend it 1444b77cf4f8SJohannes Berg * was buffered just in case some clients only 1445b77cf4f8SJohannes Berg * expect more-data=0 when eosp=1. 1446b77cf4f8SJohannes Berg */ 1447b77cf4f8SJohannes Berg hdr->frame_control |= 1448b77cf4f8SJohannes Berg cpu_to_le16(IEEE80211_FCTL_MOREDATA); 1449b77cf4f8SJohannes Berg need_null = true; 1450b77cf4f8SJohannes Berg num++; 145152a3f20cSMarco Porsch } 1452b77cf4f8SJohannes Berg break; 145347086fc5SJohannes Berg } 145447086fc5SJohannes Berg 145540b96408SJohannes Berg drv_allow_buffered_frames(local, sta, tids, num, 145640b96408SJohannes Berg reason, more_data); 145740b96408SJohannes Berg 145847086fc5SJohannes Berg ieee80211_add_pending_skbs(local, &pending); 1459af818581SJohannes Berg 1460b77cf4f8SJohannes Berg if (need_null) 1461b77cf4f8SJohannes Berg ieee80211_send_null_response( 1462b77cf4f8SJohannes Berg sdata, sta, find_highest_prio_tid(tids), 1463b77cf4f8SJohannes Berg reason, false); 1464b77cf4f8SJohannes Berg 1465c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1466af818581SJohannes Berg } else { 1467af818581SJohannes Berg /* 14684049e09aSJohannes Berg * We need to release a frame that is buffered somewhere in the 14694049e09aSJohannes Berg * driver ... it'll have to handle that. 1470f9f760b4SJohannes Berg * Note that the driver also has to check the number of frames 1471f9f760b4SJohannes Berg * on the TIDs we're releasing from - if there are more than 1472f9f760b4SJohannes Berg * n_frames it has to set the more-data bit (if we didn't ask 1473f9f760b4SJohannes Berg * it to set it anyway due to other buffered frames); if there 1474f9f760b4SJohannes Berg * are fewer than n_frames it has to make sure to adjust that 1475f9f760b4SJohannes Berg * to allow the service period to end properly. 1476af818581SJohannes Berg */ 14774049e09aSJohannes Berg drv_release_buffered_frames(local, sta, driver_release_tids, 147847086fc5SJohannes Berg n_frames, reason, more_data); 14794049e09aSJohannes Berg 14804049e09aSJohannes Berg /* 14814049e09aSJohannes Berg * Note that we don't recalculate the TIM bit here as it would 14824049e09aSJohannes Berg * most likely have no effect at all unless the driver told us 1483f9f760b4SJohannes Berg * that the TID(s) became empty before returning here from the 14844049e09aSJohannes Berg * release function. 1485f9f760b4SJohannes Berg * Either way, however, when the driver tells us that the TID(s) 14864049e09aSJohannes Berg * became empty we'll do the TIM recalculation. 14874049e09aSJohannes Berg */ 1488af818581SJohannes Berg } 1489af818581SJohannes Berg } 1490af818581SJohannes Berg 1491af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) 1492af818581SJohannes Berg { 149347086fc5SJohannes Berg u8 ignore_for_response = sta->sta.uapsd_queues; 1494af818581SJohannes Berg 1495af818581SJohannes Berg /* 149647086fc5SJohannes Berg * If all ACs are delivery-enabled then we should reply 149747086fc5SJohannes Berg * from any of them, if only some are enabled we reply 149847086fc5SJohannes Berg * only from the non-enabled ones. 1499af818581SJohannes Berg */ 150047086fc5SJohannes Berg if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1) 150147086fc5SJohannes Berg ignore_for_response = 0; 1502af818581SJohannes Berg 150347086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response, 150447086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_PSPOLL); 1505af818581SJohannes Berg } 150647086fc5SJohannes Berg 150747086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta) 150847086fc5SJohannes Berg { 150947086fc5SJohannes Berg int n_frames = sta->sta.max_sp; 151047086fc5SJohannes Berg u8 delivery_enabled = sta->sta.uapsd_queues; 151147086fc5SJohannes Berg 151247086fc5SJohannes Berg /* 151347086fc5SJohannes Berg * If we ever grow support for TSPEC this might happen if 151447086fc5SJohannes Berg * the TSPEC update from hostapd comes in between a trigger 151547086fc5SJohannes Berg * frame setting WLAN_STA_UAPSD in the RX path and this 151647086fc5SJohannes Berg * actually getting called. 151747086fc5SJohannes Berg */ 151847086fc5SJohannes Berg if (!delivery_enabled) 151947086fc5SJohannes Berg return; 152047086fc5SJohannes Berg 152147086fc5SJohannes Berg switch (sta->sta.max_sp) { 152247086fc5SJohannes Berg case 1: 152347086fc5SJohannes Berg n_frames = 2; 152447086fc5SJohannes Berg break; 152547086fc5SJohannes Berg case 2: 152647086fc5SJohannes Berg n_frames = 4; 152747086fc5SJohannes Berg break; 152847086fc5SJohannes Berg case 3: 152947086fc5SJohannes Berg n_frames = 6; 153047086fc5SJohannes Berg break; 153147086fc5SJohannes Berg case 0: 153247086fc5SJohannes Berg /* XXX: what is a good value? */ 153347086fc5SJohannes Berg n_frames = 8; 153447086fc5SJohannes Berg break; 153547086fc5SJohannes Berg } 153647086fc5SJohannes Berg 153747086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled, 153847086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_UAPSD); 1539af818581SJohannes Berg } 1540af818581SJohannes Berg 1541af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw, 1542af818581SJohannes Berg struct ieee80211_sta *pubsta, bool block) 1543af818581SJohannes Berg { 1544af818581SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1545af818581SJohannes Berg 1546b5878a2dSJohannes Berg trace_api_sta_block_awake(sta->local, pubsta, block); 1547b5878a2dSJohannes Berg 1548af818581SJohannes Berg if (block) 1549c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_PS_DRIVER); 1550c2c98fdeSJohannes Berg else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) 1551af818581SJohannes Berg ieee80211_queue_work(hw, &sta->drv_unblock_wk); 1552af818581SJohannes Berg } 1553af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake); 1554dcf55fb5SFelix Fietkau 1555e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta) 155637fbd908SJohannes Berg { 155737fbd908SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 155837fbd908SJohannes Berg struct ieee80211_local *local = sta->local; 155937fbd908SJohannes Berg 156037fbd908SJohannes Berg trace_api_eosp(local, pubsta); 156137fbd908SJohannes Berg 156237fbd908SJohannes Berg clear_sta_flag(sta, WLAN_STA_SP); 156337fbd908SJohannes Berg } 1564e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp); 156537fbd908SJohannes Berg 1566042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta, 1567042ec453SJohannes Berg u8 tid, bool buffered) 1568dcf55fb5SFelix Fietkau { 1569dcf55fb5SFelix Fietkau struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1570dcf55fb5SFelix Fietkau 15715a306f58SJohannes Berg if (WARN_ON(tid >= IEEE80211_NUM_TIDS)) 1572042ec453SJohannes Berg return; 1573042ec453SJohannes Berg 15741b000789SJohannes Berg trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered); 15751b000789SJohannes Berg 1576948d887dSJohannes Berg if (buffered) 1577948d887dSJohannes Berg set_bit(tid, &sta->driver_buffered_tids); 1578948d887dSJohannes Berg else 1579948d887dSJohannes Berg clear_bit(tid, &sta->driver_buffered_tids); 1580948d887dSJohannes Berg 1581c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1582dcf55fb5SFelix Fietkau } 1583042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered); 1584d9a7ddb0SJohannes Berg 158583d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta, 1586d9a7ddb0SJohannes Berg enum ieee80211_sta_state new_state) 1587d9a7ddb0SJohannes Berg { 15888bf11d8dSJohannes Berg might_sleep(); 1589d9a7ddb0SJohannes Berg 1590d9a7ddb0SJohannes Berg if (sta->sta_state == new_state) 1591d9a7ddb0SJohannes Berg return 0; 1592d9a7ddb0SJohannes Berg 1593f09603a2SJohannes Berg /* check allowed transitions first */ 1594f09603a2SJohannes Berg 1595d9a7ddb0SJohannes Berg switch (new_state) { 1596d9a7ddb0SJohannes Berg case IEEE80211_STA_NONE: 1597f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_AUTH) 1598d9a7ddb0SJohannes Berg return -EINVAL; 1599d9a7ddb0SJohannes Berg break; 1600d9a7ddb0SJohannes Berg case IEEE80211_STA_AUTH: 1601f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_NONE && 1602f09603a2SJohannes Berg sta->sta_state != IEEE80211_STA_ASSOC) 1603d9a7ddb0SJohannes Berg return -EINVAL; 1604d9a7ddb0SJohannes Berg break; 1605d9a7ddb0SJohannes Berg case IEEE80211_STA_ASSOC: 1606f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_AUTH && 1607f09603a2SJohannes Berg sta->sta_state != IEEE80211_STA_AUTHORIZED) 1608d9a7ddb0SJohannes Berg return -EINVAL; 1609d9a7ddb0SJohannes Berg break; 1610d9a7ddb0SJohannes Berg case IEEE80211_STA_AUTHORIZED: 1611f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_ASSOC) 1612d9a7ddb0SJohannes Berg return -EINVAL; 1613d9a7ddb0SJohannes Berg break; 1614d9a7ddb0SJohannes Berg default: 1615d9a7ddb0SJohannes Berg WARN(1, "invalid state %d", new_state); 1616d9a7ddb0SJohannes Berg return -EINVAL; 1617d9a7ddb0SJohannes Berg } 1618d9a7ddb0SJohannes Berg 1619bdcbd8e0SJohannes Berg sta_dbg(sta->sdata, "moving STA %pM to state %d\n", 1620bdcbd8e0SJohannes Berg sta->sta.addr, new_state); 1621f09603a2SJohannes Berg 1622f09603a2SJohannes Berg /* 1623f09603a2SJohannes Berg * notify the driver before the actual changes so it can 1624f09603a2SJohannes Berg * fail the transition 1625f09603a2SJohannes Berg */ 1626f09603a2SJohannes Berg if (test_sta_flag(sta, WLAN_STA_INSERTED)) { 1627f09603a2SJohannes Berg int err = drv_sta_state(sta->local, sta->sdata, sta, 1628f09603a2SJohannes Berg sta->sta_state, new_state); 1629f09603a2SJohannes Berg if (err) 1630f09603a2SJohannes Berg return err; 1631f09603a2SJohannes Berg } 1632f09603a2SJohannes Berg 1633f09603a2SJohannes Berg /* reflect the change in all state variables */ 1634f09603a2SJohannes Berg 1635f09603a2SJohannes Berg switch (new_state) { 1636f09603a2SJohannes Berg case IEEE80211_STA_NONE: 1637f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_AUTH) 1638f09603a2SJohannes Berg clear_bit(WLAN_STA_AUTH, &sta->_flags); 1639f09603a2SJohannes Berg break; 1640f09603a2SJohannes Berg case IEEE80211_STA_AUTH: 1641f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_NONE) 1642f09603a2SJohannes Berg set_bit(WLAN_STA_AUTH, &sta->_flags); 1643f09603a2SJohannes Berg else if (sta->sta_state == IEEE80211_STA_ASSOC) 1644f09603a2SJohannes Berg clear_bit(WLAN_STA_ASSOC, &sta->_flags); 1645f09603a2SJohannes Berg break; 1646f09603a2SJohannes Berg case IEEE80211_STA_ASSOC: 1647f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_AUTH) { 1648f09603a2SJohannes Berg set_bit(WLAN_STA_ASSOC, &sta->_flags); 1649f09603a2SJohannes Berg } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) { 16507e3ed02cSFelix Fietkau if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 16517e3ed02cSFelix Fietkau (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 16527e3ed02cSFelix Fietkau !sta->sdata->u.vlan.sta)) 16537e3ed02cSFelix Fietkau atomic_dec(&sta->sdata->bss->num_mcast_sta); 1654f09603a2SJohannes Berg clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 1655f09603a2SJohannes Berg } 1656f09603a2SJohannes Berg break; 1657f09603a2SJohannes Berg case IEEE80211_STA_AUTHORIZED: 1658f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_ASSOC) { 16597e3ed02cSFelix Fietkau if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 16607e3ed02cSFelix Fietkau (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 16617e3ed02cSFelix Fietkau !sta->sdata->u.vlan.sta)) 16627e3ed02cSFelix Fietkau atomic_inc(&sta->sdata->bss->num_mcast_sta); 1663f09603a2SJohannes Berg set_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 1664f09603a2SJohannes Berg } 1665f09603a2SJohannes Berg break; 1666f09603a2SJohannes Berg default: 1667f09603a2SJohannes Berg break; 1668f09603a2SJohannes Berg } 1669f09603a2SJohannes Berg 1670d9a7ddb0SJohannes Berg sta->sta_state = new_state; 1671d9a7ddb0SJohannes Berg 1672d9a7ddb0SJohannes Berg return 0; 1673d9a7ddb0SJohannes Berg } 1674687da132SEmmanuel Grumbach 1675687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta) 1676687da132SEmmanuel Grumbach { 1677687da132SEmmanuel Grumbach struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap; 1678687da132SEmmanuel Grumbach u8 rx_streams; 1679687da132SEmmanuel Grumbach 1680687da132SEmmanuel Grumbach if (!sta->sta.ht_cap.ht_supported) 1681687da132SEmmanuel Grumbach return 1; 1682687da132SEmmanuel Grumbach 1683687da132SEmmanuel Grumbach if (sta->sta.vht_cap.vht_supported) { 1684687da132SEmmanuel Grumbach int i; 1685687da132SEmmanuel Grumbach u16 tx_mcs_map = 1686687da132SEmmanuel Grumbach le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map); 1687687da132SEmmanuel Grumbach 1688687da132SEmmanuel Grumbach for (i = 7; i >= 0; i--) 1689687da132SEmmanuel Grumbach if ((tx_mcs_map & (0x3 << (i * 2))) != 1690687da132SEmmanuel Grumbach IEEE80211_VHT_MCS_NOT_SUPPORTED) 1691687da132SEmmanuel Grumbach return i + 1; 1692687da132SEmmanuel Grumbach } 1693687da132SEmmanuel Grumbach 1694687da132SEmmanuel Grumbach if (ht_cap->mcs.rx_mask[3]) 1695687da132SEmmanuel Grumbach rx_streams = 4; 1696687da132SEmmanuel Grumbach else if (ht_cap->mcs.rx_mask[2]) 1697687da132SEmmanuel Grumbach rx_streams = 3; 1698687da132SEmmanuel Grumbach else if (ht_cap->mcs.rx_mask[1]) 1699687da132SEmmanuel Grumbach rx_streams = 2; 1700687da132SEmmanuel Grumbach else 1701687da132SEmmanuel Grumbach rx_streams = 1; 1702687da132SEmmanuel Grumbach 1703687da132SEmmanuel Grumbach if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF)) 1704687da132SEmmanuel Grumbach return rx_streams; 1705687da132SEmmanuel Grumbach 1706687da132SEmmanuel Grumbach return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK) 1707687da132SEmmanuel Grumbach >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1; 1708687da132SEmmanuel Grumbach } 1709