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 9497f97b1fSJohannes 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 } 142b22cfcfcSEliad Peller 143b22cfcfcSEliad Peller sta_info_free(local, sta); 144b22cfcfcSEliad Peller } 145b22cfcfcSEliad Peller 146d0709a65SJohannes Berg /* protected by RCU */ 147abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata, 148abe60632SJohannes Berg const u8 *addr) 14943ba7e95SJohannes Berg { 150abe60632SJohannes Berg struct ieee80211_local *local = sdata->local; 15143ba7e95SJohannes Berg struct sta_info *sta; 15243ba7e95SJohannes Berg 1530379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1540379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 15543ba7e95SJohannes Berg while (sta) { 156abe60632SJohannes Berg if (sta->sdata == sdata && 157b203ca39SJoe Perches ether_addr_equal(sta->sta.addr, addr)) 15843ba7e95SJohannes Berg break; 1590379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 1600379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 16143ba7e95SJohannes Berg } 16243ba7e95SJohannes Berg return sta; 16343ba7e95SJohannes Berg } 16443ba7e95SJohannes Berg 1650e5ded5aSFelix Fietkau /* 1660e5ded5aSFelix Fietkau * Get sta info either from the specified interface 1670e5ded5aSFelix Fietkau * or from one of its vlans 1680e5ded5aSFelix Fietkau */ 1690e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata, 1700e5ded5aSFelix Fietkau const u8 *addr) 1710e5ded5aSFelix Fietkau { 1720e5ded5aSFelix Fietkau struct ieee80211_local *local = sdata->local; 1730e5ded5aSFelix Fietkau struct sta_info *sta; 1740e5ded5aSFelix Fietkau 1750379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 1760379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 1770e5ded5aSFelix Fietkau while (sta) { 1780e5ded5aSFelix Fietkau if ((sta->sdata == sdata || 179a2c1e3daSJohannes Berg (sta->sdata->bss && sta->sdata->bss == sdata->bss)) && 180b203ca39SJoe Perches ether_addr_equal(sta->sta.addr, addr)) 1810e5ded5aSFelix Fietkau break; 1820379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 1830379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 1840e5ded5aSFelix Fietkau } 1850e5ded5aSFelix Fietkau return sta; 1860e5ded5aSFelix Fietkau } 1870e5ded5aSFelix Fietkau 1883b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata, 1893b53fde8SJohannes Berg int idx) 190ee385855SLuis Carlos Cobo { 1913b53fde8SJohannes Berg struct ieee80211_local *local = sdata->local; 192ee385855SLuis Carlos Cobo struct sta_info *sta; 193ee385855SLuis Carlos Cobo int i = 0; 194ee385855SLuis Carlos Cobo 195d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) { 1963b53fde8SJohannes Berg if (sdata != sta->sdata) 1972a8ca29aSLuis Carlos Cobo continue; 198ee385855SLuis Carlos Cobo if (i < idx) { 199ee385855SLuis Carlos Cobo ++i; 200ee385855SLuis Carlos Cobo continue; 201ee385855SLuis Carlos Cobo } 2022a8ca29aSLuis Carlos Cobo return sta; 203ee385855SLuis Carlos Cobo } 204ee385855SLuis Carlos Cobo 205ee385855SLuis Carlos Cobo return NULL; 206ee385855SLuis Carlos Cobo } 207f0706e82SJiri Benc 20893e5deb1SJohannes Berg /** 209d9a7ddb0SJohannes Berg * sta_info_free - free STA 21093e5deb1SJohannes Berg * 2116ef307bcSRandy Dunlap * @local: pointer to the global information 21293e5deb1SJohannes Berg * @sta: STA info to free 21393e5deb1SJohannes Berg * 21493e5deb1SJohannes Berg * This function must undo everything done by sta_info_alloc() 215d9a7ddb0SJohannes Berg * that may happen before sta_info_insert(). It may only be 216d9a7ddb0SJohannes Berg * called when sta_info_insert() has not been attempted (and 217d9a7ddb0SJohannes Berg * if that fails, the station is freed anyway.) 21893e5deb1SJohannes Berg */ 219d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta) 22093e5deb1SJohannes Berg { 221ad38bfc9SMatti Gottlieb int i; 222ad38bfc9SMatti Gottlieb 223889cbb91SJohannes Berg if (sta->rate_ctrl) 2244b7679a5SJohannes Berg rate_control_free_sta(sta); 22593e5deb1SJohannes Berg 226ad38bfc9SMatti Gottlieb if (sta->tx_lat) { 227ad38bfc9SMatti Gottlieb for (i = 0; i < IEEE80211_NUM_TIDS; i++) 228ad38bfc9SMatti Gottlieb kfree(sta->tx_lat[i].bins); 229ad38bfc9SMatti Gottlieb kfree(sta->tx_lat); 230ad38bfc9SMatti Gottlieb } 231ad38bfc9SMatti Gottlieb 232bdcbd8e0SJohannes Berg sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr); 23393e5deb1SJohannes Berg 23493e5deb1SJohannes Berg kfree(sta); 23593e5deb1SJohannes Berg } 23693e5deb1SJohannes Berg 2374d33960bSJohannes Berg /* Caller must hold local->sta_mtx */ 238d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local, 239d0709a65SJohannes Berg struct sta_info *sta) 240f0706e82SJiri Benc { 2414d33960bSJohannes Berg lockdep_assert_held(&local->sta_mtx); 24217741cdcSJohannes Berg sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)]; 243cf778b00SEric Dumazet rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta); 244f0706e82SJiri Benc } 245f0706e82SJiri Benc 246af818581SJohannes Berg static void sta_unblock(struct work_struct *wk) 247af818581SJohannes Berg { 248af818581SJohannes Berg struct sta_info *sta; 249af818581SJohannes Berg 250af818581SJohannes Berg sta = container_of(wk, struct sta_info, drv_unblock_wk); 251af818581SJohannes Berg 252af818581SJohannes Berg if (sta->dead) 253af818581SJohannes Berg return; 254af818581SJohannes Berg 25554420473SHelmut Schaa if (!test_sta_flag(sta, WLAN_STA_PS_STA)) { 25654420473SHelmut Schaa local_bh_disable(); 257af818581SJohannes Berg ieee80211_sta_ps_deliver_wakeup(sta); 25854420473SHelmut Schaa local_bh_enable(); 25954420473SHelmut Schaa } else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) { 260c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 261ce662b44SJohannes Berg 262ce662b44SJohannes Berg local_bh_disable(); 263af818581SJohannes Berg ieee80211_sta_ps_deliver_poll_response(sta); 264ce662b44SJohannes Berg local_bh_enable(); 265c2c98fdeSJohannes Berg } else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) { 266c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 267ce662b44SJohannes Berg 268ce662b44SJohannes Berg local_bh_disable(); 26947086fc5SJohannes Berg ieee80211_sta_ps_deliver_uapsd(sta); 270ce662b44SJohannes Berg local_bh_enable(); 27150a9432dSJohannes Berg } else 272c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 273af818581SJohannes Berg } 274af818581SJohannes Berg 275af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local, 276af65cd96SJohannes Berg struct sta_info *sta, gfp_t gfp) 277af65cd96SJohannes Berg { 278af65cd96SJohannes Berg if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) 279af65cd96SJohannes Berg return 0; 280af65cd96SJohannes Berg 281889cbb91SJohannes Berg sta->rate_ctrl = local->rate_ctrl; 282af65cd96SJohannes Berg sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, 283af65cd96SJohannes Berg &sta->sta, gfp); 284889cbb91SJohannes Berg if (!sta->rate_ctrl_priv) 285af65cd96SJohannes Berg return -ENOMEM; 286af65cd96SJohannes Berg 287af65cd96SJohannes Berg return 0; 288af65cd96SJohannes Berg } 289af65cd96SJohannes Berg 29073651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, 29156544160SJohannes Berg const u8 *addr, gfp_t gfp) 292f0706e82SJiri Benc { 293d0709a65SJohannes Berg struct ieee80211_local *local = sdata->local; 294f0706e82SJiri Benc struct sta_info *sta; 295ebe27c91SMohammed Shafi Shajakhan struct timespec uptime; 296ad38bfc9SMatti Gottlieb struct ieee80211_tx_latency_bin_ranges *tx_latency; 29716c5f15cSRon Rindjunsky int i; 298f0706e82SJiri Benc 29917741cdcSJohannes Berg sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp); 300f0706e82SJiri Benc if (!sta) 30173651ee6SJohannes Berg return NULL; 302f0706e82SJiri Benc 303ef04a297SJohannes Berg rcu_read_lock(); 304ef04a297SJohannes Berg tx_latency = rcu_dereference(local->tx_latency); 305ef04a297SJohannes Berg /* init stations Tx latency statistics && TID bins */ 306ef04a297SJohannes Berg if (tx_latency) { 307ef04a297SJohannes Berg sta->tx_lat = kzalloc(IEEE80211_NUM_TIDS * 308ef04a297SJohannes Berg sizeof(struct ieee80211_tx_latency_stat), 309ef04a297SJohannes Berg GFP_ATOMIC); 310ef04a297SJohannes Berg if (!sta->tx_lat) { 311ef04a297SJohannes Berg rcu_read_unlock(); 312ef04a297SJohannes Berg goto free; 313ef04a297SJohannes Berg } 314ef04a297SJohannes Berg 315ef04a297SJohannes Berg if (tx_latency->n_ranges) { 316ef04a297SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 317ef04a297SJohannes Berg /* size of bins is size of the ranges +1 */ 318ef04a297SJohannes Berg sta->tx_lat[i].bin_count = 319ef04a297SJohannes Berg tx_latency->n_ranges + 1; 320ef04a297SJohannes Berg sta->tx_lat[i].bins = 321ef04a297SJohannes Berg kcalloc(sta->tx_lat[i].bin_count, 322ef04a297SJohannes Berg sizeof(u32), GFP_ATOMIC); 323ef04a297SJohannes Berg if (!sta->tx_lat[i].bins) { 324ef04a297SJohannes Berg rcu_read_unlock(); 325ef04a297SJohannes Berg goto free; 326ef04a297SJohannes Berg } 327ef04a297SJohannes Berg } 328ef04a297SJohannes Berg } 329ef04a297SJohannes Berg } 330ef04a297SJohannes Berg rcu_read_unlock(); 331ef04a297SJohannes Berg 33207346f81SJohannes Berg spin_lock_init(&sta->lock); 333af818581SJohannes Berg INIT_WORK(&sta->drv_unblock_wk, sta_unblock); 33467c282c0SJohannes Berg INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work); 335a93e3644SJohannes Berg mutex_init(&sta->ampdu_mlme.mtx); 33687f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH 33787f59c70SThomas Pedersen if (ieee80211_vif_is_mesh(&sdata->vif) && 33887f59c70SThomas Pedersen !sdata->u.mesh.user_mpm) 33987f59c70SThomas Pedersen init_timer(&sta->plink_timer); 3406c7c4cbfSThomas Pedersen sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE; 34187f59c70SThomas Pedersen #endif 34207346f81SJohannes Berg 34317741cdcSJohannes Berg memcpy(sta->sta.addr, addr, ETH_ALEN); 344d0709a65SJohannes Berg sta->local = local; 345d0709a65SJohannes Berg sta->sdata = sdata; 3468bc8aecdSFelix Fietkau sta->last_rx = jiffies; 347f0706e82SJiri Benc 34871ec375cSJohannes Berg sta->sta_state = IEEE80211_STA_NONE; 34971ec375cSJohannes Berg 350ebe27c91SMohammed Shafi Shajakhan do_posix_clock_monotonic_gettime(&uptime); 351ebe27c91SMohammed Shafi Shajakhan sta->last_connected = uptime.tv_sec; 352541a45a1SBruno Randolf ewma_init(&sta->avg_signal, 1024, 8); 353ef0621e8SFelix Fietkau for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++) 354ef0621e8SFelix Fietkau ewma_init(&sta->chain_signal_avg[i], 1024, 8); 355541a45a1SBruno Randolf 356ef04a297SJohannes Berg if (sta_prepare_rate_control(local, sta, gfp)) 357ef04a297SJohannes Berg goto free; 358f0706e82SJiri Benc 3595a306f58SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 360a622ab72SJohannes Berg /* 361a622ab72SJohannes Berg * timer_to_tid must be initialized with identity mapping 362a622ab72SJohannes Berg * to enable session_timer's data differentiation. See 363a622ab72SJohannes Berg * sta_rx_agg_session_timer_expired for usage. 364a622ab72SJohannes Berg */ 36516c5f15cSRon Rindjunsky sta->timer_to_tid[i] = i; 36616c5f15cSRon Rindjunsky } 367948d887dSJohannes Berg for (i = 0; i < IEEE80211_NUM_ACS; i++) { 368948d887dSJohannes Berg skb_queue_head_init(&sta->ps_tx_buf[i]); 369948d887dSJohannes Berg skb_queue_head_init(&sta->tx_filtered[i]); 370948d887dSJohannes Berg } 37173651ee6SJohannes Berg 3725a306f58SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) 3734be929beSAlexey Dobriyan sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX); 374cccaec98SSenthil Balasubramanian 375af0ed69bSJohannes Berg sta->sta.smps_mode = IEEE80211_SMPS_OFF; 376687da132SEmmanuel Grumbach if (sdata->vif.type == NL80211_IFTYPE_AP || 377687da132SEmmanuel Grumbach sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 378687da132SEmmanuel Grumbach struct ieee80211_supported_band *sband = 379687da132SEmmanuel Grumbach local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)]; 380687da132SEmmanuel Grumbach u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 381687da132SEmmanuel Grumbach IEEE80211_HT_CAP_SM_PS_SHIFT; 382687da132SEmmanuel Grumbach /* 383687da132SEmmanuel Grumbach * Assume that hostapd advertises our caps in the beacon and 384687da132SEmmanuel Grumbach * this is the known_smps_mode for a station that just assciated 385687da132SEmmanuel Grumbach */ 386687da132SEmmanuel Grumbach switch (smps) { 387687da132SEmmanuel Grumbach case WLAN_HT_SMPS_CONTROL_DISABLED: 388687da132SEmmanuel Grumbach sta->known_smps_mode = IEEE80211_SMPS_OFF; 389687da132SEmmanuel Grumbach break; 390687da132SEmmanuel Grumbach case WLAN_HT_SMPS_CONTROL_STATIC: 391687da132SEmmanuel Grumbach sta->known_smps_mode = IEEE80211_SMPS_STATIC; 392687da132SEmmanuel Grumbach break; 393687da132SEmmanuel Grumbach case WLAN_HT_SMPS_CONTROL_DYNAMIC: 394687da132SEmmanuel Grumbach sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC; 395687da132SEmmanuel Grumbach break; 396687da132SEmmanuel Grumbach default: 397687da132SEmmanuel Grumbach WARN_ON(1); 398687da132SEmmanuel Grumbach } 399687da132SEmmanuel Grumbach } 400af0ed69bSJohannes Berg 401bdcbd8e0SJohannes Berg sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr); 40273651ee6SJohannes Berg return sta; 403ef04a297SJohannes Berg 404ef04a297SJohannes Berg free: 405ef04a297SJohannes Berg if (sta->tx_lat) { 406ef04a297SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) 407ef04a297SJohannes Berg kfree(sta->tx_lat[i].bins); 408ef04a297SJohannes Berg kfree(sta->tx_lat); 409ef04a297SJohannes Berg } 410ef04a297SJohannes Berg kfree(sta); 411ef04a297SJohannes Berg return NULL; 41273651ee6SJohannes Berg } 41373651ee6SJohannes Berg 4148c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta) 41534e89507SJohannes Berg { 41634e89507SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 41734e89507SJohannes Berg 41803e4497eSJohannes Berg /* 41903e4497eSJohannes Berg * Can't be a WARN_ON because it can be triggered through a race: 42003e4497eSJohannes Berg * something inserts a STA (on one CPU) without holding the RTNL 42103e4497eSJohannes Berg * and another CPU turns off the net device. 42203e4497eSJohannes Berg */ 4238c71df7aSGuy Eilam if (unlikely(!ieee80211_sdata_running(sdata))) 4248c71df7aSGuy Eilam return -ENETDOWN; 42503e4497eSJohannes Berg 426b203ca39SJoe Perches if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) || 4278c71df7aSGuy Eilam is_multicast_ether_addr(sta->sta.addr))) 4288c71df7aSGuy Eilam return -EINVAL; 4298c71df7aSGuy Eilam 4308c71df7aSGuy Eilam return 0; 43193e5deb1SJohannes Berg } 43244213b5eSJohannes Berg 433f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local, 434f09603a2SJohannes Berg struct ieee80211_sub_if_data *sdata, 435f09603a2SJohannes Berg struct sta_info *sta) 436f09603a2SJohannes Berg { 437f09603a2SJohannes Berg enum ieee80211_sta_state state; 438f09603a2SJohannes Berg int err = 0; 439f09603a2SJohannes Berg 440f09603a2SJohannes Berg for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) { 441f09603a2SJohannes Berg err = drv_sta_state(local, sdata, sta, state, state + 1); 442f09603a2SJohannes Berg if (err) 443f09603a2SJohannes Berg break; 444f09603a2SJohannes Berg } 445f09603a2SJohannes Berg 446f09603a2SJohannes Berg if (!err) { 447a4ec45a4SJohannes Berg /* 448a4ec45a4SJohannes Berg * Drivers using legacy sta_add/sta_remove callbacks only 449a4ec45a4SJohannes Berg * get uploaded set to true after sta_add is called. 450a4ec45a4SJohannes Berg */ 451a4ec45a4SJohannes Berg if (!local->ops->sta_add) 452f09603a2SJohannes Berg sta->uploaded = true; 453f09603a2SJohannes Berg return 0; 454f09603a2SJohannes Berg } 455f09603a2SJohannes Berg 456f09603a2SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { 457bdcbd8e0SJohannes Berg sdata_info(sdata, 458bdcbd8e0SJohannes Berg "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n", 459bdcbd8e0SJohannes Berg sta->sta.addr, state + 1, err); 460f09603a2SJohannes Berg err = 0; 461f09603a2SJohannes Berg } 462f09603a2SJohannes Berg 463f09603a2SJohannes Berg /* unwind on error */ 464f09603a2SJohannes Berg for (; state > IEEE80211_STA_NOTEXIST; state--) 465f09603a2SJohannes Berg WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1)); 466f09603a2SJohannes Berg 467f09603a2SJohannes Berg return err; 468f09603a2SJohannes Berg } 469f09603a2SJohannes Berg 47034e89507SJohannes Berg /* 4718c71df7aSGuy Eilam * should be called with sta_mtx locked 4728c71df7aSGuy Eilam * this function replaces the mutex lock 4738c71df7aSGuy Eilam * with a RCU lock 4748c71df7aSGuy Eilam */ 4754d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU) 4768c71df7aSGuy Eilam { 4778c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 4788c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 4797852e361SJohannes Berg struct station_info sinfo; 4808c71df7aSGuy Eilam int err = 0; 4818c71df7aSGuy Eilam 4828c71df7aSGuy Eilam lockdep_assert_held(&local->sta_mtx); 4838c71df7aSGuy Eilam 4847852e361SJohannes Berg /* check if STA exists already */ 4857852e361SJohannes Berg if (sta_info_get_bss(sdata, sta->sta.addr)) { 4864d33960bSJohannes Berg err = -EEXIST; 4874d33960bSJohannes Berg goto out_err; 48834e89507SJohannes Berg } 48934e89507SJohannes Berg 4904d33960bSJohannes Berg /* notify driver */ 491f09603a2SJohannes Berg err = sta_info_insert_drv_state(local, sdata, sta); 492f09603a2SJohannes Berg if (err) 493f09603a2SJohannes Berg goto out_err; 4944d33960bSJohannes Berg 4954d33960bSJohannes Berg local->num_sta++; 4964d33960bSJohannes Berg local->sta_generation++; 4974d33960bSJohannes Berg smp_mb(); 4984d33960bSJohannes Berg 4994d33960bSJohannes Berg /* make the station visible */ 5004d33960bSJohannes Berg sta_info_hash_add(local, sta); 5014d33960bSJohannes Berg 502794454ceSArik Nemtsov list_add_rcu(&sta->list, &local->sta_list); 50383d5cc01SJohannes Berg 50483d5cc01SJohannes Berg set_sta_flag(sta, WLAN_STA_INSERTED); 5054d33960bSJohannes Berg 50621f659bfSEliad Peller ieee80211_recalc_min_chandef(sdata); 5074d33960bSJohannes Berg ieee80211_sta_debugfs_add(sta); 5084d33960bSJohannes Berg rate_control_add_sta_debugfs(sta); 5094d33960bSJohannes Berg 5104d33960bSJohannes Berg memset(&sinfo, 0, sizeof(sinfo)); 5114d33960bSJohannes Berg sinfo.filled = 0; 5124d33960bSJohannes Berg sinfo.generation = local->sta_generation; 5134d33960bSJohannes Berg cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL); 514d0709a65SJohannes Berg 515bdcbd8e0SJohannes Berg sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr); 516f0706e82SJiri Benc 51734e89507SJohannes Berg /* move reference to rcu-protected */ 51834e89507SJohannes Berg rcu_read_lock(); 51934e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 520e9f207f0SJiri Benc 52173651ee6SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 52273651ee6SJohannes Berg mesh_accept_plinks_update(sdata); 52373651ee6SJohannes Berg 52473651ee6SJohannes Berg return 0; 5254d33960bSJohannes Berg out_err: 5264d33960bSJohannes Berg mutex_unlock(&local->sta_mtx); 5274d33960bSJohannes Berg rcu_read_lock(); 5284d33960bSJohannes Berg return err; 5298c71df7aSGuy Eilam } 5308c71df7aSGuy Eilam 5318c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) 5328c71df7aSGuy Eilam { 5338c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 5348c71df7aSGuy Eilam int err = 0; 5358c71df7aSGuy Eilam 5364d33960bSJohannes Berg might_sleep(); 5374d33960bSJohannes Berg 5388c71df7aSGuy Eilam err = sta_info_insert_check(sta); 5398c71df7aSGuy Eilam if (err) { 5408c71df7aSGuy Eilam rcu_read_lock(); 5418c71df7aSGuy Eilam goto out_free; 5428c71df7aSGuy Eilam } 5438c71df7aSGuy Eilam 544004c872eSJohannes Berg mutex_lock(&local->sta_mtx); 545004c872eSJohannes Berg 5464d33960bSJohannes Berg err = sta_info_insert_finish(sta); 5478c71df7aSGuy Eilam if (err) 548004c872eSJohannes Berg goto out_free; 549d0709a65SJohannes Berg 550004c872eSJohannes Berg return 0; 55193e5deb1SJohannes Berg out_free: 55293e5deb1SJohannes Berg BUG_ON(!err); 553d9a7ddb0SJohannes Berg sta_info_free(local, sta); 55493e5deb1SJohannes Berg return err; 555f0706e82SJiri Benc } 556f0706e82SJiri Benc 55734e89507SJohannes Berg int sta_info_insert(struct sta_info *sta) 55834e89507SJohannes Berg { 55934e89507SJohannes Berg int err = sta_info_insert_rcu(sta); 56034e89507SJohannes Berg 56134e89507SJohannes Berg rcu_read_unlock(); 56234e89507SJohannes Berg 56334e89507SJohannes Berg return err; 56434e89507SJohannes Berg } 56534e89507SJohannes Berg 566d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id) 567004c872eSJohannes Berg { 568004c872eSJohannes Berg /* 569004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 570004c872eSJohannes Berg * so this line may not be changed to use the __set_bit() format. 571004c872eSJohannes Berg */ 572d012a605SMarco Porsch tim[id / 8] |= (1 << (id % 8)); 573004c872eSJohannes Berg } 574004c872eSJohannes Berg 575d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id) 576004c872eSJohannes Berg { 577004c872eSJohannes Berg /* 578004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 579004c872eSJohannes Berg * so this line may not be changed to use the __clear_bit() format. 580004c872eSJohannes Berg */ 581d012a605SMarco Porsch tim[id / 8] &= ~(1 << (id % 8)); 582004c872eSJohannes Berg } 583004c872eSJohannes Berg 5843d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id) 5853d5839b6SIlan Peer { 5863d5839b6SIlan Peer /* 5873d5839b6SIlan Peer * This format has been mandated by the IEEE specifications, 5883d5839b6SIlan Peer * so this line may not be changed to use the test_bit() format. 5893d5839b6SIlan Peer */ 5903d5839b6SIlan Peer return tim[id / 8] & (1 << (id % 8)); 5913d5839b6SIlan Peer } 5923d5839b6SIlan Peer 593948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac) 594004c872eSJohannes Berg { 595948d887dSJohannes Berg /* If we ever support TIDs > 7, this obviously needs to be adjusted */ 596948d887dSJohannes Berg switch (ac) { 597948d887dSJohannes Berg case IEEE80211_AC_VO: 598948d887dSJohannes Berg return BIT(6) | BIT(7); 599948d887dSJohannes Berg case IEEE80211_AC_VI: 600948d887dSJohannes Berg return BIT(4) | BIT(5); 601948d887dSJohannes Berg case IEEE80211_AC_BE: 602948d887dSJohannes Berg return BIT(0) | BIT(3); 603948d887dSJohannes Berg case IEEE80211_AC_BK: 604948d887dSJohannes Berg return BIT(1) | BIT(2); 605948d887dSJohannes Berg default: 606948d887dSJohannes Berg WARN_ON(1); 607948d887dSJohannes Berg return 0; 608d0709a65SJohannes Berg } 609004c872eSJohannes Berg } 610004c872eSJohannes Berg 611c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta) 612004c872eSJohannes Berg { 613c868cb35SJohannes Berg struct ieee80211_local *local = sta->local; 614d012a605SMarco Porsch struct ps_data *ps; 615948d887dSJohannes Berg bool indicate_tim = false; 616948d887dSJohannes Berg u8 ignore_for_tim = sta->sta.uapsd_queues; 617948d887dSJohannes Berg int ac; 618d012a605SMarco Porsch u16 id; 619004c872eSJohannes Berg 620d012a605SMarco Porsch if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 621d012a605SMarco Porsch sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 622c868cb35SJohannes Berg if (WARN_ON_ONCE(!sta->sdata->bss)) 623c868cb35SJohannes Berg return; 6243e122be0SJohannes Berg 625d012a605SMarco Porsch ps = &sta->sdata->bss->ps; 626d012a605SMarco Porsch id = sta->sta.aid; 6273f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH 6283f52b7e3SMarco Porsch } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) { 6293f52b7e3SMarco Porsch ps = &sta->sdata->u.mesh.ps; 630204d1304SThomas Pedersen /* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */ 6316f101ef0SChun-Yeow Yeoh id = sta->plid % (IEEE80211_MAX_AID + 1); 6323f52b7e3SMarco Porsch #endif 633d012a605SMarco Porsch } else { 634d012a605SMarco Porsch return; 635d012a605SMarco Porsch } 636d012a605SMarco Porsch 637c868cb35SJohannes Berg /* No need to do anything if the driver does all */ 638c868cb35SJohannes Berg if (local->hw.flags & IEEE80211_HW_AP_LINK_PS) 639c868cb35SJohannes Berg return; 640004c872eSJohannes Berg 641c868cb35SJohannes Berg if (sta->dead) 642c868cb35SJohannes Berg goto done; 6433e122be0SJohannes Berg 644948d887dSJohannes Berg /* 645948d887dSJohannes Berg * If all ACs are delivery-enabled then we should build 646948d887dSJohannes Berg * the TIM bit for all ACs anyway; if only some are then 647948d887dSJohannes Berg * we ignore those and build the TIM bit using only the 648948d887dSJohannes Berg * non-enabled ones. 649948d887dSJohannes Berg */ 650948d887dSJohannes Berg if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1) 651948d887dSJohannes Berg ignore_for_tim = 0; 652948d887dSJohannes Berg 653948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 654948d887dSJohannes Berg unsigned long tids; 655948d887dSJohannes Berg 656948d887dSJohannes Berg if (ignore_for_tim & BIT(ac)) 657948d887dSJohannes Berg continue; 658948d887dSJohannes Berg 659948d887dSJohannes Berg indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) || 660948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac]); 661948d887dSJohannes Berg if (indicate_tim) 662948d887dSJohannes Berg break; 663948d887dSJohannes Berg 664948d887dSJohannes Berg tids = ieee80211_tids_for_ac(ac); 665948d887dSJohannes Berg 666948d887dSJohannes Berg indicate_tim |= 667948d887dSJohannes Berg sta->driver_buffered_tids & tids; 668004c872eSJohannes Berg } 669004c872eSJohannes Berg 670c868cb35SJohannes Berg done: 67165f704a5SJohannes Berg spin_lock_bh(&local->tim_lock); 672004c872eSJohannes Berg 6733d5839b6SIlan Peer if (indicate_tim == __bss_tim_get(ps->tim, id)) 6743d5839b6SIlan Peer goto out_unlock; 6753d5839b6SIlan Peer 676948d887dSJohannes Berg if (indicate_tim) 677d012a605SMarco Porsch __bss_tim_set(ps->tim, id); 678c868cb35SJohannes Berg else 679d012a605SMarco Porsch __bss_tim_clear(ps->tim, id); 6803e122be0SJohannes Berg 681c868cb35SJohannes Berg if (local->ops->set_tim) { 682c868cb35SJohannes Berg local->tim_in_locked_section = true; 683948d887dSJohannes Berg drv_set_tim(local, &sta->sta, indicate_tim); 684c868cb35SJohannes Berg local->tim_in_locked_section = false; 685004c872eSJohannes Berg } 686004c872eSJohannes Berg 6873d5839b6SIlan Peer out_unlock: 68865f704a5SJohannes Berg spin_unlock_bh(&local->tim_lock); 689004c872eSJohannes Berg } 690004c872eSJohannes Berg 691cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb) 692f0706e82SJiri Benc { 693e039fa4aSJohannes Berg struct ieee80211_tx_info *info; 694f0706e82SJiri Benc int timeout; 695f0706e82SJiri Benc 696f0706e82SJiri Benc if (!skb) 697cd0b8d89SJohannes Berg return false; 698f0706e82SJiri Benc 699e039fa4aSJohannes Berg info = IEEE80211_SKB_CB(skb); 700f0706e82SJiri Benc 701f0706e82SJiri Benc /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ 70257c4d7b4SJohannes Berg timeout = (sta->listen_interval * 70357c4d7b4SJohannes Berg sta->sdata->vif.bss_conf.beacon_int * 70457c4d7b4SJohannes Berg 32 / 15625) * HZ; 705f0706e82SJiri Benc if (timeout < STA_TX_BUFFER_EXPIRE) 706f0706e82SJiri Benc timeout = STA_TX_BUFFER_EXPIRE; 707e039fa4aSJohannes Berg return time_after(jiffies, info->control.jiffies + timeout); 708f0706e82SJiri Benc } 709f0706e82SJiri Benc 710f0706e82SJiri Benc 711948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local, 712948d887dSJohannes Berg struct sta_info *sta, int ac) 713f0706e82SJiri Benc { 714f0706e82SJiri Benc unsigned long flags; 715f0706e82SJiri Benc struct sk_buff *skb; 716f0706e82SJiri Benc 71760750397SJohannes Berg /* 71860750397SJohannes Berg * First check for frames that should expire on the filtered 71960750397SJohannes Berg * queue. Frames here were rejected by the driver and are on 72060750397SJohannes Berg * a separate queue to avoid reordering with normal PS-buffered 72160750397SJohannes Berg * frames. They also aren't accounted for right now in the 72260750397SJohannes Berg * total_ps_buffered counter. 72360750397SJohannes Berg */ 724f0706e82SJiri Benc for (;;) { 725948d887dSJohannes Berg spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 726948d887dSJohannes Berg skb = skb_peek(&sta->tx_filtered[ac]); 72757c4d7b4SJohannes Berg if (sta_info_buffer_expired(sta, skb)) 728948d887dSJohannes Berg skb = __skb_dequeue(&sta->tx_filtered[ac]); 729836341a7SJohannes Berg else 730f0706e82SJiri Benc skb = NULL; 731948d887dSJohannes Berg spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 732f0706e82SJiri Benc 73360750397SJohannes Berg /* 73460750397SJohannes Berg * Frames are queued in order, so if this one 73560750397SJohannes Berg * hasn't expired yet we can stop testing. If 73660750397SJohannes Berg * we actually reached the end of the queue we 73760750397SJohannes Berg * also need to stop, of course. 73860750397SJohannes Berg */ 73960750397SJohannes Berg if (!skb) 74060750397SJohannes Berg break; 741d4fa14cdSFelix Fietkau ieee80211_free_txskb(&local->hw, skb); 74260750397SJohannes Berg } 74360750397SJohannes Berg 74460750397SJohannes Berg /* 74560750397SJohannes Berg * Now also check the normal PS-buffered queue, this will 74660750397SJohannes Berg * only find something if the filtered queue was emptied 74760750397SJohannes Berg * since the filtered frames are all before the normal PS 74860750397SJohannes Berg * buffered frames. 74960750397SJohannes Berg */ 750f0706e82SJiri Benc for (;;) { 751948d887dSJohannes Berg spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 752948d887dSJohannes Berg skb = skb_peek(&sta->ps_tx_buf[ac]); 753f0706e82SJiri Benc if (sta_info_buffer_expired(sta, skb)) 754948d887dSJohannes Berg skb = __skb_dequeue(&sta->ps_tx_buf[ac]); 755f0706e82SJiri Benc else 756f0706e82SJiri Benc skb = NULL; 757948d887dSJohannes Berg spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 758f0706e82SJiri Benc 75960750397SJohannes Berg /* 76060750397SJohannes Berg * frames are queued in order, so if this one 76160750397SJohannes Berg * hasn't expired yet (or we reached the end of 76260750397SJohannes Berg * the queue) we can stop testing 76360750397SJohannes Berg */ 764836341a7SJohannes Berg if (!skb) 765836341a7SJohannes Berg break; 766836341a7SJohannes Berg 767f0706e82SJiri Benc local->total_ps_buffered--; 768bdcbd8e0SJohannes Berg ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n", 769bdcbd8e0SJohannes Berg sta->sta.addr); 770d4fa14cdSFelix Fietkau ieee80211_free_txskb(&local->hw, skb); 771f0706e82SJiri Benc } 7723393a608SJuuso Oikarinen 77360750397SJohannes Berg /* 77460750397SJohannes Berg * Finally, recalculate the TIM bit for this station -- it might 77560750397SJohannes Berg * now be clear because the station was too slow to retrieve its 77660750397SJohannes Berg * frames. 77760750397SJohannes Berg */ 77860750397SJohannes Berg sta_info_recalc_tim(sta); 77960750397SJohannes Berg 78060750397SJohannes Berg /* 78160750397SJohannes Berg * Return whether there are any frames still buffered, this is 78260750397SJohannes Berg * used to check whether the cleanup timer still needs to run, 78360750397SJohannes Berg * if there are no frames we don't need to rearm the timer. 78460750397SJohannes Berg */ 785948d887dSJohannes Berg return !(skb_queue_empty(&sta->ps_tx_buf[ac]) && 786948d887dSJohannes Berg skb_queue_empty(&sta->tx_filtered[ac])); 787948d887dSJohannes Berg } 788948d887dSJohannes Berg 789948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local, 790948d887dSJohannes Berg struct sta_info *sta) 791948d887dSJohannes Berg { 792948d887dSJohannes Berg bool have_buffered = false; 793948d887dSJohannes Berg int ac; 794948d887dSJohannes Berg 7953f52b7e3SMarco Porsch /* This is only necessary for stations on BSS/MBSS interfaces */ 7963f52b7e3SMarco Porsch if (!sta->sdata->bss && 7973f52b7e3SMarco Porsch !ieee80211_vif_is_mesh(&sta->sdata->vif)) 798948d887dSJohannes Berg return false; 799948d887dSJohannes Berg 800948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 801948d887dSJohannes Berg have_buffered |= 802948d887dSJohannes Berg sta_info_cleanup_expire_buffered_ac(local, sta, ac); 803948d887dSJohannes Berg 804948d887dSJohannes Berg return have_buffered; 805f0706e82SJiri Benc } 806f0706e82SJiri Benc 807d778207bSJohannes Berg static int __must_check __sta_info_destroy_part1(struct sta_info *sta) 80834e89507SJohannes Berg { 80934e89507SJohannes Berg struct ieee80211_local *local; 81034e89507SJohannes Berg struct ieee80211_sub_if_data *sdata; 8116d10e46bSJohannes Berg int ret; 81234e89507SJohannes Berg 81334e89507SJohannes Berg might_sleep(); 81434e89507SJohannes Berg 81534e89507SJohannes Berg if (!sta) 81634e89507SJohannes Berg return -ENOENT; 81734e89507SJohannes Berg 81834e89507SJohannes Berg local = sta->local; 81934e89507SJohannes Berg sdata = sta->sdata; 82034e89507SJohannes Berg 82183d5cc01SJohannes Berg lockdep_assert_held(&local->sta_mtx); 82283d5cc01SJohannes Berg 823098a6070SJohannes Berg /* 824098a6070SJohannes Berg * Before removing the station from the driver and 825098a6070SJohannes Berg * rate control, it might still start new aggregation 826098a6070SJohannes Berg * sessions -- block that to make sure the tear-down 827098a6070SJohannes Berg * will be sufficient. 828098a6070SJohannes Berg */ 829c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_BLOCK_BA); 830c82c4a80SJohannes Berg ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA); 831098a6070SJohannes Berg 83234e89507SJohannes Berg ret = sta_info_hash_del(local, sta); 833b01711beSJohannes Berg if (WARN_ON(ret)) 83434e89507SJohannes Berg return ret; 83534e89507SJohannes Berg 836794454ceSArik Nemtsov list_del_rcu(&sta->list); 8374d33960bSJohannes Berg 8386a9d1b91SJohannes Berg drv_sta_pre_rcu_remove(local, sta->sdata, sta); 8396a9d1b91SJohannes Berg 840a710c816SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 841a710c816SJohannes Berg rcu_access_pointer(sdata->u.vlan.sta) == sta) 842a710c816SJohannes Berg RCU_INIT_POINTER(sdata->u.vlan.sta, NULL); 843a710c816SJohannes Berg 844d778207bSJohannes Berg return 0; 845d778207bSJohannes Berg } 846d778207bSJohannes Berg 847d778207bSJohannes Berg static void __sta_info_destroy_part2(struct sta_info *sta) 848d778207bSJohannes Berg { 849d778207bSJohannes Berg struct ieee80211_local *local = sta->local; 850d778207bSJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 851d778207bSJohannes Berg int ret; 852d778207bSJohannes Berg 853d778207bSJohannes Berg /* 854d778207bSJohannes Berg * NOTE: This assumes at least synchronize_net() was done 855d778207bSJohannes Berg * after _part1 and before _part2! 856d778207bSJohannes Berg */ 857d778207bSJohannes Berg 858d778207bSJohannes Berg might_sleep(); 859d778207bSJohannes Berg lockdep_assert_held(&local->sta_mtx); 860d778207bSJohannes Berg 861c8782078SJohannes Berg /* now keys can no longer be reached */ 8626d10e46bSJohannes Berg ieee80211_free_sta_keys(local, sta); 86334e89507SJohannes Berg 86434e89507SJohannes Berg sta->dead = true; 86534e89507SJohannes Berg 86634e89507SJohannes Berg local->num_sta--; 86734e89507SJohannes Berg local->sta_generation++; 86834e89507SJohannes Berg 86983d5cc01SJohannes Berg while (sta->sta_state > IEEE80211_STA_NONE) { 870f09603a2SJohannes Berg ret = sta_info_move_state(sta, sta->sta_state - 1); 871f09603a2SJohannes Berg if (ret) { 87283d5cc01SJohannes Berg WARN_ON_ONCE(1); 87383d5cc01SJohannes Berg break; 87483d5cc01SJohannes Berg } 87583d5cc01SJohannes Berg } 876d9a7ddb0SJohannes Berg 877f09603a2SJohannes Berg if (sta->uploaded) { 878f09603a2SJohannes Berg ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE, 879f09603a2SJohannes Berg IEEE80211_STA_NOTEXIST); 880f09603a2SJohannes Berg WARN_ON_ONCE(ret != 0); 881f09603a2SJohannes Berg } 88234e89507SJohannes Berg 883bdcbd8e0SJohannes Berg sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr); 884bdcbd8e0SJohannes Berg 885ec15e68bSJouni Malinen cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL); 886ec15e68bSJouni Malinen 88734e89507SJohannes Berg rate_control_remove_sta_debugfs(sta); 88834e89507SJohannes Berg ieee80211_sta_debugfs_remove(sta); 88921f659bfSEliad Peller ieee80211_recalc_min_chandef(sdata); 89034e89507SJohannes Berg 891d34ba216SJohannes Berg cleanup_single_sta(sta); 892d778207bSJohannes Berg } 893d778207bSJohannes Berg 894d778207bSJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta) 895d778207bSJohannes Berg { 896d778207bSJohannes Berg int err = __sta_info_destroy_part1(sta); 897d778207bSJohannes Berg 898d778207bSJohannes Berg if (err) 899d778207bSJohannes Berg return err; 900d778207bSJohannes Berg 901d778207bSJohannes Berg synchronize_net(); 902d778207bSJohannes Berg 903d778207bSJohannes Berg __sta_info_destroy_part2(sta); 90434e89507SJohannes Berg 90534e89507SJohannes Berg return 0; 90634e89507SJohannes Berg } 90734e89507SJohannes Berg 90834e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr) 90934e89507SJohannes Berg { 91034e89507SJohannes Berg struct sta_info *sta; 91134e89507SJohannes Berg int ret; 91234e89507SJohannes Berg 91334e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 9147852e361SJohannes Berg sta = sta_info_get(sdata, addr); 91534e89507SJohannes Berg ret = __sta_info_destroy(sta); 91634e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 91734e89507SJohannes Berg 91834e89507SJohannes Berg return ret; 91934e89507SJohannes Berg } 92034e89507SJohannes Berg 92134e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata, 92234e89507SJohannes Berg const u8 *addr) 92334e89507SJohannes Berg { 92434e89507SJohannes Berg struct sta_info *sta; 92534e89507SJohannes Berg int ret; 92634e89507SJohannes Berg 92734e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 9287852e361SJohannes Berg sta = sta_info_get_bss(sdata, addr); 92934e89507SJohannes Berg ret = __sta_info_destroy(sta); 93034e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 93134e89507SJohannes Berg 93234e89507SJohannes Berg return ret; 93334e89507SJohannes Berg } 934f0706e82SJiri Benc 935f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data) 936f0706e82SJiri Benc { 937f0706e82SJiri Benc struct ieee80211_local *local = (struct ieee80211_local *) data; 938f0706e82SJiri Benc struct sta_info *sta; 9393393a608SJuuso Oikarinen bool timer_needed = false; 940f0706e82SJiri Benc 941d0709a65SJohannes Berg rcu_read_lock(); 942d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) 9433393a608SJuuso Oikarinen if (sta_info_cleanup_expire_buffered(local, sta)) 9443393a608SJuuso Oikarinen timer_needed = true; 945d0709a65SJohannes Berg rcu_read_unlock(); 946f0706e82SJiri Benc 9475bb644a0SJohannes Berg if (local->quiescing) 9485bb644a0SJohannes Berg return; 9495bb644a0SJohannes Berg 9503393a608SJuuso Oikarinen if (!timer_needed) 9513393a608SJuuso Oikarinen return; 9523393a608SJuuso Oikarinen 95326d59535SJohannes Berg mod_timer(&local->sta_cleanup, 95426d59535SJohannes Berg round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); 955f0706e82SJiri Benc } 956f0706e82SJiri Benc 957f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local) 958f0706e82SJiri Benc { 9594d33960bSJohannes Berg spin_lock_init(&local->tim_lock); 96034e89507SJohannes Berg mutex_init(&local->sta_mtx); 961f0706e82SJiri Benc INIT_LIST_HEAD(&local->sta_list); 962f0706e82SJiri Benc 963b24b8a24SPavel Emelyanov setup_timer(&local->sta_cleanup, sta_info_cleanup, 964b24b8a24SPavel Emelyanov (unsigned long)local); 965f0706e82SJiri Benc } 966f0706e82SJiri Benc 967f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local) 968f0706e82SJiri Benc { 969a56f992cSJohannes Berg del_timer_sync(&local->sta_cleanup); 970f0706e82SJiri Benc } 971f0706e82SJiri Benc 972051007d9SJohannes Berg 973e716251dSJohannes Berg int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans) 974f0706e82SJiri Benc { 975b998e8bbSJohannes Berg struct ieee80211_local *local = sdata->local; 976f0706e82SJiri Benc struct sta_info *sta, *tmp; 977d778207bSJohannes Berg LIST_HEAD(free_list); 97844213b5eSJohannes Berg int ret = 0; 979f0706e82SJiri Benc 980d0709a65SJohannes Berg might_sleep(); 981d0709a65SJohannes Berg 982e716251dSJohannes Berg WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP); 983e716251dSJohannes Berg WARN_ON(vlans && !sdata->bss); 984e716251dSJohannes Berg 98534e89507SJohannes Berg mutex_lock(&local->sta_mtx); 98634e89507SJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 987e716251dSJohannes Berg if (sdata == sta->sdata || 988e716251dSJohannes Berg (vlans && sdata->bss == sta->sdata->bss)) { 989d778207bSJohannes Berg if (!WARN_ON(__sta_info_destroy_part1(sta))) 990d778207bSJohannes Berg list_add(&sta->free_list, &free_list); 99134316837SJohannes Berg ret++; 99234316837SJohannes Berg } 99334e89507SJohannes Berg } 994d778207bSJohannes Berg 995d778207bSJohannes Berg if (!list_empty(&free_list)) { 996d778207bSJohannes Berg synchronize_net(); 997d778207bSJohannes Berg list_for_each_entry_safe(sta, tmp, &free_list, free_list) 998d778207bSJohannes Berg __sta_info_destroy_part2(sta); 999d778207bSJohannes Berg } 100034e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 100144213b5eSJohannes Berg 1002051007d9SJohannes Berg return ret; 1003051007d9SJohannes Berg } 1004051007d9SJohannes Berg 100524723d1bSJohannes Berg void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, 100624723d1bSJohannes Berg unsigned long exp_time) 100724723d1bSJohannes Berg { 100824723d1bSJohannes Berg struct ieee80211_local *local = sdata->local; 100924723d1bSJohannes Berg struct sta_info *sta, *tmp; 101024723d1bSJohannes Berg 101134e89507SJohannes Berg mutex_lock(&local->sta_mtx); 1012e46a2cf9SMohammed Shafi Shajakhan 1013e46a2cf9SMohammed Shafi Shajakhan list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 1014ec2b774eSMarek Lindner if (sdata != sta->sdata) 1015ec2b774eSMarek Lindner continue; 1016ec2b774eSMarek Lindner 101724723d1bSJohannes Berg if (time_after(jiffies, sta->last_rx + exp_time)) { 1018eea57d42SMohammed Shafi Shajakhan sta_dbg(sta->sdata, "expiring inactive STA %pM\n", 1019bdcbd8e0SJohannes Berg sta->sta.addr); 10203f52b7e3SMarco Porsch 10213f52b7e3SMarco Porsch if (ieee80211_vif_is_mesh(&sdata->vif) && 10223f52b7e3SMarco Porsch test_sta_flag(sta, WLAN_STA_PS_STA)) 10233f52b7e3SMarco Porsch atomic_dec(&sdata->u.mesh.ps.num_sta_ps); 10243f52b7e3SMarco Porsch 102534e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 102624723d1bSJohannes Berg } 1027e46a2cf9SMohammed Shafi Shajakhan } 1028e46a2cf9SMohammed Shafi Shajakhan 102934e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 103024723d1bSJohannes Berg } 103117741cdcSJohannes Berg 1032686b9cb9SBen Greear struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, 1033686b9cb9SBen Greear const u8 *addr, 1034686b9cb9SBen Greear const u8 *localaddr) 103517741cdcSJohannes Berg { 1036abe60632SJohannes Berg struct sta_info *sta, *nxt; 103717741cdcSJohannes Berg 1038686b9cb9SBen Greear /* 1039686b9cb9SBen Greear * Just return a random station if localaddr is NULL 1040686b9cb9SBen Greear * ... first in list. 1041686b9cb9SBen Greear */ 1042f7c65594SJohannes Berg for_each_sta_info(hw_to_local(hw), addr, sta, nxt) { 1043686b9cb9SBen Greear if (localaddr && 1044b203ca39SJoe Perches !ether_addr_equal(sta->sdata->vif.addr, localaddr)) 1045686b9cb9SBen Greear continue; 1046f7c65594SJohannes Berg if (!sta->uploaded) 1047f7c65594SJohannes Berg return NULL; 104817741cdcSJohannes Berg return &sta->sta; 1049f7c65594SJohannes Berg } 1050f7c65594SJohannes Berg 1051abe60632SJohannes Berg return NULL; 105217741cdcSJohannes Berg } 1053686b9cb9SBen Greear EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr); 10545ed176e1SJohannes Berg 10555ed176e1SJohannes Berg struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif, 10565ed176e1SJohannes Berg const u8 *addr) 10575ed176e1SJohannes Berg { 1058f7c65594SJohannes Berg struct sta_info *sta; 10595ed176e1SJohannes Berg 10605ed176e1SJohannes Berg if (!vif) 10615ed176e1SJohannes Berg return NULL; 10625ed176e1SJohannes Berg 1063f7c65594SJohannes Berg sta = sta_info_get_bss(vif_to_sdata(vif), addr); 1064f7c65594SJohannes Berg if (!sta) 1065f7c65594SJohannes Berg return NULL; 10665ed176e1SJohannes Berg 1067f7c65594SJohannes Berg if (!sta->uploaded) 1068f7c65594SJohannes Berg return NULL; 1069f7c65594SJohannes Berg 1070f7c65594SJohannes Berg return &sta->sta; 10715ed176e1SJohannes Berg } 107217741cdcSJohannes Berg EXPORT_SYMBOL(ieee80211_find_sta); 1073af818581SJohannes Berg 107450a9432dSJohannes Berg static void clear_sta_ps_flags(void *_sta) 107550a9432dSJohannes Berg { 107650a9432dSJohannes Berg struct sta_info *sta = _sta; 1077608383bfSHelmut Schaa struct ieee80211_sub_if_data *sdata = sta->sdata; 1078d012a605SMarco Porsch struct ps_data *ps; 1079d012a605SMarco Porsch 1080d012a605SMarco Porsch if (sdata->vif.type == NL80211_IFTYPE_AP || 1081d012a605SMarco Porsch sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 1082d012a605SMarco Porsch ps = &sdata->bss->ps; 10833f52b7e3SMarco Porsch else if (ieee80211_vif_is_mesh(&sdata->vif)) 10843f52b7e3SMarco Porsch ps = &sdata->u.mesh.ps; 1085d012a605SMarco Porsch else 1086d012a605SMarco Porsch return; 108750a9432dSJohannes Berg 1088c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 1089608383bfSHelmut Schaa if (test_and_clear_sta_flag(sta, WLAN_STA_PS_STA)) 1090d012a605SMarco Porsch atomic_dec(&ps->num_sta_ps); 109150a9432dSJohannes Berg } 109250a9432dSJohannes Berg 1093af818581SJohannes Berg /* powersave support code */ 1094af818581SJohannes Berg void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) 1095af818581SJohannes Berg { 1096af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1097af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 1098948d887dSJohannes Berg struct sk_buff_head pending; 1099948d887dSJohannes Berg int filtered = 0, buffered = 0, ac; 1100987c285cSArik Nemtsov unsigned long flags; 1101af818581SJohannes Berg 1102c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_SP); 110347086fc5SJohannes Berg 11045a306f58SJohannes Berg BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1); 1105948d887dSJohannes Berg sta->driver_buffered_tids = 0; 1106948d887dSJohannes Berg 1107d057e5a3SArik Nemtsov if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS)) 110812375ef9SJohannes Berg drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); 1109af818581SJohannes Berg 1110948d887dSJohannes Berg skb_queue_head_init(&pending); 1111af818581SJohannes Berg 1112af818581SJohannes Berg /* Send all buffered frames to the station */ 1113948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1114948d887dSJohannes Berg int count = skb_queue_len(&pending), tmp; 1115948d887dSJohannes Berg 1116987c285cSArik Nemtsov spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 1117948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending); 1118987c285cSArik Nemtsov spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 1119948d887dSJohannes Berg tmp = skb_queue_len(&pending); 1120948d887dSJohannes Berg filtered += tmp - count; 1121948d887dSJohannes Berg count = tmp; 1122948d887dSJohannes Berg 1123987c285cSArik Nemtsov spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 1124948d887dSJohannes Berg skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending); 1125987c285cSArik Nemtsov spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 1126948d887dSJohannes Berg tmp = skb_queue_len(&pending); 1127948d887dSJohannes Berg buffered += tmp - count; 1128948d887dSJohannes Berg } 1129948d887dSJohannes Berg 1130948d887dSJohannes Berg ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta); 1131948d887dSJohannes Berg 1132687da132SEmmanuel Grumbach /* This station just woke up and isn't aware of our SMPS state */ 1133687da132SEmmanuel Grumbach if (!ieee80211_smps_is_restrictive(sta->known_smps_mode, 1134687da132SEmmanuel Grumbach sdata->smps_mode) && 1135687da132SEmmanuel Grumbach sta->known_smps_mode != sdata->bss->req_smps && 1136687da132SEmmanuel Grumbach sta_info_tx_streams(sta) != 1) { 1137687da132SEmmanuel Grumbach ht_dbg(sdata, 1138687da132SEmmanuel Grumbach "%pM just woke up and MIMO capable - update SMPS\n", 1139687da132SEmmanuel Grumbach sta->sta.addr); 1140687da132SEmmanuel Grumbach ieee80211_send_smps_action(sdata, sdata->bss->req_smps, 1141687da132SEmmanuel Grumbach sta->sta.addr, 1142687da132SEmmanuel Grumbach sdata->vif.bss_conf.bssid); 1143687da132SEmmanuel Grumbach } 1144687da132SEmmanuel Grumbach 1145af818581SJohannes Berg local->total_ps_buffered -= buffered; 1146af818581SJohannes Berg 1147c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1148c868cb35SJohannes Berg 1149bdcbd8e0SJohannes Berg ps_dbg(sdata, 1150bdcbd8e0SJohannes Berg "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n", 1151bdcbd8e0SJohannes Berg sta->sta.addr, sta->sta.aid, filtered, buffered); 1152af818581SJohannes Berg } 1153af818581SJohannes Berg 1154ce662b44SJohannes Berg static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata, 1155ce662b44SJohannes Berg struct sta_info *sta, int tid, 115640b96408SJohannes Berg enum ieee80211_frame_release_type reason) 1157ce662b44SJohannes Berg { 1158ce662b44SJohannes Berg struct ieee80211_local *local = sdata->local; 1159ce662b44SJohannes Berg struct ieee80211_qos_hdr *nullfunc; 1160ce662b44SJohannes Berg struct sk_buff *skb; 1161ce662b44SJohannes Berg int size = sizeof(*nullfunc); 1162ce662b44SJohannes Berg __le16 fc; 1163c2c98fdeSJohannes Berg bool qos = test_sta_flag(sta, WLAN_STA_WME); 1164ce662b44SJohannes Berg struct ieee80211_tx_info *info; 116555de908aSJohannes Berg struct ieee80211_chanctx_conf *chanctx_conf; 1166ce662b44SJohannes Berg 1167ce662b44SJohannes Berg if (qos) { 1168ce662b44SJohannes Berg fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1169ce662b44SJohannes Berg IEEE80211_STYPE_QOS_NULLFUNC | 1170ce662b44SJohannes Berg IEEE80211_FCTL_FROMDS); 1171ce662b44SJohannes Berg } else { 1172ce662b44SJohannes Berg size -= 2; 1173ce662b44SJohannes Berg fc = cpu_to_le16(IEEE80211_FTYPE_DATA | 1174ce662b44SJohannes Berg IEEE80211_STYPE_NULLFUNC | 1175ce662b44SJohannes Berg IEEE80211_FCTL_FROMDS); 1176ce662b44SJohannes Berg } 1177ce662b44SJohannes Berg 1178ce662b44SJohannes Berg skb = dev_alloc_skb(local->hw.extra_tx_headroom + size); 1179ce662b44SJohannes Berg if (!skb) 1180ce662b44SJohannes Berg return; 1181ce662b44SJohannes Berg 1182ce662b44SJohannes Berg skb_reserve(skb, local->hw.extra_tx_headroom); 1183ce662b44SJohannes Berg 1184ce662b44SJohannes Berg nullfunc = (void *) skb_put(skb, size); 1185ce662b44SJohannes Berg nullfunc->frame_control = fc; 1186ce662b44SJohannes Berg nullfunc->duration_id = 0; 1187ce662b44SJohannes Berg memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN); 1188ce662b44SJohannes Berg memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN); 1189ce662b44SJohannes Berg memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN); 1190ce662b44SJohannes Berg 1191ce662b44SJohannes Berg skb->priority = tid; 1192ce662b44SJohannes Berg skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]); 119359b66255SJohannes Berg if (qos) { 1194ce662b44SJohannes Berg nullfunc->qos_ctrl = cpu_to_le16(tid); 1195ce662b44SJohannes Berg 119640b96408SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_UAPSD) 1197ce662b44SJohannes Berg nullfunc->qos_ctrl |= 1198ce662b44SJohannes Berg cpu_to_le16(IEEE80211_QOS_CTL_EOSP); 1199ce662b44SJohannes Berg } 1200ce662b44SJohannes Berg 1201ce662b44SJohannes Berg info = IEEE80211_SKB_CB(skb); 1202ce662b44SJohannes Berg 1203ce662b44SJohannes Berg /* 1204ce662b44SJohannes Berg * Tell TX path to send this frame even though the 1205ce662b44SJohannes Berg * STA may still remain is PS mode after this frame 1206deeaee19SJohannes Berg * exchange. Also set EOSP to indicate this packet 1207deeaee19SJohannes Berg * ends the poll/service period. 1208ce662b44SJohannes Berg */ 120902f2f1a9SJohannes Berg info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER | 1210d6d23de2SFelix Fietkau IEEE80211_TX_CTL_PS_RESPONSE | 1211deeaee19SJohannes Berg IEEE80211_TX_STATUS_EOSP | 1212ce662b44SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 1213ce662b44SJohannes Berg 121440b96408SJohannes Berg drv_allow_buffered_frames(local, sta, BIT(tid), 1, reason, false); 121540b96408SJohannes Berg 121689afe614SJohannes Berg skb->dev = sdata->dev; 121789afe614SJohannes Berg 121855de908aSJohannes Berg rcu_read_lock(); 121955de908aSJohannes Berg chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf); 122055de908aSJohannes Berg if (WARN_ON(!chanctx_conf)) { 122155de908aSJohannes Berg rcu_read_unlock(); 122255de908aSJohannes Berg kfree_skb(skb); 122355de908aSJohannes Berg return; 122455de908aSJohannes Berg } 122555de908aSJohannes Berg 12264bf88530SJohannes Berg ieee80211_xmit(sdata, skb, chanctx_conf->def.chan->band); 122755de908aSJohannes Berg rcu_read_unlock(); 1228ce662b44SJohannes Berg } 1229ce662b44SJohannes Berg 1230*0a1cb809SJohannes Berg static int find_highest_prio_tid(unsigned long tids) 1231*0a1cb809SJohannes Berg { 1232*0a1cb809SJohannes Berg /* lower 3 TIDs aren't ordered perfectly */ 1233*0a1cb809SJohannes Berg if (tids & 0xF8) 1234*0a1cb809SJohannes Berg return fls(tids) - 1; 1235*0a1cb809SJohannes Berg /* TID 0 is BE just like TID 3 */ 1236*0a1cb809SJohannes Berg if (tids & BIT(0)) 1237*0a1cb809SJohannes Berg return 0; 1238*0a1cb809SJohannes Berg return fls(tids) - 1; 1239*0a1cb809SJohannes Berg } 1240*0a1cb809SJohannes Berg 124147086fc5SJohannes Berg static void 124247086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta, 124347086fc5SJohannes Berg int n_frames, u8 ignored_acs, 124447086fc5SJohannes Berg enum ieee80211_frame_release_type reason) 1245af818581SJohannes Berg { 1246af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1247af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 12484049e09aSJohannes Berg bool found = false; 1249948d887dSJohannes Berg bool more_data = false; 1250948d887dSJohannes Berg int ac; 12514049e09aSJohannes Berg unsigned long driver_release_tids = 0; 125247086fc5SJohannes Berg struct sk_buff_head frames; 125347086fc5SJohannes Berg 1254deeaee19SJohannes Berg /* Service or PS-Poll period starts */ 1255c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_SP); 1256deeaee19SJohannes Berg 125747086fc5SJohannes Berg __skb_queue_head_init(&frames); 1258af818581SJohannes Berg 1259948d887dSJohannes Berg /* 126047086fc5SJohannes Berg * Get response frame(s) and more data bit for it. 1261948d887dSJohannes Berg */ 1262948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 12634049e09aSJohannes Berg unsigned long tids; 12644049e09aSJohannes Berg 126547086fc5SJohannes Berg if (ignored_acs & BIT(ac)) 1266948d887dSJohannes Berg continue; 1267948d887dSJohannes Berg 12684049e09aSJohannes Berg tids = ieee80211_tids_for_ac(ac); 12694049e09aSJohannes Berg 12704049e09aSJohannes Berg if (!found) { 12714049e09aSJohannes Berg driver_release_tids = sta->driver_buffered_tids & tids; 12724049e09aSJohannes Berg if (driver_release_tids) { 12734049e09aSJohannes Berg found = true; 12744049e09aSJohannes Berg } else { 127547086fc5SJohannes Berg struct sk_buff *skb; 127647086fc5SJohannes Berg 127747086fc5SJohannes Berg while (n_frames > 0) { 1278948d887dSJohannes Berg skb = skb_dequeue(&sta->tx_filtered[ac]); 1279948d887dSJohannes Berg if (!skb) { 128047086fc5SJohannes Berg skb = skb_dequeue( 128147086fc5SJohannes Berg &sta->ps_tx_buf[ac]); 1282af818581SJohannes Berg if (skb) 1283af818581SJohannes Berg local->total_ps_buffered--; 1284af818581SJohannes Berg } 128547086fc5SJohannes Berg if (!skb) 128647086fc5SJohannes Berg break; 128747086fc5SJohannes Berg n_frames--; 12884049e09aSJohannes Berg found = true; 128947086fc5SJohannes Berg __skb_queue_tail(&frames, skb); 129047086fc5SJohannes Berg } 1291948d887dSJohannes Berg } 1292948d887dSJohannes Berg 12934049e09aSJohannes Berg /* 12944049e09aSJohannes Berg * If the driver has data on more than one TID then 12954049e09aSJohannes Berg * certainly there's more data if we release just a 12964049e09aSJohannes Berg * single frame now (from a single TID). 12974049e09aSJohannes Berg */ 129847086fc5SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_PSPOLL && 129947086fc5SJohannes Berg hweight16(driver_release_tids) > 1) { 13004049e09aSJohannes Berg more_data = true; 13014049e09aSJohannes Berg driver_release_tids = 1302*0a1cb809SJohannes Berg BIT(find_highest_prio_tid( 1303*0a1cb809SJohannes Berg driver_release_tids)); 13044049e09aSJohannes Berg break; 13054049e09aSJohannes Berg } 13064049e09aSJohannes Berg } 1307948d887dSJohannes Berg 1308948d887dSJohannes Berg if (!skb_queue_empty(&sta->tx_filtered[ac]) || 1309948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac])) { 1310948d887dSJohannes Berg more_data = true; 1311948d887dSJohannes Berg break; 1312948d887dSJohannes Berg } 1313948d887dSJohannes Berg } 1314af818581SJohannes Berg 13154049e09aSJohannes Berg if (!found) { 1316ce662b44SJohannes Berg int tid; 13174049e09aSJohannes Berg 1318ce662b44SJohannes Berg /* 1319ce662b44SJohannes Berg * For PS-Poll, this can only happen due to a race condition 1320ce662b44SJohannes Berg * when we set the TIM bit and the station notices it, but 1321ce662b44SJohannes Berg * before it can poll for the frame we expire it. 1322ce662b44SJohannes Berg * 1323ce662b44SJohannes Berg * For uAPSD, this is said in the standard (11.2.1.5 h): 1324ce662b44SJohannes Berg * At each unscheduled SP for a non-AP STA, the AP shall 1325ce662b44SJohannes Berg * attempt to transmit at least one MSDU or MMPDU, but no 1326ce662b44SJohannes Berg * more than the value specified in the Max SP Length field 1327ce662b44SJohannes Berg * in the QoS Capability element from delivery-enabled ACs, 1328ce662b44SJohannes Berg * that are destined for the non-AP STA. 1329ce662b44SJohannes Berg * 1330ce662b44SJohannes Berg * Since we have no other MSDU/MMPDU, transmit a QoS null frame. 1331ce662b44SJohannes Berg */ 1332ce662b44SJohannes Berg 1333ce662b44SJohannes Berg /* This will evaluate to 1, 3, 5 or 7. */ 1334ce662b44SJohannes Berg tid = 7 - ((ffs(~ignored_acs) - 1) << 1); 1335ce662b44SJohannes Berg 133640b96408SJohannes Berg ieee80211_send_null_response(sdata, sta, tid, reason); 13374049e09aSJohannes Berg return; 13384049e09aSJohannes Berg } 13394049e09aSJohannes Berg 134047086fc5SJohannes Berg if (!driver_release_tids) { 134147086fc5SJohannes Berg struct sk_buff_head pending; 134247086fc5SJohannes Berg struct sk_buff *skb; 134340b96408SJohannes Berg int num = 0; 134440b96408SJohannes Berg u16 tids = 0; 134547086fc5SJohannes Berg 134647086fc5SJohannes Berg skb_queue_head_init(&pending); 134747086fc5SJohannes Berg 134847086fc5SJohannes Berg while ((skb = __skb_dequeue(&frames))) { 1349af818581SJohannes Berg struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 135047086fc5SJohannes Berg struct ieee80211_hdr *hdr = (void *) skb->data; 135140b96408SJohannes Berg u8 *qoshdr = NULL; 135240b96408SJohannes Berg 135340b96408SJohannes Berg num++; 1354af818581SJohannes Berg 1355af818581SJohannes Berg /* 135647086fc5SJohannes Berg * Tell TX path to send this frame even though the 135747086fc5SJohannes Berg * STA may still remain is PS mode after this frame 135847086fc5SJohannes Berg * exchange. 1359af818581SJohannes Berg */ 1360d6d23de2SFelix Fietkau info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER | 1361d6d23de2SFelix Fietkau IEEE80211_TX_CTL_PS_RESPONSE; 1362af818581SJohannes Berg 136347086fc5SJohannes Berg /* 136447086fc5SJohannes Berg * Use MoreData flag to indicate whether there are 136547086fc5SJohannes Berg * more buffered frames for this STA 136647086fc5SJohannes Berg */ 136724b9c373SJanusz.Dziedzic@tieto.com if (more_data || !skb_queue_empty(&frames)) 136847086fc5SJohannes Berg hdr->frame_control |= 136947086fc5SJohannes Berg cpu_to_le16(IEEE80211_FCTL_MOREDATA); 137024b9c373SJanusz.Dziedzic@tieto.com else 137124b9c373SJanusz.Dziedzic@tieto.com hdr->frame_control &= 137224b9c373SJanusz.Dziedzic@tieto.com cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 1373af818581SJohannes Berg 137440b96408SJohannes Berg if (ieee80211_is_data_qos(hdr->frame_control) || 137540b96408SJohannes Berg ieee80211_is_qos_nullfunc(hdr->frame_control)) 137640b96408SJohannes Berg qoshdr = ieee80211_get_qos_ctl(hdr); 137740b96408SJohannes Berg 137852a3f20cSMarco Porsch /* end service period after last frame */ 137952a3f20cSMarco Porsch if (skb_queue_empty(&frames)) { 138040b96408SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_UAPSD && 138152a3f20cSMarco Porsch qoshdr) 138240b96408SJohannes Berg *qoshdr |= IEEE80211_QOS_CTL_EOSP; 1383deeaee19SJohannes Berg 138447086fc5SJohannes Berg info->flags |= IEEE80211_TX_STATUS_EOSP | 138547086fc5SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 138652a3f20cSMarco Porsch } 138747086fc5SJohannes Berg 138840b96408SJohannes Berg if (qoshdr) 138940b96408SJohannes Berg tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK); 139040b96408SJohannes Berg else 139140b96408SJohannes Berg tids |= BIT(0); 139240b96408SJohannes Berg 139347086fc5SJohannes Berg __skb_queue_tail(&pending, skb); 139447086fc5SJohannes Berg } 139547086fc5SJohannes Berg 139640b96408SJohannes Berg drv_allow_buffered_frames(local, sta, tids, num, 139740b96408SJohannes Berg reason, more_data); 139840b96408SJohannes Berg 139947086fc5SJohannes Berg ieee80211_add_pending_skbs(local, &pending); 1400af818581SJohannes Berg 1401c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1402af818581SJohannes Berg } else { 1403af818581SJohannes Berg /* 14044049e09aSJohannes Berg * We need to release a frame that is buffered somewhere in the 14054049e09aSJohannes Berg * driver ... it'll have to handle that. 14064049e09aSJohannes Berg * Note that, as per the comment above, it'll also have to see 14074049e09aSJohannes Berg * if there is more than just one frame on the specific TID that 14084049e09aSJohannes Berg * we're releasing from, and it needs to set the more-data bit 14094049e09aSJohannes Berg * accordingly if we tell it that there's no more data. If we do 14104049e09aSJohannes Berg * tell it there's more data, then of course the more-data bit 14114049e09aSJohannes Berg * needs to be set anyway. 1412af818581SJohannes Berg */ 14134049e09aSJohannes Berg drv_release_buffered_frames(local, sta, driver_release_tids, 141447086fc5SJohannes Berg n_frames, reason, more_data); 14154049e09aSJohannes Berg 14164049e09aSJohannes Berg /* 14174049e09aSJohannes Berg * Note that we don't recalculate the TIM bit here as it would 14184049e09aSJohannes Berg * most likely have no effect at all unless the driver told us 14194049e09aSJohannes Berg * that the TID became empty before returning here from the 14204049e09aSJohannes Berg * release function. 14214049e09aSJohannes Berg * Either way, however, when the driver tells us that the TID 14224049e09aSJohannes Berg * became empty we'll do the TIM recalculation. 14234049e09aSJohannes Berg */ 1424af818581SJohannes Berg } 1425af818581SJohannes Berg } 1426af818581SJohannes Berg 1427af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) 1428af818581SJohannes Berg { 142947086fc5SJohannes Berg u8 ignore_for_response = sta->sta.uapsd_queues; 1430af818581SJohannes Berg 1431af818581SJohannes Berg /* 143247086fc5SJohannes Berg * If all ACs are delivery-enabled then we should reply 143347086fc5SJohannes Berg * from any of them, if only some are enabled we reply 143447086fc5SJohannes Berg * only from the non-enabled ones. 1435af818581SJohannes Berg */ 143647086fc5SJohannes Berg if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1) 143747086fc5SJohannes Berg ignore_for_response = 0; 1438af818581SJohannes Berg 143947086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response, 144047086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_PSPOLL); 1441af818581SJohannes Berg } 144247086fc5SJohannes Berg 144347086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta) 144447086fc5SJohannes Berg { 144547086fc5SJohannes Berg int n_frames = sta->sta.max_sp; 144647086fc5SJohannes Berg u8 delivery_enabled = sta->sta.uapsd_queues; 144747086fc5SJohannes Berg 144847086fc5SJohannes Berg /* 144947086fc5SJohannes Berg * If we ever grow support for TSPEC this might happen if 145047086fc5SJohannes Berg * the TSPEC update from hostapd comes in between a trigger 145147086fc5SJohannes Berg * frame setting WLAN_STA_UAPSD in the RX path and this 145247086fc5SJohannes Berg * actually getting called. 145347086fc5SJohannes Berg */ 145447086fc5SJohannes Berg if (!delivery_enabled) 145547086fc5SJohannes Berg return; 145647086fc5SJohannes Berg 145747086fc5SJohannes Berg switch (sta->sta.max_sp) { 145847086fc5SJohannes Berg case 1: 145947086fc5SJohannes Berg n_frames = 2; 146047086fc5SJohannes Berg break; 146147086fc5SJohannes Berg case 2: 146247086fc5SJohannes Berg n_frames = 4; 146347086fc5SJohannes Berg break; 146447086fc5SJohannes Berg case 3: 146547086fc5SJohannes Berg n_frames = 6; 146647086fc5SJohannes Berg break; 146747086fc5SJohannes Berg case 0: 146847086fc5SJohannes Berg /* XXX: what is a good value? */ 146947086fc5SJohannes Berg n_frames = 8; 147047086fc5SJohannes Berg break; 147147086fc5SJohannes Berg } 147247086fc5SJohannes Berg 147347086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled, 147447086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_UAPSD); 1475af818581SJohannes Berg } 1476af818581SJohannes Berg 1477af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw, 1478af818581SJohannes Berg struct ieee80211_sta *pubsta, bool block) 1479af818581SJohannes Berg { 1480af818581SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1481af818581SJohannes Berg 1482b5878a2dSJohannes Berg trace_api_sta_block_awake(sta->local, pubsta, block); 1483b5878a2dSJohannes Berg 1484af818581SJohannes Berg if (block) 1485c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_PS_DRIVER); 1486c2c98fdeSJohannes Berg else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) 1487af818581SJohannes Berg ieee80211_queue_work(hw, &sta->drv_unblock_wk); 1488af818581SJohannes Berg } 1489af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake); 1490dcf55fb5SFelix Fietkau 1491e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta) 149237fbd908SJohannes Berg { 149337fbd908SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 149437fbd908SJohannes Berg struct ieee80211_local *local = sta->local; 149537fbd908SJohannes Berg 149637fbd908SJohannes Berg trace_api_eosp(local, pubsta); 149737fbd908SJohannes Berg 149837fbd908SJohannes Berg clear_sta_flag(sta, WLAN_STA_SP); 149937fbd908SJohannes Berg } 1500e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp); 150137fbd908SJohannes Berg 1502042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta, 1503042ec453SJohannes Berg u8 tid, bool buffered) 1504dcf55fb5SFelix Fietkau { 1505dcf55fb5SFelix Fietkau struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1506dcf55fb5SFelix Fietkau 15075a306f58SJohannes Berg if (WARN_ON(tid >= IEEE80211_NUM_TIDS)) 1508042ec453SJohannes Berg return; 1509042ec453SJohannes Berg 15101b000789SJohannes Berg trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered); 15111b000789SJohannes Berg 1512948d887dSJohannes Berg if (buffered) 1513948d887dSJohannes Berg set_bit(tid, &sta->driver_buffered_tids); 1514948d887dSJohannes Berg else 1515948d887dSJohannes Berg clear_bit(tid, &sta->driver_buffered_tids); 1516948d887dSJohannes Berg 1517c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1518dcf55fb5SFelix Fietkau } 1519042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered); 1520d9a7ddb0SJohannes Berg 152183d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta, 1522d9a7ddb0SJohannes Berg enum ieee80211_sta_state new_state) 1523d9a7ddb0SJohannes Berg { 15248bf11d8dSJohannes Berg might_sleep(); 1525d9a7ddb0SJohannes Berg 1526d9a7ddb0SJohannes Berg if (sta->sta_state == new_state) 1527d9a7ddb0SJohannes Berg return 0; 1528d9a7ddb0SJohannes Berg 1529f09603a2SJohannes Berg /* check allowed transitions first */ 1530f09603a2SJohannes Berg 1531d9a7ddb0SJohannes Berg switch (new_state) { 1532d9a7ddb0SJohannes Berg case IEEE80211_STA_NONE: 1533f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_AUTH) 1534d9a7ddb0SJohannes Berg return -EINVAL; 1535d9a7ddb0SJohannes Berg break; 1536d9a7ddb0SJohannes Berg case IEEE80211_STA_AUTH: 1537f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_NONE && 1538f09603a2SJohannes Berg sta->sta_state != IEEE80211_STA_ASSOC) 1539d9a7ddb0SJohannes Berg return -EINVAL; 1540d9a7ddb0SJohannes Berg break; 1541d9a7ddb0SJohannes Berg case IEEE80211_STA_ASSOC: 1542f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_AUTH && 1543f09603a2SJohannes Berg sta->sta_state != IEEE80211_STA_AUTHORIZED) 1544d9a7ddb0SJohannes Berg return -EINVAL; 1545d9a7ddb0SJohannes Berg break; 1546d9a7ddb0SJohannes Berg case IEEE80211_STA_AUTHORIZED: 1547f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_ASSOC) 1548d9a7ddb0SJohannes Berg return -EINVAL; 1549d9a7ddb0SJohannes Berg break; 1550d9a7ddb0SJohannes Berg default: 1551d9a7ddb0SJohannes Berg WARN(1, "invalid state %d", new_state); 1552d9a7ddb0SJohannes Berg return -EINVAL; 1553d9a7ddb0SJohannes Berg } 1554d9a7ddb0SJohannes Berg 1555bdcbd8e0SJohannes Berg sta_dbg(sta->sdata, "moving STA %pM to state %d\n", 1556bdcbd8e0SJohannes Berg sta->sta.addr, new_state); 1557f09603a2SJohannes Berg 1558f09603a2SJohannes Berg /* 1559f09603a2SJohannes Berg * notify the driver before the actual changes so it can 1560f09603a2SJohannes Berg * fail the transition 1561f09603a2SJohannes Berg */ 1562f09603a2SJohannes Berg if (test_sta_flag(sta, WLAN_STA_INSERTED)) { 1563f09603a2SJohannes Berg int err = drv_sta_state(sta->local, sta->sdata, sta, 1564f09603a2SJohannes Berg sta->sta_state, new_state); 1565f09603a2SJohannes Berg if (err) 1566f09603a2SJohannes Berg return err; 1567f09603a2SJohannes Berg } 1568f09603a2SJohannes Berg 1569f09603a2SJohannes Berg /* reflect the change in all state variables */ 1570f09603a2SJohannes Berg 1571f09603a2SJohannes Berg switch (new_state) { 1572f09603a2SJohannes Berg case IEEE80211_STA_NONE: 1573f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_AUTH) 1574f09603a2SJohannes Berg clear_bit(WLAN_STA_AUTH, &sta->_flags); 1575f09603a2SJohannes Berg break; 1576f09603a2SJohannes Berg case IEEE80211_STA_AUTH: 1577f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_NONE) 1578f09603a2SJohannes Berg set_bit(WLAN_STA_AUTH, &sta->_flags); 1579f09603a2SJohannes Berg else if (sta->sta_state == IEEE80211_STA_ASSOC) 1580f09603a2SJohannes Berg clear_bit(WLAN_STA_ASSOC, &sta->_flags); 1581f09603a2SJohannes Berg break; 1582f09603a2SJohannes Berg case IEEE80211_STA_ASSOC: 1583f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_AUTH) { 1584f09603a2SJohannes Berg set_bit(WLAN_STA_ASSOC, &sta->_flags); 1585f09603a2SJohannes Berg } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) { 15867e3ed02cSFelix Fietkau if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 15877e3ed02cSFelix Fietkau (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 15887e3ed02cSFelix Fietkau !sta->sdata->u.vlan.sta)) 15897e3ed02cSFelix Fietkau atomic_dec(&sta->sdata->bss->num_mcast_sta); 1590f09603a2SJohannes Berg clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 1591f09603a2SJohannes Berg } 1592f09603a2SJohannes Berg break; 1593f09603a2SJohannes Berg case IEEE80211_STA_AUTHORIZED: 1594f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_ASSOC) { 15957e3ed02cSFelix Fietkau if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 15967e3ed02cSFelix Fietkau (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 15977e3ed02cSFelix Fietkau !sta->sdata->u.vlan.sta)) 15987e3ed02cSFelix Fietkau atomic_inc(&sta->sdata->bss->num_mcast_sta); 1599f09603a2SJohannes Berg set_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 1600f09603a2SJohannes Berg } 1601f09603a2SJohannes Berg break; 1602f09603a2SJohannes Berg default: 1603f09603a2SJohannes Berg break; 1604f09603a2SJohannes Berg } 1605f09603a2SJohannes Berg 1606d9a7ddb0SJohannes Berg sta->sta_state = new_state; 1607d9a7ddb0SJohannes Berg 1608d9a7ddb0SJohannes Berg return 0; 1609d9a7ddb0SJohannes Berg } 1610687da132SEmmanuel Grumbach 1611687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta) 1612687da132SEmmanuel Grumbach { 1613687da132SEmmanuel Grumbach struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap; 1614687da132SEmmanuel Grumbach u8 rx_streams; 1615687da132SEmmanuel Grumbach 1616687da132SEmmanuel Grumbach if (!sta->sta.ht_cap.ht_supported) 1617687da132SEmmanuel Grumbach return 1; 1618687da132SEmmanuel Grumbach 1619687da132SEmmanuel Grumbach if (sta->sta.vht_cap.vht_supported) { 1620687da132SEmmanuel Grumbach int i; 1621687da132SEmmanuel Grumbach u16 tx_mcs_map = 1622687da132SEmmanuel Grumbach le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map); 1623687da132SEmmanuel Grumbach 1624687da132SEmmanuel Grumbach for (i = 7; i >= 0; i--) 1625687da132SEmmanuel Grumbach if ((tx_mcs_map & (0x3 << (i * 2))) != 1626687da132SEmmanuel Grumbach IEEE80211_VHT_MCS_NOT_SUPPORTED) 1627687da132SEmmanuel Grumbach return i + 1; 1628687da132SEmmanuel Grumbach } 1629687da132SEmmanuel Grumbach 1630687da132SEmmanuel Grumbach if (ht_cap->mcs.rx_mask[3]) 1631687da132SEmmanuel Grumbach rx_streams = 4; 1632687da132SEmmanuel Grumbach else if (ht_cap->mcs.rx_mask[2]) 1633687da132SEmmanuel Grumbach rx_streams = 3; 1634687da132SEmmanuel Grumbach else if (ht_cap->mcs.rx_mask[1]) 1635687da132SEmmanuel Grumbach rx_streams = 2; 1636687da132SEmmanuel Grumbach else 1637687da132SEmmanuel Grumbach rx_streams = 1; 1638687da132SEmmanuel Grumbach 1639687da132SEmmanuel Grumbach if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF)) 1640687da132SEmmanuel Grumbach return rx_streams; 1641687da132SEmmanuel Grumbach 1642687da132SEmmanuel Grumbach return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK) 1643687da132SEmmanuel Grumbach >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1; 1644687da132SEmmanuel Grumbach } 1645