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 /* 103b22cfcfcSEliad Peller * At this point, when being called as call_rcu callback, 104b22cfcfcSEliad Peller * neither mac80211 nor the driver can reference this 105b22cfcfcSEliad Peller * sta struct any more except by still existing timers 106b22cfcfcSEliad Peller * associated with this station that we clean up below. 107051007d9SJohannes Berg * 108051007d9SJohannes Berg * Note though that this still uses the sdata and even 109051007d9SJohannes Berg * calls the driver in AP and mesh mode, so interfaces 110051007d9SJohannes Berg * of those types mush use call sta_info_flush_cleanup() 111051007d9SJohannes Berg * (typically via sta_info_flush()) before deconfiguring 112051007d9SJohannes Berg * the driver. 113051007d9SJohannes Berg * 114051007d9SJohannes Berg * In station mode, nothing happens here so it doesn't 115051007d9SJohannes Berg * have to (and doesn't) do that, this is intentional to 116051007d9SJohannes Berg * speed up roaming. 117b22cfcfcSEliad Peller */ 118b22cfcfcSEliad Peller 119b22cfcfcSEliad Peller if (test_sta_flag(sta, WLAN_STA_PS_STA)) { 120d012a605SMarco Porsch if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 121d012a605SMarco Porsch sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 122d012a605SMarco Porsch ps = &sdata->bss->ps; 1233f52b7e3SMarco Porsch else if (ieee80211_vif_is_mesh(&sdata->vif)) 1243f52b7e3SMarco Porsch ps = &sdata->u.mesh.ps; 125d012a605SMarco Porsch else 126d012a605SMarco Porsch return; 127b22cfcfcSEliad Peller 128b22cfcfcSEliad Peller clear_sta_flag(sta, WLAN_STA_PS_STA); 129b22cfcfcSEliad Peller 130d012a605SMarco Porsch atomic_dec(&ps->num_sta_ps); 131b22cfcfcSEliad Peller sta_info_recalc_tim(sta); 132b22cfcfcSEliad Peller } 133b22cfcfcSEliad Peller 134b22cfcfcSEliad Peller for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 135b22cfcfcSEliad Peller local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]); 1361f98ab7fSFelix Fietkau ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]); 1371f98ab7fSFelix Fietkau ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]); 138b22cfcfcSEliad Peller } 139b22cfcfcSEliad Peller 14045b5028eSThomas Pedersen if (ieee80211_vif_is_mesh(&sdata->vif)) 14145b5028eSThomas Pedersen mesh_sta_cleanup(sta); 142b22cfcfcSEliad Peller 143b22cfcfcSEliad Peller cancel_work_sync(&sta->drv_unblock_wk); 144b22cfcfcSEliad Peller 145b22cfcfcSEliad Peller /* 146b22cfcfcSEliad Peller * Destroy aggregation state here. It would be nice to wait for the 147b22cfcfcSEliad Peller * driver to finish aggregation stop and then clean up, but for now 148b22cfcfcSEliad Peller * drivers have to handle aggregation stop being requested, followed 149b22cfcfcSEliad Peller * directly by station destruction. 150b22cfcfcSEliad Peller */ 1515a306f58SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 152661eb381SJohannes Berg kfree(sta->ampdu_mlme.tid_start_tx[i]); 153b22cfcfcSEliad Peller tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]); 154b22cfcfcSEliad Peller if (!tid_tx) 155b22cfcfcSEliad Peller continue; 1561f98ab7fSFelix Fietkau ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending); 157b22cfcfcSEliad Peller kfree(tid_tx); 158b22cfcfcSEliad Peller } 159b22cfcfcSEliad Peller 160b22cfcfcSEliad Peller sta_info_free(local, sta); 161b22cfcfcSEliad Peller } 162b22cfcfcSEliad Peller 16397f97b1fSJohannes Berg void ieee80211_cleanup_sdata_stas(struct ieee80211_sub_if_data *sdata) 16497f97b1fSJohannes Berg { 16597f97b1fSJohannes Berg struct sta_info *sta; 16697f97b1fSJohannes Berg 16797f97b1fSJohannes Berg spin_lock_bh(&sdata->cleanup_stations_lock); 16897f97b1fSJohannes Berg while (!list_empty(&sdata->cleanup_stations)) { 16997f97b1fSJohannes Berg sta = list_first_entry(&sdata->cleanup_stations, 17097f97b1fSJohannes Berg struct sta_info, list); 17197f97b1fSJohannes Berg list_del(&sta->list); 17297f97b1fSJohannes Berg spin_unlock_bh(&sdata->cleanup_stations_lock); 17397f97b1fSJohannes Berg 17497f97b1fSJohannes Berg cleanup_single_sta(sta); 17597f97b1fSJohannes Berg 17697f97b1fSJohannes Berg spin_lock_bh(&sdata->cleanup_stations_lock); 17797f97b1fSJohannes Berg } 17897f97b1fSJohannes Berg 17997f97b1fSJohannes Berg spin_unlock_bh(&sdata->cleanup_stations_lock); 18097f97b1fSJohannes Berg } 18197f97b1fSJohannes Berg 182b22cfcfcSEliad Peller static void free_sta_rcu(struct rcu_head *h) 183b22cfcfcSEliad Peller { 184b22cfcfcSEliad Peller struct sta_info *sta = container_of(h, struct sta_info, rcu_head); 18597f97b1fSJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 186b22cfcfcSEliad Peller 18797f97b1fSJohannes Berg spin_lock(&sdata->cleanup_stations_lock); 18897f97b1fSJohannes Berg list_add_tail(&sta->list, &sdata->cleanup_stations); 18997f97b1fSJohannes Berg spin_unlock(&sdata->cleanup_stations_lock); 19097f97b1fSJohannes Berg 19197f97b1fSJohannes Berg ieee80211_queue_work(&sdata->local->hw, &sdata->cleanup_stations_wk); 192b22cfcfcSEliad Peller } 193b22cfcfcSEliad Peller 194d0709a65SJohannes Berg /* protected by RCU */ 195abe60632SJohannes Berg struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata, 196abe60632SJohannes Berg const u8 *addr) 19743ba7e95SJohannes Berg { 198abe60632SJohannes Berg struct ieee80211_local *local = sdata->local; 19943ba7e95SJohannes Berg struct sta_info *sta; 20043ba7e95SJohannes Berg 2010379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 2020379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 20343ba7e95SJohannes Berg while (sta) { 204abe60632SJohannes Berg if (sta->sdata == sdata && 205b203ca39SJoe Perches ether_addr_equal(sta->sta.addr, addr)) 20643ba7e95SJohannes Berg break; 2070379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 2080379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 20943ba7e95SJohannes Berg } 21043ba7e95SJohannes Berg return sta; 21143ba7e95SJohannes Berg } 21243ba7e95SJohannes Berg 2130e5ded5aSFelix Fietkau /* 2140e5ded5aSFelix Fietkau * Get sta info either from the specified interface 2150e5ded5aSFelix Fietkau * or from one of its vlans 2160e5ded5aSFelix Fietkau */ 2170e5ded5aSFelix Fietkau struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata, 2180e5ded5aSFelix Fietkau const u8 *addr) 2190e5ded5aSFelix Fietkau { 2200e5ded5aSFelix Fietkau struct ieee80211_local *local = sdata->local; 2210e5ded5aSFelix Fietkau struct sta_info *sta; 2220e5ded5aSFelix Fietkau 2230379185bSJohannes Berg sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 2240379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 2250e5ded5aSFelix Fietkau while (sta) { 2260e5ded5aSFelix Fietkau if ((sta->sdata == sdata || 227a2c1e3daSJohannes Berg (sta->sdata->bss && sta->sdata->bss == sdata->bss)) && 228b203ca39SJoe Perches ether_addr_equal(sta->sta.addr, addr)) 2290e5ded5aSFelix Fietkau break; 2300379185bSJohannes Berg sta = rcu_dereference_check(sta->hnext, 2310379185bSJohannes Berg lockdep_is_held(&local->sta_mtx)); 2320e5ded5aSFelix Fietkau } 2330e5ded5aSFelix Fietkau return sta; 2340e5ded5aSFelix Fietkau } 2350e5ded5aSFelix Fietkau 2363b53fde8SJohannes Berg struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata, 2373b53fde8SJohannes Berg int idx) 238ee385855SLuis Carlos Cobo { 2393b53fde8SJohannes Berg struct ieee80211_local *local = sdata->local; 240ee385855SLuis Carlos Cobo struct sta_info *sta; 241ee385855SLuis Carlos Cobo int i = 0; 242ee385855SLuis Carlos Cobo 243d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) { 2443b53fde8SJohannes Berg if (sdata != sta->sdata) 2452a8ca29aSLuis Carlos Cobo continue; 246ee385855SLuis Carlos Cobo if (i < idx) { 247ee385855SLuis Carlos Cobo ++i; 248ee385855SLuis Carlos Cobo continue; 249ee385855SLuis Carlos Cobo } 2502a8ca29aSLuis Carlos Cobo return sta; 251ee385855SLuis Carlos Cobo } 252ee385855SLuis Carlos Cobo 253ee385855SLuis Carlos Cobo return NULL; 254ee385855SLuis Carlos Cobo } 255f0706e82SJiri Benc 25693e5deb1SJohannes Berg /** 257d9a7ddb0SJohannes Berg * sta_info_free - free STA 25893e5deb1SJohannes Berg * 2596ef307bcSRandy Dunlap * @local: pointer to the global information 26093e5deb1SJohannes Berg * @sta: STA info to free 26193e5deb1SJohannes Berg * 26293e5deb1SJohannes Berg * This function must undo everything done by sta_info_alloc() 263d9a7ddb0SJohannes Berg * that may happen before sta_info_insert(). It may only be 264d9a7ddb0SJohannes Berg * called when sta_info_insert() has not been attempted (and 265d9a7ddb0SJohannes Berg * if that fails, the station is freed anyway.) 26693e5deb1SJohannes Berg */ 267d9a7ddb0SJohannes Berg void sta_info_free(struct ieee80211_local *local, struct sta_info *sta) 26893e5deb1SJohannes Berg { 269ad38bfc9SMatti Gottlieb int i; 270ad38bfc9SMatti Gottlieb 271889cbb91SJohannes Berg if (sta->rate_ctrl) 2724b7679a5SJohannes Berg rate_control_free_sta(sta); 27393e5deb1SJohannes Berg 274ad38bfc9SMatti Gottlieb if (sta->tx_lat) { 275ad38bfc9SMatti Gottlieb for (i = 0; i < IEEE80211_NUM_TIDS; i++) 276ad38bfc9SMatti Gottlieb kfree(sta->tx_lat[i].bins); 277ad38bfc9SMatti Gottlieb kfree(sta->tx_lat); 278ad38bfc9SMatti Gottlieb } 279ad38bfc9SMatti Gottlieb 280bdcbd8e0SJohannes Berg sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr); 28193e5deb1SJohannes Berg 28293e5deb1SJohannes Berg kfree(sta); 28393e5deb1SJohannes Berg } 28493e5deb1SJohannes Berg 2854d33960bSJohannes Berg /* Caller must hold local->sta_mtx */ 286d0709a65SJohannes Berg static void sta_info_hash_add(struct ieee80211_local *local, 287d0709a65SJohannes Berg struct sta_info *sta) 288f0706e82SJiri Benc { 2894d33960bSJohannes Berg lockdep_assert_held(&local->sta_mtx); 29017741cdcSJohannes Berg sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)]; 291cf778b00SEric Dumazet rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta); 292f0706e82SJiri Benc } 293f0706e82SJiri Benc 294af818581SJohannes Berg static void sta_unblock(struct work_struct *wk) 295af818581SJohannes Berg { 296af818581SJohannes Berg struct sta_info *sta; 297af818581SJohannes Berg 298af818581SJohannes Berg sta = container_of(wk, struct sta_info, drv_unblock_wk); 299af818581SJohannes Berg 300af818581SJohannes Berg if (sta->dead) 301af818581SJohannes Berg return; 302af818581SJohannes Berg 30354420473SHelmut Schaa if (!test_sta_flag(sta, WLAN_STA_PS_STA)) { 30454420473SHelmut Schaa local_bh_disable(); 305af818581SJohannes Berg ieee80211_sta_ps_deliver_wakeup(sta); 30654420473SHelmut Schaa local_bh_enable(); 30754420473SHelmut Schaa } else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) { 308c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 309ce662b44SJohannes Berg 310ce662b44SJohannes Berg local_bh_disable(); 311af818581SJohannes Berg ieee80211_sta_ps_deliver_poll_response(sta); 312ce662b44SJohannes Berg local_bh_enable(); 313c2c98fdeSJohannes Berg } else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) { 314c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 315ce662b44SJohannes Berg 316ce662b44SJohannes Berg local_bh_disable(); 31747086fc5SJohannes Berg ieee80211_sta_ps_deliver_uapsd(sta); 318ce662b44SJohannes Berg local_bh_enable(); 31950a9432dSJohannes Berg } else 320c2c98fdeSJohannes Berg clear_sta_flag(sta, WLAN_STA_PS_DRIVER); 321af818581SJohannes Berg } 322af818581SJohannes Berg 323af65cd96SJohannes Berg static int sta_prepare_rate_control(struct ieee80211_local *local, 324af65cd96SJohannes Berg struct sta_info *sta, gfp_t gfp) 325af65cd96SJohannes Berg { 326af65cd96SJohannes Berg if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) 327af65cd96SJohannes Berg return 0; 328af65cd96SJohannes Berg 329889cbb91SJohannes Berg sta->rate_ctrl = local->rate_ctrl; 330af65cd96SJohannes Berg sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, 331af65cd96SJohannes Berg &sta->sta, gfp); 332889cbb91SJohannes Berg if (!sta->rate_ctrl_priv) 333af65cd96SJohannes Berg return -ENOMEM; 334af65cd96SJohannes Berg 335af65cd96SJohannes Berg return 0; 336af65cd96SJohannes Berg } 337af65cd96SJohannes Berg 33873651ee6SJohannes Berg struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, 33956544160SJohannes Berg const u8 *addr, gfp_t gfp) 340f0706e82SJiri Benc { 341d0709a65SJohannes Berg struct ieee80211_local *local = sdata->local; 342f0706e82SJiri Benc struct sta_info *sta; 343ebe27c91SMohammed Shafi Shajakhan struct timespec uptime; 344ad38bfc9SMatti Gottlieb struct ieee80211_tx_latency_bin_ranges *tx_latency; 34516c5f15cSRon Rindjunsky int i; 346f0706e82SJiri Benc 34717741cdcSJohannes Berg sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp); 348f0706e82SJiri Benc if (!sta) 34973651ee6SJohannes Berg return NULL; 350f0706e82SJiri Benc 35107346f81SJohannes Berg spin_lock_init(&sta->lock); 352af818581SJohannes Berg INIT_WORK(&sta->drv_unblock_wk, sta_unblock); 35367c282c0SJohannes Berg INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work); 354a93e3644SJohannes Berg mutex_init(&sta->ampdu_mlme.mtx); 35587f59c70SThomas Pedersen #ifdef CONFIG_MAC80211_MESH 35687f59c70SThomas Pedersen if (ieee80211_vif_is_mesh(&sdata->vif) && 35787f59c70SThomas Pedersen !sdata->u.mesh.user_mpm) 35887f59c70SThomas Pedersen init_timer(&sta->plink_timer); 3596c7c4cbfSThomas Pedersen sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE; 36087f59c70SThomas Pedersen #endif 36107346f81SJohannes Berg 36217741cdcSJohannes Berg memcpy(sta->sta.addr, addr, ETH_ALEN); 363d0709a65SJohannes Berg sta->local = local; 364d0709a65SJohannes Berg sta->sdata = sdata; 3658bc8aecdSFelix Fietkau sta->last_rx = jiffies; 366f0706e82SJiri Benc 36771ec375cSJohannes Berg sta->sta_state = IEEE80211_STA_NONE; 36871ec375cSJohannes Berg 369ebe27c91SMohammed Shafi Shajakhan do_posix_clock_monotonic_gettime(&uptime); 370ebe27c91SMohammed Shafi Shajakhan sta->last_connected = uptime.tv_sec; 371541a45a1SBruno Randolf ewma_init(&sta->avg_signal, 1024, 8); 372ef0621e8SFelix Fietkau for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++) 373ef0621e8SFelix Fietkau ewma_init(&sta->chain_signal_avg[i], 1024, 8); 374541a45a1SBruno Randolf 375af65cd96SJohannes Berg if (sta_prepare_rate_control(local, sta, gfp)) { 376f0706e82SJiri Benc kfree(sta); 37773651ee6SJohannes Berg return NULL; 378f0706e82SJiri Benc } 379f0706e82SJiri Benc 3805a306f58SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 381a622ab72SJohannes Berg /* 382a622ab72SJohannes Berg * timer_to_tid must be initialized with identity mapping 383a622ab72SJohannes Berg * to enable session_timer's data differentiation. See 384a622ab72SJohannes Berg * sta_rx_agg_session_timer_expired for usage. 385a622ab72SJohannes Berg */ 38616c5f15cSRon Rindjunsky sta->timer_to_tid[i] = i; 38716c5f15cSRon Rindjunsky } 388948d887dSJohannes Berg for (i = 0; i < IEEE80211_NUM_ACS; i++) { 389948d887dSJohannes Berg skb_queue_head_init(&sta->ps_tx_buf[i]); 390948d887dSJohannes Berg skb_queue_head_init(&sta->tx_filtered[i]); 391948d887dSJohannes Berg } 39273651ee6SJohannes Berg 3935a306f58SJohannes Berg for (i = 0; i < IEEE80211_NUM_TIDS; i++) 3944be929beSAlexey Dobriyan sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX); 395cccaec98SSenthil Balasubramanian 396af0ed69bSJohannes Berg sta->sta.smps_mode = IEEE80211_SMPS_OFF; 397687da132SEmmanuel Grumbach if (sdata->vif.type == NL80211_IFTYPE_AP || 398687da132SEmmanuel Grumbach sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 399687da132SEmmanuel Grumbach struct ieee80211_supported_band *sband = 400687da132SEmmanuel Grumbach local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)]; 401687da132SEmmanuel Grumbach u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 402687da132SEmmanuel Grumbach IEEE80211_HT_CAP_SM_PS_SHIFT; 403687da132SEmmanuel Grumbach /* 404687da132SEmmanuel Grumbach * Assume that hostapd advertises our caps in the beacon and 405687da132SEmmanuel Grumbach * this is the known_smps_mode for a station that just assciated 406687da132SEmmanuel Grumbach */ 407687da132SEmmanuel Grumbach switch (smps) { 408687da132SEmmanuel Grumbach case WLAN_HT_SMPS_CONTROL_DISABLED: 409687da132SEmmanuel Grumbach sta->known_smps_mode = IEEE80211_SMPS_OFF; 410687da132SEmmanuel Grumbach break; 411687da132SEmmanuel Grumbach case WLAN_HT_SMPS_CONTROL_STATIC: 412687da132SEmmanuel Grumbach sta->known_smps_mode = IEEE80211_SMPS_STATIC; 413687da132SEmmanuel Grumbach break; 414687da132SEmmanuel Grumbach case WLAN_HT_SMPS_CONTROL_DYNAMIC: 415687da132SEmmanuel Grumbach sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC; 416687da132SEmmanuel Grumbach break; 417687da132SEmmanuel Grumbach default: 418687da132SEmmanuel Grumbach WARN_ON(1); 419687da132SEmmanuel Grumbach } 420687da132SEmmanuel Grumbach } 421af0ed69bSJohannes Berg 422ad38bfc9SMatti Gottlieb rcu_read_lock(); 423ad38bfc9SMatti Gottlieb 424ad38bfc9SMatti Gottlieb tx_latency = rcu_dereference(local->tx_latency); 425ad38bfc9SMatti Gottlieb /* init stations Tx latency statistics && TID bins */ 426ad38bfc9SMatti Gottlieb if (tx_latency) 427ad38bfc9SMatti Gottlieb sta->tx_lat = kzalloc(IEEE80211_NUM_TIDS * 428ad38bfc9SMatti Gottlieb sizeof(struct ieee80211_tx_latency_stat), 429ad38bfc9SMatti Gottlieb GFP_ATOMIC); 430ad38bfc9SMatti Gottlieb 431ad38bfc9SMatti Gottlieb /* 432ad38bfc9SMatti Gottlieb * if Tx latency and bins are enabled and the previous allocation 433ad38bfc9SMatti Gottlieb * succeeded 434ad38bfc9SMatti Gottlieb */ 435ad38bfc9SMatti Gottlieb if (tx_latency && tx_latency->n_ranges && sta->tx_lat) 436ad38bfc9SMatti Gottlieb for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 437ad38bfc9SMatti Gottlieb /* size of bins is size of the ranges +1 */ 438ad38bfc9SMatti Gottlieb sta->tx_lat[i].bin_count = 439ad38bfc9SMatti Gottlieb tx_latency->n_ranges + 1; 440ad38bfc9SMatti Gottlieb sta->tx_lat[i].bins = kcalloc(sta->tx_lat[i].bin_count, 441ad38bfc9SMatti Gottlieb sizeof(u32), 442ad38bfc9SMatti Gottlieb GFP_ATOMIC); 443ad38bfc9SMatti Gottlieb } 444ad38bfc9SMatti Gottlieb 445ad38bfc9SMatti Gottlieb rcu_read_unlock(); 446ad38bfc9SMatti Gottlieb 447bdcbd8e0SJohannes Berg sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr); 44873651ee6SJohannes Berg 44973651ee6SJohannes Berg return sta; 45073651ee6SJohannes Berg } 45173651ee6SJohannes Berg 4528c71df7aSGuy Eilam static int sta_info_insert_check(struct sta_info *sta) 45334e89507SJohannes Berg { 45434e89507SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 45534e89507SJohannes Berg 45603e4497eSJohannes Berg /* 45703e4497eSJohannes Berg * Can't be a WARN_ON because it can be triggered through a race: 45803e4497eSJohannes Berg * something inserts a STA (on one CPU) without holding the RTNL 45903e4497eSJohannes Berg * and another CPU turns off the net device. 46003e4497eSJohannes Berg */ 4618c71df7aSGuy Eilam if (unlikely(!ieee80211_sdata_running(sdata))) 4628c71df7aSGuy Eilam return -ENETDOWN; 46303e4497eSJohannes Berg 464b203ca39SJoe Perches if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) || 4658c71df7aSGuy Eilam is_multicast_ether_addr(sta->sta.addr))) 4668c71df7aSGuy Eilam return -EINVAL; 4678c71df7aSGuy Eilam 4688c71df7aSGuy Eilam return 0; 46993e5deb1SJohannes Berg } 47044213b5eSJohannes Berg 471f09603a2SJohannes Berg static int sta_info_insert_drv_state(struct ieee80211_local *local, 472f09603a2SJohannes Berg struct ieee80211_sub_if_data *sdata, 473f09603a2SJohannes Berg struct sta_info *sta) 474f09603a2SJohannes Berg { 475f09603a2SJohannes Berg enum ieee80211_sta_state state; 476f09603a2SJohannes Berg int err = 0; 477f09603a2SJohannes Berg 478f09603a2SJohannes Berg for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) { 479f09603a2SJohannes Berg err = drv_sta_state(local, sdata, sta, state, state + 1); 480f09603a2SJohannes Berg if (err) 481f09603a2SJohannes Berg break; 482f09603a2SJohannes Berg } 483f09603a2SJohannes Berg 484f09603a2SJohannes Berg if (!err) { 485a4ec45a4SJohannes Berg /* 486a4ec45a4SJohannes Berg * Drivers using legacy sta_add/sta_remove callbacks only 487a4ec45a4SJohannes Berg * get uploaded set to true after sta_add is called. 488a4ec45a4SJohannes Berg */ 489a4ec45a4SJohannes Berg if (!local->ops->sta_add) 490f09603a2SJohannes Berg sta->uploaded = true; 491f09603a2SJohannes Berg return 0; 492f09603a2SJohannes Berg } 493f09603a2SJohannes Berg 494f09603a2SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { 495bdcbd8e0SJohannes Berg sdata_info(sdata, 496bdcbd8e0SJohannes Berg "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n", 497bdcbd8e0SJohannes Berg sta->sta.addr, state + 1, err); 498f09603a2SJohannes Berg err = 0; 499f09603a2SJohannes Berg } 500f09603a2SJohannes Berg 501f09603a2SJohannes Berg /* unwind on error */ 502f09603a2SJohannes Berg for (; state > IEEE80211_STA_NOTEXIST; state--) 503f09603a2SJohannes Berg WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1)); 504f09603a2SJohannes Berg 505f09603a2SJohannes Berg return err; 506f09603a2SJohannes Berg } 507f09603a2SJohannes Berg 50834e89507SJohannes Berg /* 5098c71df7aSGuy Eilam * should be called with sta_mtx locked 5108c71df7aSGuy Eilam * this function replaces the mutex lock 5118c71df7aSGuy Eilam * with a RCU lock 5128c71df7aSGuy Eilam */ 5134d33960bSJohannes Berg static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU) 5148c71df7aSGuy Eilam { 5158c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 5168c71df7aSGuy Eilam struct ieee80211_sub_if_data *sdata = sta->sdata; 5177852e361SJohannes Berg struct station_info sinfo; 5188c71df7aSGuy Eilam int err = 0; 5198c71df7aSGuy Eilam 5208c71df7aSGuy Eilam lockdep_assert_held(&local->sta_mtx); 5218c71df7aSGuy Eilam 5227852e361SJohannes Berg /* check if STA exists already */ 5237852e361SJohannes Berg if (sta_info_get_bss(sdata, sta->sta.addr)) { 5244d33960bSJohannes Berg err = -EEXIST; 5254d33960bSJohannes Berg goto out_err; 52634e89507SJohannes Berg } 52734e89507SJohannes Berg 5284d33960bSJohannes Berg /* notify driver */ 529f09603a2SJohannes Berg err = sta_info_insert_drv_state(local, sdata, sta); 530f09603a2SJohannes Berg if (err) 531f09603a2SJohannes Berg goto out_err; 5324d33960bSJohannes Berg 5334d33960bSJohannes Berg local->num_sta++; 5344d33960bSJohannes Berg local->sta_generation++; 5354d33960bSJohannes Berg smp_mb(); 5364d33960bSJohannes Berg 5374d33960bSJohannes Berg /* make the station visible */ 5384d33960bSJohannes Berg sta_info_hash_add(local, sta); 5394d33960bSJohannes Berg 540794454ceSArik Nemtsov list_add_rcu(&sta->list, &local->sta_list); 54183d5cc01SJohannes Berg 54283d5cc01SJohannes Berg set_sta_flag(sta, WLAN_STA_INSERTED); 5434d33960bSJohannes Berg 54421f659bfSEliad Peller ieee80211_recalc_min_chandef(sdata); 5454d33960bSJohannes Berg ieee80211_sta_debugfs_add(sta); 5464d33960bSJohannes Berg rate_control_add_sta_debugfs(sta); 5474d33960bSJohannes Berg 5484d33960bSJohannes Berg memset(&sinfo, 0, sizeof(sinfo)); 5494d33960bSJohannes Berg sinfo.filled = 0; 5504d33960bSJohannes Berg sinfo.generation = local->sta_generation; 5514d33960bSJohannes Berg cfg80211_new_sta(sdata->dev, sta->sta.addr, &sinfo, GFP_KERNEL); 552d0709a65SJohannes Berg 553bdcbd8e0SJohannes Berg sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr); 554f0706e82SJiri Benc 55534e89507SJohannes Berg /* move reference to rcu-protected */ 55634e89507SJohannes Berg rcu_read_lock(); 55734e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 558e9f207f0SJiri Benc 55973651ee6SJohannes Berg if (ieee80211_vif_is_mesh(&sdata->vif)) 56073651ee6SJohannes Berg mesh_accept_plinks_update(sdata); 56173651ee6SJohannes Berg 56273651ee6SJohannes Berg return 0; 5634d33960bSJohannes Berg out_err: 5644d33960bSJohannes Berg mutex_unlock(&local->sta_mtx); 5654d33960bSJohannes Berg rcu_read_lock(); 5664d33960bSJohannes Berg return err; 5678c71df7aSGuy Eilam } 5688c71df7aSGuy Eilam 5698c71df7aSGuy Eilam int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) 5708c71df7aSGuy Eilam { 5718c71df7aSGuy Eilam struct ieee80211_local *local = sta->local; 5728c71df7aSGuy Eilam int err = 0; 5738c71df7aSGuy Eilam 5744d33960bSJohannes Berg might_sleep(); 5754d33960bSJohannes Berg 5768c71df7aSGuy Eilam err = sta_info_insert_check(sta); 5778c71df7aSGuy Eilam if (err) { 5788c71df7aSGuy Eilam rcu_read_lock(); 5798c71df7aSGuy Eilam goto out_free; 5808c71df7aSGuy Eilam } 5818c71df7aSGuy Eilam 582004c872eSJohannes Berg mutex_lock(&local->sta_mtx); 583004c872eSJohannes Berg 5844d33960bSJohannes Berg err = sta_info_insert_finish(sta); 5858c71df7aSGuy Eilam if (err) 586004c872eSJohannes Berg goto out_free; 587d0709a65SJohannes Berg 588004c872eSJohannes Berg return 0; 58993e5deb1SJohannes Berg out_free: 59093e5deb1SJohannes Berg BUG_ON(!err); 591d9a7ddb0SJohannes Berg sta_info_free(local, sta); 59293e5deb1SJohannes Berg return err; 593f0706e82SJiri Benc } 594f0706e82SJiri Benc 59534e89507SJohannes Berg int sta_info_insert(struct sta_info *sta) 59634e89507SJohannes Berg { 59734e89507SJohannes Berg int err = sta_info_insert_rcu(sta); 59834e89507SJohannes Berg 59934e89507SJohannes Berg rcu_read_unlock(); 60034e89507SJohannes Berg 60134e89507SJohannes Berg return err; 60234e89507SJohannes Berg } 60334e89507SJohannes Berg 604d012a605SMarco Porsch static inline void __bss_tim_set(u8 *tim, u16 id) 605004c872eSJohannes Berg { 606004c872eSJohannes Berg /* 607004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 608004c872eSJohannes Berg * so this line may not be changed to use the __set_bit() format. 609004c872eSJohannes Berg */ 610d012a605SMarco Porsch tim[id / 8] |= (1 << (id % 8)); 611004c872eSJohannes Berg } 612004c872eSJohannes Berg 613d012a605SMarco Porsch static inline void __bss_tim_clear(u8 *tim, u16 id) 614004c872eSJohannes Berg { 615004c872eSJohannes Berg /* 616004c872eSJohannes Berg * This format has been mandated by the IEEE specifications, 617004c872eSJohannes Berg * so this line may not be changed to use the __clear_bit() format. 618004c872eSJohannes Berg */ 619d012a605SMarco Porsch tim[id / 8] &= ~(1 << (id % 8)); 620004c872eSJohannes Berg } 621004c872eSJohannes Berg 6223d5839b6SIlan Peer static inline bool __bss_tim_get(u8 *tim, u16 id) 6233d5839b6SIlan Peer { 6243d5839b6SIlan Peer /* 6253d5839b6SIlan Peer * This format has been mandated by the IEEE specifications, 6263d5839b6SIlan Peer * so this line may not be changed to use the test_bit() format. 6273d5839b6SIlan Peer */ 6283d5839b6SIlan Peer return tim[id / 8] & (1 << (id % 8)); 6293d5839b6SIlan Peer } 6303d5839b6SIlan Peer 631948d887dSJohannes Berg static unsigned long ieee80211_tids_for_ac(int ac) 632004c872eSJohannes Berg { 633948d887dSJohannes Berg /* If we ever support TIDs > 7, this obviously needs to be adjusted */ 634948d887dSJohannes Berg switch (ac) { 635948d887dSJohannes Berg case IEEE80211_AC_VO: 636948d887dSJohannes Berg return BIT(6) | BIT(7); 637948d887dSJohannes Berg case IEEE80211_AC_VI: 638948d887dSJohannes Berg return BIT(4) | BIT(5); 639948d887dSJohannes Berg case IEEE80211_AC_BE: 640948d887dSJohannes Berg return BIT(0) | BIT(3); 641948d887dSJohannes Berg case IEEE80211_AC_BK: 642948d887dSJohannes Berg return BIT(1) | BIT(2); 643948d887dSJohannes Berg default: 644948d887dSJohannes Berg WARN_ON(1); 645948d887dSJohannes Berg return 0; 646d0709a65SJohannes Berg } 647004c872eSJohannes Berg } 648004c872eSJohannes Berg 649c868cb35SJohannes Berg void sta_info_recalc_tim(struct sta_info *sta) 650004c872eSJohannes Berg { 651c868cb35SJohannes Berg struct ieee80211_local *local = sta->local; 652d012a605SMarco Porsch struct ps_data *ps; 653948d887dSJohannes Berg bool indicate_tim = false; 654948d887dSJohannes Berg u8 ignore_for_tim = sta->sta.uapsd_queues; 655948d887dSJohannes Berg int ac; 656d012a605SMarco Porsch u16 id; 657004c872eSJohannes Berg 658d012a605SMarco Porsch if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 659d012a605SMarco Porsch sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 660c868cb35SJohannes Berg if (WARN_ON_ONCE(!sta->sdata->bss)) 661c868cb35SJohannes Berg return; 6623e122be0SJohannes Berg 663d012a605SMarco Porsch ps = &sta->sdata->bss->ps; 664d012a605SMarco Porsch id = sta->sta.aid; 6653f52b7e3SMarco Porsch #ifdef CONFIG_MAC80211_MESH 6663f52b7e3SMarco Porsch } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) { 6673f52b7e3SMarco Porsch ps = &sta->sdata->u.mesh.ps; 668204d1304SThomas Pedersen /* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */ 6696f101ef0SChun-Yeow Yeoh id = sta->plid % (IEEE80211_MAX_AID + 1); 6703f52b7e3SMarco Porsch #endif 671d012a605SMarco Porsch } else { 672d012a605SMarco Porsch return; 673d012a605SMarco Porsch } 674d012a605SMarco Porsch 675c868cb35SJohannes Berg /* No need to do anything if the driver does all */ 676c868cb35SJohannes Berg if (local->hw.flags & IEEE80211_HW_AP_LINK_PS) 677c868cb35SJohannes Berg return; 678004c872eSJohannes Berg 679c868cb35SJohannes Berg if (sta->dead) 680c868cb35SJohannes Berg goto done; 6813e122be0SJohannes Berg 682948d887dSJohannes Berg /* 683948d887dSJohannes Berg * If all ACs are delivery-enabled then we should build 684948d887dSJohannes Berg * the TIM bit for all ACs anyway; if only some are then 685948d887dSJohannes Berg * we ignore those and build the TIM bit using only the 686948d887dSJohannes Berg * non-enabled ones. 687948d887dSJohannes Berg */ 688948d887dSJohannes Berg if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1) 689948d887dSJohannes Berg ignore_for_tim = 0; 690948d887dSJohannes Berg 691948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 692948d887dSJohannes Berg unsigned long tids; 693948d887dSJohannes Berg 694948d887dSJohannes Berg if (ignore_for_tim & BIT(ac)) 695948d887dSJohannes Berg continue; 696948d887dSJohannes Berg 697948d887dSJohannes Berg indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) || 698948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac]); 699948d887dSJohannes Berg if (indicate_tim) 700948d887dSJohannes Berg break; 701948d887dSJohannes Berg 702948d887dSJohannes Berg tids = ieee80211_tids_for_ac(ac); 703948d887dSJohannes Berg 704948d887dSJohannes Berg indicate_tim |= 705948d887dSJohannes Berg sta->driver_buffered_tids & tids; 706004c872eSJohannes Berg } 707004c872eSJohannes Berg 708c868cb35SJohannes Berg done: 70965f704a5SJohannes Berg spin_lock_bh(&local->tim_lock); 710004c872eSJohannes Berg 7113d5839b6SIlan Peer if (indicate_tim == __bss_tim_get(ps->tim, id)) 7123d5839b6SIlan Peer goto out_unlock; 7133d5839b6SIlan Peer 714948d887dSJohannes Berg if (indicate_tim) 715d012a605SMarco Porsch __bss_tim_set(ps->tim, id); 716c868cb35SJohannes Berg else 717d012a605SMarco Porsch __bss_tim_clear(ps->tim, id); 7183e122be0SJohannes Berg 719c868cb35SJohannes Berg if (local->ops->set_tim) { 720c868cb35SJohannes Berg local->tim_in_locked_section = true; 721948d887dSJohannes Berg drv_set_tim(local, &sta->sta, indicate_tim); 722c868cb35SJohannes Berg local->tim_in_locked_section = false; 723004c872eSJohannes Berg } 724004c872eSJohannes Berg 7253d5839b6SIlan Peer out_unlock: 72665f704a5SJohannes Berg spin_unlock_bh(&local->tim_lock); 727004c872eSJohannes Berg } 728004c872eSJohannes Berg 729cd0b8d89SJohannes Berg static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb) 730f0706e82SJiri Benc { 731e039fa4aSJohannes Berg struct ieee80211_tx_info *info; 732f0706e82SJiri Benc int timeout; 733f0706e82SJiri Benc 734f0706e82SJiri Benc if (!skb) 735cd0b8d89SJohannes Berg return false; 736f0706e82SJiri Benc 737e039fa4aSJohannes Berg info = IEEE80211_SKB_CB(skb); 738f0706e82SJiri Benc 739f0706e82SJiri Benc /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ 74057c4d7b4SJohannes Berg timeout = (sta->listen_interval * 74157c4d7b4SJohannes Berg sta->sdata->vif.bss_conf.beacon_int * 74257c4d7b4SJohannes Berg 32 / 15625) * HZ; 743f0706e82SJiri Benc if (timeout < STA_TX_BUFFER_EXPIRE) 744f0706e82SJiri Benc timeout = STA_TX_BUFFER_EXPIRE; 745e039fa4aSJohannes Berg return time_after(jiffies, info->control.jiffies + timeout); 746f0706e82SJiri Benc } 747f0706e82SJiri Benc 748f0706e82SJiri Benc 749948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local, 750948d887dSJohannes Berg struct sta_info *sta, int ac) 751f0706e82SJiri Benc { 752f0706e82SJiri Benc unsigned long flags; 753f0706e82SJiri Benc struct sk_buff *skb; 754f0706e82SJiri Benc 75560750397SJohannes Berg /* 75660750397SJohannes Berg * First check for frames that should expire on the filtered 75760750397SJohannes Berg * queue. Frames here were rejected by the driver and are on 75860750397SJohannes Berg * a separate queue to avoid reordering with normal PS-buffered 75960750397SJohannes Berg * frames. They also aren't accounted for right now in the 76060750397SJohannes Berg * total_ps_buffered counter. 76160750397SJohannes Berg */ 762f0706e82SJiri Benc for (;;) { 763948d887dSJohannes Berg spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); 764948d887dSJohannes Berg skb = skb_peek(&sta->tx_filtered[ac]); 76557c4d7b4SJohannes Berg if (sta_info_buffer_expired(sta, skb)) 766948d887dSJohannes Berg skb = __skb_dequeue(&sta->tx_filtered[ac]); 767836341a7SJohannes Berg else 768f0706e82SJiri Benc skb = NULL; 769948d887dSJohannes Berg spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); 770f0706e82SJiri Benc 77160750397SJohannes Berg /* 77260750397SJohannes Berg * Frames are queued in order, so if this one 77360750397SJohannes Berg * hasn't expired yet we can stop testing. If 77460750397SJohannes Berg * we actually reached the end of the queue we 77560750397SJohannes Berg * also need to stop, of course. 77660750397SJohannes Berg */ 77760750397SJohannes Berg if (!skb) 77860750397SJohannes Berg break; 779d4fa14cdSFelix Fietkau ieee80211_free_txskb(&local->hw, skb); 78060750397SJohannes Berg } 78160750397SJohannes Berg 78260750397SJohannes Berg /* 78360750397SJohannes Berg * Now also check the normal PS-buffered queue, this will 78460750397SJohannes Berg * only find something if the filtered queue was emptied 78560750397SJohannes Berg * since the filtered frames are all before the normal PS 78660750397SJohannes Berg * buffered frames. 78760750397SJohannes Berg */ 788f0706e82SJiri Benc for (;;) { 789948d887dSJohannes Berg spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); 790948d887dSJohannes Berg skb = skb_peek(&sta->ps_tx_buf[ac]); 791f0706e82SJiri Benc if (sta_info_buffer_expired(sta, skb)) 792948d887dSJohannes Berg skb = __skb_dequeue(&sta->ps_tx_buf[ac]); 793f0706e82SJiri Benc else 794f0706e82SJiri Benc skb = NULL; 795948d887dSJohannes Berg spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); 796f0706e82SJiri Benc 79760750397SJohannes Berg /* 79860750397SJohannes Berg * frames are queued in order, so if this one 79960750397SJohannes Berg * hasn't expired yet (or we reached the end of 80060750397SJohannes Berg * the queue) we can stop testing 80160750397SJohannes Berg */ 802836341a7SJohannes Berg if (!skb) 803836341a7SJohannes Berg break; 804836341a7SJohannes Berg 805f0706e82SJiri Benc local->total_ps_buffered--; 806bdcbd8e0SJohannes Berg ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n", 807bdcbd8e0SJohannes Berg sta->sta.addr); 808d4fa14cdSFelix Fietkau ieee80211_free_txskb(&local->hw, skb); 809f0706e82SJiri Benc } 8103393a608SJuuso Oikarinen 81160750397SJohannes Berg /* 81260750397SJohannes Berg * Finally, recalculate the TIM bit for this station -- it might 81360750397SJohannes Berg * now be clear because the station was too slow to retrieve its 81460750397SJohannes Berg * frames. 81560750397SJohannes Berg */ 81660750397SJohannes Berg sta_info_recalc_tim(sta); 81760750397SJohannes Berg 81860750397SJohannes Berg /* 81960750397SJohannes Berg * Return whether there are any frames still buffered, this is 82060750397SJohannes Berg * used to check whether the cleanup timer still needs to run, 82160750397SJohannes Berg * if there are no frames we don't need to rearm the timer. 82260750397SJohannes Berg */ 823948d887dSJohannes Berg return !(skb_queue_empty(&sta->ps_tx_buf[ac]) && 824948d887dSJohannes Berg skb_queue_empty(&sta->tx_filtered[ac])); 825948d887dSJohannes Berg } 826948d887dSJohannes Berg 827948d887dSJohannes Berg static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local, 828948d887dSJohannes Berg struct sta_info *sta) 829948d887dSJohannes Berg { 830948d887dSJohannes Berg bool have_buffered = false; 831948d887dSJohannes Berg int ac; 832948d887dSJohannes Berg 8333f52b7e3SMarco Porsch /* This is only necessary for stations on BSS/MBSS interfaces */ 8343f52b7e3SMarco Porsch if (!sta->sdata->bss && 8353f52b7e3SMarco Porsch !ieee80211_vif_is_mesh(&sta->sdata->vif)) 836948d887dSJohannes Berg return false; 837948d887dSJohannes Berg 838948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 839948d887dSJohannes Berg have_buffered |= 840948d887dSJohannes Berg sta_info_cleanup_expire_buffered_ac(local, sta, ac); 841948d887dSJohannes Berg 842948d887dSJohannes Berg return have_buffered; 843f0706e82SJiri Benc } 844f0706e82SJiri Benc 84583d5cc01SJohannes Berg int __must_check __sta_info_destroy(struct sta_info *sta) 84634e89507SJohannes Berg { 84734e89507SJohannes Berg struct ieee80211_local *local; 84834e89507SJohannes Berg struct ieee80211_sub_if_data *sdata; 8496d10e46bSJohannes Berg int ret; 85034e89507SJohannes Berg 85134e89507SJohannes Berg might_sleep(); 85234e89507SJohannes Berg 85334e89507SJohannes Berg if (!sta) 85434e89507SJohannes Berg return -ENOENT; 85534e89507SJohannes Berg 85634e89507SJohannes Berg local = sta->local; 85734e89507SJohannes Berg sdata = sta->sdata; 85834e89507SJohannes Berg 85983d5cc01SJohannes Berg lockdep_assert_held(&local->sta_mtx); 86083d5cc01SJohannes Berg 861098a6070SJohannes Berg /* 862098a6070SJohannes Berg * Before removing the station from the driver and 863098a6070SJohannes Berg * rate control, it might still start new aggregation 864098a6070SJohannes Berg * sessions -- block that to make sure the tear-down 865098a6070SJohannes Berg * will be sufficient. 866098a6070SJohannes Berg */ 867c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_BLOCK_BA); 868c82c4a80SJohannes Berg ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA); 869098a6070SJohannes Berg 87034e89507SJohannes Berg ret = sta_info_hash_del(local, sta); 871*b01711beSJohannes Berg if (WARN_ON(ret)) 87234e89507SJohannes Berg return ret; 87334e89507SJohannes Berg 874794454ceSArik Nemtsov list_del_rcu(&sta->list); 8754d33960bSJohannes Berg 8766d10e46bSJohannes Berg /* this always calls synchronize_net() */ 8776d10e46bSJohannes Berg ieee80211_free_sta_keys(local, sta); 87834e89507SJohannes Berg 87934e89507SJohannes Berg sta->dead = true; 88034e89507SJohannes Berg 88134e89507SJohannes Berg local->num_sta--; 88234e89507SJohannes Berg local->sta_generation++; 88334e89507SJohannes Berg 88434e89507SJohannes Berg if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 885a9b3cd7fSStephen Hemminger RCU_INIT_POINTER(sdata->u.vlan.sta, NULL); 88634e89507SJohannes Berg 88783d5cc01SJohannes Berg while (sta->sta_state > IEEE80211_STA_NONE) { 888f09603a2SJohannes Berg ret = sta_info_move_state(sta, sta->sta_state - 1); 889f09603a2SJohannes Berg if (ret) { 89083d5cc01SJohannes Berg WARN_ON_ONCE(1); 89183d5cc01SJohannes Berg break; 89283d5cc01SJohannes Berg } 89383d5cc01SJohannes Berg } 894d9a7ddb0SJohannes Berg 895f09603a2SJohannes Berg if (sta->uploaded) { 896f09603a2SJohannes Berg ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE, 897f09603a2SJohannes Berg IEEE80211_STA_NOTEXIST); 898f09603a2SJohannes Berg WARN_ON_ONCE(ret != 0); 899f09603a2SJohannes Berg } 90034e89507SJohannes Berg 901bdcbd8e0SJohannes Berg sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr); 902bdcbd8e0SJohannes Berg 903ec15e68bSJouni Malinen cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL); 904ec15e68bSJouni Malinen 90534e89507SJohannes Berg rate_control_remove_sta_debugfs(sta); 90634e89507SJohannes Berg ieee80211_sta_debugfs_remove(sta); 90721f659bfSEliad Peller ieee80211_recalc_min_chandef(sdata); 90834e89507SJohannes Berg 909b22cfcfcSEliad Peller call_rcu(&sta->rcu_head, free_sta_rcu); 91034e89507SJohannes Berg 91134e89507SJohannes Berg return 0; 91234e89507SJohannes Berg } 91334e89507SJohannes Berg 91434e89507SJohannes Berg int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr) 91534e89507SJohannes Berg { 91634e89507SJohannes Berg struct sta_info *sta; 91734e89507SJohannes Berg int ret; 91834e89507SJohannes Berg 91934e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 9207852e361SJohannes Berg sta = sta_info_get(sdata, addr); 92134e89507SJohannes Berg ret = __sta_info_destroy(sta); 92234e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 92334e89507SJohannes Berg 92434e89507SJohannes Berg return ret; 92534e89507SJohannes Berg } 92634e89507SJohannes Berg 92734e89507SJohannes Berg int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata, 92834e89507SJohannes Berg const u8 *addr) 92934e89507SJohannes Berg { 93034e89507SJohannes Berg struct sta_info *sta; 93134e89507SJohannes Berg int ret; 93234e89507SJohannes Berg 93334e89507SJohannes Berg mutex_lock(&sdata->local->sta_mtx); 9347852e361SJohannes Berg sta = sta_info_get_bss(sdata, addr); 93534e89507SJohannes Berg ret = __sta_info_destroy(sta); 93634e89507SJohannes Berg mutex_unlock(&sdata->local->sta_mtx); 93734e89507SJohannes Berg 93834e89507SJohannes Berg return ret; 93934e89507SJohannes Berg } 940f0706e82SJiri Benc 941f0706e82SJiri Benc static void sta_info_cleanup(unsigned long data) 942f0706e82SJiri Benc { 943f0706e82SJiri Benc struct ieee80211_local *local = (struct ieee80211_local *) data; 944f0706e82SJiri Benc struct sta_info *sta; 9453393a608SJuuso Oikarinen bool timer_needed = false; 946f0706e82SJiri Benc 947d0709a65SJohannes Berg rcu_read_lock(); 948d0709a65SJohannes Berg list_for_each_entry_rcu(sta, &local->sta_list, list) 9493393a608SJuuso Oikarinen if (sta_info_cleanup_expire_buffered(local, sta)) 9503393a608SJuuso Oikarinen timer_needed = true; 951d0709a65SJohannes Berg rcu_read_unlock(); 952f0706e82SJiri Benc 9535bb644a0SJohannes Berg if (local->quiescing) 9545bb644a0SJohannes Berg return; 9555bb644a0SJohannes Berg 9563393a608SJuuso Oikarinen if (!timer_needed) 9573393a608SJuuso Oikarinen return; 9583393a608SJuuso Oikarinen 95926d59535SJohannes Berg mod_timer(&local->sta_cleanup, 96026d59535SJohannes Berg round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); 961f0706e82SJiri Benc } 962f0706e82SJiri Benc 963f0706e82SJiri Benc void sta_info_init(struct ieee80211_local *local) 964f0706e82SJiri Benc { 9654d33960bSJohannes Berg spin_lock_init(&local->tim_lock); 96634e89507SJohannes Berg mutex_init(&local->sta_mtx); 967f0706e82SJiri Benc INIT_LIST_HEAD(&local->sta_list); 968f0706e82SJiri Benc 969b24b8a24SPavel Emelyanov setup_timer(&local->sta_cleanup, sta_info_cleanup, 970b24b8a24SPavel Emelyanov (unsigned long)local); 971f0706e82SJiri Benc } 972f0706e82SJiri Benc 973f0706e82SJiri Benc void sta_info_stop(struct ieee80211_local *local) 974f0706e82SJiri Benc { 975a56f992cSJohannes Berg del_timer_sync(&local->sta_cleanup); 976f0706e82SJiri Benc } 977f0706e82SJiri Benc 978051007d9SJohannes Berg 979051007d9SJohannes Berg int sta_info_flush_defer(struct ieee80211_sub_if_data *sdata) 980f0706e82SJiri Benc { 981b998e8bbSJohannes Berg struct ieee80211_local *local = sdata->local; 982f0706e82SJiri Benc struct sta_info *sta, *tmp; 98344213b5eSJohannes Berg int ret = 0; 984f0706e82SJiri Benc 985d0709a65SJohannes Berg might_sleep(); 986d0709a65SJohannes Berg 98734e89507SJohannes Berg mutex_lock(&local->sta_mtx); 98834e89507SJohannes Berg list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { 989b998e8bbSJohannes Berg if (sdata == sta->sdata) { 99034e89507SJohannes Berg WARN_ON(__sta_info_destroy(sta)); 99134316837SJohannes Berg ret++; 99234316837SJohannes Berg } 99334e89507SJohannes Berg } 99434e89507SJohannes Berg mutex_unlock(&local->sta_mtx); 99544213b5eSJohannes Berg 996051007d9SJohannes Berg return ret; 997051007d9SJohannes Berg } 998051007d9SJohannes Berg 999051007d9SJohannes Berg void sta_info_flush_cleanup(struct ieee80211_sub_if_data *sdata) 1000051007d9SJohannes Berg { 100197f97b1fSJohannes Berg ieee80211_cleanup_sdata_stas(sdata); 100297f97b1fSJohannes Berg cancel_work_sync(&sdata->cleanup_stations_wk); 1003f0706e82SJiri Benc } 1004dc6676b7SJohannes 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 123047086fc5SJohannes Berg static void 123147086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(struct sta_info *sta, 123247086fc5SJohannes Berg int n_frames, u8 ignored_acs, 123347086fc5SJohannes Berg enum ieee80211_frame_release_type reason) 1234af818581SJohannes Berg { 1235af818581SJohannes Berg struct ieee80211_sub_if_data *sdata = sta->sdata; 1236af818581SJohannes Berg struct ieee80211_local *local = sdata->local; 12374049e09aSJohannes Berg bool found = false; 1238948d887dSJohannes Berg bool more_data = false; 1239948d887dSJohannes Berg int ac; 12404049e09aSJohannes Berg unsigned long driver_release_tids = 0; 124147086fc5SJohannes Berg struct sk_buff_head frames; 124247086fc5SJohannes Berg 1243deeaee19SJohannes Berg /* Service or PS-Poll period starts */ 1244c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_SP); 1245deeaee19SJohannes Berg 124647086fc5SJohannes Berg __skb_queue_head_init(&frames); 1247af818581SJohannes Berg 1248948d887dSJohannes Berg /* 124947086fc5SJohannes Berg * Get response frame(s) and more data bit for it. 1250948d887dSJohannes Berg */ 1251948d887dSJohannes Berg for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 12524049e09aSJohannes Berg unsigned long tids; 12534049e09aSJohannes Berg 125447086fc5SJohannes Berg if (ignored_acs & BIT(ac)) 1255948d887dSJohannes Berg continue; 1256948d887dSJohannes Berg 12574049e09aSJohannes Berg tids = ieee80211_tids_for_ac(ac); 12584049e09aSJohannes Berg 12594049e09aSJohannes Berg if (!found) { 12604049e09aSJohannes Berg driver_release_tids = sta->driver_buffered_tids & tids; 12614049e09aSJohannes Berg if (driver_release_tids) { 12624049e09aSJohannes Berg found = true; 12634049e09aSJohannes Berg } else { 126447086fc5SJohannes Berg struct sk_buff *skb; 126547086fc5SJohannes Berg 126647086fc5SJohannes Berg while (n_frames > 0) { 1267948d887dSJohannes Berg skb = skb_dequeue(&sta->tx_filtered[ac]); 1268948d887dSJohannes Berg if (!skb) { 126947086fc5SJohannes Berg skb = skb_dequeue( 127047086fc5SJohannes Berg &sta->ps_tx_buf[ac]); 1271af818581SJohannes Berg if (skb) 1272af818581SJohannes Berg local->total_ps_buffered--; 1273af818581SJohannes Berg } 127447086fc5SJohannes Berg if (!skb) 127547086fc5SJohannes Berg break; 127647086fc5SJohannes Berg n_frames--; 12774049e09aSJohannes Berg found = true; 127847086fc5SJohannes Berg __skb_queue_tail(&frames, skb); 127947086fc5SJohannes Berg } 1280948d887dSJohannes Berg } 1281948d887dSJohannes Berg 12824049e09aSJohannes Berg /* 12834049e09aSJohannes Berg * If the driver has data on more than one TID then 12844049e09aSJohannes Berg * certainly there's more data if we release just a 12854049e09aSJohannes Berg * single frame now (from a single TID). 12864049e09aSJohannes Berg */ 128747086fc5SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_PSPOLL && 128847086fc5SJohannes Berg hweight16(driver_release_tids) > 1) { 12894049e09aSJohannes Berg more_data = true; 12904049e09aSJohannes Berg driver_release_tids = 12914049e09aSJohannes Berg BIT(ffs(driver_release_tids) - 1); 12924049e09aSJohannes Berg break; 12934049e09aSJohannes Berg } 12944049e09aSJohannes Berg } 1295948d887dSJohannes Berg 1296948d887dSJohannes Berg if (!skb_queue_empty(&sta->tx_filtered[ac]) || 1297948d887dSJohannes Berg !skb_queue_empty(&sta->ps_tx_buf[ac])) { 1298948d887dSJohannes Berg more_data = true; 1299948d887dSJohannes Berg break; 1300948d887dSJohannes Berg } 1301948d887dSJohannes Berg } 1302af818581SJohannes Berg 13034049e09aSJohannes Berg if (!found) { 1304ce662b44SJohannes Berg int tid; 13054049e09aSJohannes Berg 1306ce662b44SJohannes Berg /* 1307ce662b44SJohannes Berg * For PS-Poll, this can only happen due to a race condition 1308ce662b44SJohannes Berg * when we set the TIM bit and the station notices it, but 1309ce662b44SJohannes Berg * before it can poll for the frame we expire it. 1310ce662b44SJohannes Berg * 1311ce662b44SJohannes Berg * For uAPSD, this is said in the standard (11.2.1.5 h): 1312ce662b44SJohannes Berg * At each unscheduled SP for a non-AP STA, the AP shall 1313ce662b44SJohannes Berg * attempt to transmit at least one MSDU or MMPDU, but no 1314ce662b44SJohannes Berg * more than the value specified in the Max SP Length field 1315ce662b44SJohannes Berg * in the QoS Capability element from delivery-enabled ACs, 1316ce662b44SJohannes Berg * that are destined for the non-AP STA. 1317ce662b44SJohannes Berg * 1318ce662b44SJohannes Berg * Since we have no other MSDU/MMPDU, transmit a QoS null frame. 1319ce662b44SJohannes Berg */ 1320ce662b44SJohannes Berg 1321ce662b44SJohannes Berg /* This will evaluate to 1, 3, 5 or 7. */ 1322ce662b44SJohannes Berg tid = 7 - ((ffs(~ignored_acs) - 1) << 1); 1323ce662b44SJohannes Berg 132440b96408SJohannes Berg ieee80211_send_null_response(sdata, sta, tid, reason); 13254049e09aSJohannes Berg return; 13264049e09aSJohannes Berg } 13274049e09aSJohannes Berg 132847086fc5SJohannes Berg if (!driver_release_tids) { 132947086fc5SJohannes Berg struct sk_buff_head pending; 133047086fc5SJohannes Berg struct sk_buff *skb; 133140b96408SJohannes Berg int num = 0; 133240b96408SJohannes Berg u16 tids = 0; 133347086fc5SJohannes Berg 133447086fc5SJohannes Berg skb_queue_head_init(&pending); 133547086fc5SJohannes Berg 133647086fc5SJohannes Berg while ((skb = __skb_dequeue(&frames))) { 1337af818581SJohannes Berg struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 133847086fc5SJohannes Berg struct ieee80211_hdr *hdr = (void *) skb->data; 133940b96408SJohannes Berg u8 *qoshdr = NULL; 134040b96408SJohannes Berg 134140b96408SJohannes Berg num++; 1342af818581SJohannes Berg 1343af818581SJohannes Berg /* 134447086fc5SJohannes Berg * Tell TX path to send this frame even though the 134547086fc5SJohannes Berg * STA may still remain is PS mode after this frame 134647086fc5SJohannes Berg * exchange. 1347af818581SJohannes Berg */ 1348d6d23de2SFelix Fietkau info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER | 1349d6d23de2SFelix Fietkau IEEE80211_TX_CTL_PS_RESPONSE; 1350af818581SJohannes Berg 135147086fc5SJohannes Berg /* 135247086fc5SJohannes Berg * Use MoreData flag to indicate whether there are 135347086fc5SJohannes Berg * more buffered frames for this STA 135447086fc5SJohannes Berg */ 135524b9c373SJanusz.Dziedzic@tieto.com if (more_data || !skb_queue_empty(&frames)) 135647086fc5SJohannes Berg hdr->frame_control |= 135747086fc5SJohannes Berg cpu_to_le16(IEEE80211_FCTL_MOREDATA); 135824b9c373SJanusz.Dziedzic@tieto.com else 135924b9c373SJanusz.Dziedzic@tieto.com hdr->frame_control &= 136024b9c373SJanusz.Dziedzic@tieto.com cpu_to_le16(~IEEE80211_FCTL_MOREDATA); 1361af818581SJohannes Berg 136240b96408SJohannes Berg if (ieee80211_is_data_qos(hdr->frame_control) || 136340b96408SJohannes Berg ieee80211_is_qos_nullfunc(hdr->frame_control)) 136440b96408SJohannes Berg qoshdr = ieee80211_get_qos_ctl(hdr); 136540b96408SJohannes Berg 136652a3f20cSMarco Porsch /* end service period after last frame */ 136752a3f20cSMarco Porsch if (skb_queue_empty(&frames)) { 136840b96408SJohannes Berg if (reason == IEEE80211_FRAME_RELEASE_UAPSD && 136952a3f20cSMarco Porsch qoshdr) 137040b96408SJohannes Berg *qoshdr |= IEEE80211_QOS_CTL_EOSP; 1371deeaee19SJohannes Berg 137247086fc5SJohannes Berg info->flags |= IEEE80211_TX_STATUS_EOSP | 137347086fc5SJohannes Berg IEEE80211_TX_CTL_REQ_TX_STATUS; 137452a3f20cSMarco Porsch } 137547086fc5SJohannes Berg 137640b96408SJohannes Berg if (qoshdr) 137740b96408SJohannes Berg tids |= BIT(*qoshdr & IEEE80211_QOS_CTL_TID_MASK); 137840b96408SJohannes Berg else 137940b96408SJohannes Berg tids |= BIT(0); 138040b96408SJohannes Berg 138147086fc5SJohannes Berg __skb_queue_tail(&pending, skb); 138247086fc5SJohannes Berg } 138347086fc5SJohannes Berg 138440b96408SJohannes Berg drv_allow_buffered_frames(local, sta, tids, num, 138540b96408SJohannes Berg reason, more_data); 138640b96408SJohannes Berg 138747086fc5SJohannes Berg ieee80211_add_pending_skbs(local, &pending); 1388af818581SJohannes Berg 1389c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1390af818581SJohannes Berg } else { 1391af818581SJohannes Berg /* 13924049e09aSJohannes Berg * We need to release a frame that is buffered somewhere in the 13934049e09aSJohannes Berg * driver ... it'll have to handle that. 13944049e09aSJohannes Berg * Note that, as per the comment above, it'll also have to see 13954049e09aSJohannes Berg * if there is more than just one frame on the specific TID that 13964049e09aSJohannes Berg * we're releasing from, and it needs to set the more-data bit 13974049e09aSJohannes Berg * accordingly if we tell it that there's no more data. If we do 13984049e09aSJohannes Berg * tell it there's more data, then of course the more-data bit 13994049e09aSJohannes Berg * needs to be set anyway. 1400af818581SJohannes Berg */ 14014049e09aSJohannes Berg drv_release_buffered_frames(local, sta, driver_release_tids, 140247086fc5SJohannes Berg n_frames, reason, more_data); 14034049e09aSJohannes Berg 14044049e09aSJohannes Berg /* 14054049e09aSJohannes Berg * Note that we don't recalculate the TIM bit here as it would 14064049e09aSJohannes Berg * most likely have no effect at all unless the driver told us 14074049e09aSJohannes Berg * that the TID became empty before returning here from the 14084049e09aSJohannes Berg * release function. 14094049e09aSJohannes Berg * Either way, however, when the driver tells us that the TID 14104049e09aSJohannes Berg * became empty we'll do the TIM recalculation. 14114049e09aSJohannes Berg */ 1412af818581SJohannes Berg } 1413af818581SJohannes Berg } 1414af818581SJohannes Berg 1415af818581SJohannes Berg void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) 1416af818581SJohannes Berg { 141747086fc5SJohannes Berg u8 ignore_for_response = sta->sta.uapsd_queues; 1418af818581SJohannes Berg 1419af818581SJohannes Berg /* 142047086fc5SJohannes Berg * If all ACs are delivery-enabled then we should reply 142147086fc5SJohannes Berg * from any of them, if only some are enabled we reply 142247086fc5SJohannes Berg * only from the non-enabled ones. 1423af818581SJohannes Berg */ 142447086fc5SJohannes Berg if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1) 142547086fc5SJohannes Berg ignore_for_response = 0; 1426af818581SJohannes Berg 142747086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response, 142847086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_PSPOLL); 1429af818581SJohannes Berg } 143047086fc5SJohannes Berg 143147086fc5SJohannes Berg void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta) 143247086fc5SJohannes Berg { 143347086fc5SJohannes Berg int n_frames = sta->sta.max_sp; 143447086fc5SJohannes Berg u8 delivery_enabled = sta->sta.uapsd_queues; 143547086fc5SJohannes Berg 143647086fc5SJohannes Berg /* 143747086fc5SJohannes Berg * If we ever grow support for TSPEC this might happen if 143847086fc5SJohannes Berg * the TSPEC update from hostapd comes in between a trigger 143947086fc5SJohannes Berg * frame setting WLAN_STA_UAPSD in the RX path and this 144047086fc5SJohannes Berg * actually getting called. 144147086fc5SJohannes Berg */ 144247086fc5SJohannes Berg if (!delivery_enabled) 144347086fc5SJohannes Berg return; 144447086fc5SJohannes Berg 144547086fc5SJohannes Berg switch (sta->sta.max_sp) { 144647086fc5SJohannes Berg case 1: 144747086fc5SJohannes Berg n_frames = 2; 144847086fc5SJohannes Berg break; 144947086fc5SJohannes Berg case 2: 145047086fc5SJohannes Berg n_frames = 4; 145147086fc5SJohannes Berg break; 145247086fc5SJohannes Berg case 3: 145347086fc5SJohannes Berg n_frames = 6; 145447086fc5SJohannes Berg break; 145547086fc5SJohannes Berg case 0: 145647086fc5SJohannes Berg /* XXX: what is a good value? */ 145747086fc5SJohannes Berg n_frames = 8; 145847086fc5SJohannes Berg break; 145947086fc5SJohannes Berg } 146047086fc5SJohannes Berg 146147086fc5SJohannes Berg ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled, 146247086fc5SJohannes Berg IEEE80211_FRAME_RELEASE_UAPSD); 1463af818581SJohannes Berg } 1464af818581SJohannes Berg 1465af818581SJohannes Berg void ieee80211_sta_block_awake(struct ieee80211_hw *hw, 1466af818581SJohannes Berg struct ieee80211_sta *pubsta, bool block) 1467af818581SJohannes Berg { 1468af818581SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1469af818581SJohannes Berg 1470b5878a2dSJohannes Berg trace_api_sta_block_awake(sta->local, pubsta, block); 1471b5878a2dSJohannes Berg 1472af818581SJohannes Berg if (block) 1473c2c98fdeSJohannes Berg set_sta_flag(sta, WLAN_STA_PS_DRIVER); 1474c2c98fdeSJohannes Berg else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER)) 1475af818581SJohannes Berg ieee80211_queue_work(hw, &sta->drv_unblock_wk); 1476af818581SJohannes Berg } 1477af818581SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_block_awake); 1478dcf55fb5SFelix Fietkau 1479e943789eSJohannes Berg void ieee80211_sta_eosp(struct ieee80211_sta *pubsta) 148037fbd908SJohannes Berg { 148137fbd908SJohannes Berg struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 148237fbd908SJohannes Berg struct ieee80211_local *local = sta->local; 148337fbd908SJohannes Berg 148437fbd908SJohannes Berg trace_api_eosp(local, pubsta); 148537fbd908SJohannes Berg 148637fbd908SJohannes Berg clear_sta_flag(sta, WLAN_STA_SP); 148737fbd908SJohannes Berg } 1488e943789eSJohannes Berg EXPORT_SYMBOL(ieee80211_sta_eosp); 148937fbd908SJohannes Berg 1490042ec453SJohannes Berg void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta, 1491042ec453SJohannes Berg u8 tid, bool buffered) 1492dcf55fb5SFelix Fietkau { 1493dcf55fb5SFelix Fietkau struct sta_info *sta = container_of(pubsta, struct sta_info, sta); 1494dcf55fb5SFelix Fietkau 14955a306f58SJohannes Berg if (WARN_ON(tid >= IEEE80211_NUM_TIDS)) 1496042ec453SJohannes Berg return; 1497042ec453SJohannes Berg 1498948d887dSJohannes Berg if (buffered) 1499948d887dSJohannes Berg set_bit(tid, &sta->driver_buffered_tids); 1500948d887dSJohannes Berg else 1501948d887dSJohannes Berg clear_bit(tid, &sta->driver_buffered_tids); 1502948d887dSJohannes Berg 1503c868cb35SJohannes Berg sta_info_recalc_tim(sta); 1504dcf55fb5SFelix Fietkau } 1505042ec453SJohannes Berg EXPORT_SYMBOL(ieee80211_sta_set_buffered); 1506d9a7ddb0SJohannes Berg 150783d5cc01SJohannes Berg int sta_info_move_state(struct sta_info *sta, 1508d9a7ddb0SJohannes Berg enum ieee80211_sta_state new_state) 1509d9a7ddb0SJohannes Berg { 15108bf11d8dSJohannes Berg might_sleep(); 1511d9a7ddb0SJohannes Berg 1512d9a7ddb0SJohannes Berg if (sta->sta_state == new_state) 1513d9a7ddb0SJohannes Berg return 0; 1514d9a7ddb0SJohannes Berg 1515f09603a2SJohannes Berg /* check allowed transitions first */ 1516f09603a2SJohannes Berg 1517d9a7ddb0SJohannes Berg switch (new_state) { 1518d9a7ddb0SJohannes Berg case IEEE80211_STA_NONE: 1519f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_AUTH) 1520d9a7ddb0SJohannes Berg return -EINVAL; 1521d9a7ddb0SJohannes Berg break; 1522d9a7ddb0SJohannes Berg case IEEE80211_STA_AUTH: 1523f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_NONE && 1524f09603a2SJohannes Berg sta->sta_state != IEEE80211_STA_ASSOC) 1525d9a7ddb0SJohannes Berg return -EINVAL; 1526d9a7ddb0SJohannes Berg break; 1527d9a7ddb0SJohannes Berg case IEEE80211_STA_ASSOC: 1528f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_AUTH && 1529f09603a2SJohannes Berg sta->sta_state != IEEE80211_STA_AUTHORIZED) 1530d9a7ddb0SJohannes Berg return -EINVAL; 1531d9a7ddb0SJohannes Berg break; 1532d9a7ddb0SJohannes Berg case IEEE80211_STA_AUTHORIZED: 1533f09603a2SJohannes Berg if (sta->sta_state != IEEE80211_STA_ASSOC) 1534d9a7ddb0SJohannes Berg return -EINVAL; 1535d9a7ddb0SJohannes Berg break; 1536d9a7ddb0SJohannes Berg default: 1537d9a7ddb0SJohannes Berg WARN(1, "invalid state %d", new_state); 1538d9a7ddb0SJohannes Berg return -EINVAL; 1539d9a7ddb0SJohannes Berg } 1540d9a7ddb0SJohannes Berg 1541bdcbd8e0SJohannes Berg sta_dbg(sta->sdata, "moving STA %pM to state %d\n", 1542bdcbd8e0SJohannes Berg sta->sta.addr, new_state); 1543f09603a2SJohannes Berg 1544f09603a2SJohannes Berg /* 1545f09603a2SJohannes Berg * notify the driver before the actual changes so it can 1546f09603a2SJohannes Berg * fail the transition 1547f09603a2SJohannes Berg */ 1548f09603a2SJohannes Berg if (test_sta_flag(sta, WLAN_STA_INSERTED)) { 1549f09603a2SJohannes Berg int err = drv_sta_state(sta->local, sta->sdata, sta, 1550f09603a2SJohannes Berg sta->sta_state, new_state); 1551f09603a2SJohannes Berg if (err) 1552f09603a2SJohannes Berg return err; 1553f09603a2SJohannes Berg } 1554f09603a2SJohannes Berg 1555f09603a2SJohannes Berg /* reflect the change in all state variables */ 1556f09603a2SJohannes Berg 1557f09603a2SJohannes Berg switch (new_state) { 1558f09603a2SJohannes Berg case IEEE80211_STA_NONE: 1559f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_AUTH) 1560f09603a2SJohannes Berg clear_bit(WLAN_STA_AUTH, &sta->_flags); 1561f09603a2SJohannes Berg break; 1562f09603a2SJohannes Berg case IEEE80211_STA_AUTH: 1563f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_NONE) 1564f09603a2SJohannes Berg set_bit(WLAN_STA_AUTH, &sta->_flags); 1565f09603a2SJohannes Berg else if (sta->sta_state == IEEE80211_STA_ASSOC) 1566f09603a2SJohannes Berg clear_bit(WLAN_STA_ASSOC, &sta->_flags); 1567f09603a2SJohannes Berg break; 1568f09603a2SJohannes Berg case IEEE80211_STA_ASSOC: 1569f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_AUTH) { 1570f09603a2SJohannes Berg set_bit(WLAN_STA_ASSOC, &sta->_flags); 1571f09603a2SJohannes Berg } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) { 15727e3ed02cSFelix Fietkau if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 15737e3ed02cSFelix Fietkau (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 15747e3ed02cSFelix Fietkau !sta->sdata->u.vlan.sta)) 15757e3ed02cSFelix Fietkau atomic_dec(&sta->sdata->bss->num_mcast_sta); 1576f09603a2SJohannes Berg clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 1577f09603a2SJohannes Berg } 1578f09603a2SJohannes Berg break; 1579f09603a2SJohannes Berg case IEEE80211_STA_AUTHORIZED: 1580f09603a2SJohannes Berg if (sta->sta_state == IEEE80211_STA_ASSOC) { 15817e3ed02cSFelix Fietkau if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 15827e3ed02cSFelix Fietkau (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN && 15837e3ed02cSFelix Fietkau !sta->sdata->u.vlan.sta)) 15847e3ed02cSFelix Fietkau atomic_inc(&sta->sdata->bss->num_mcast_sta); 1585f09603a2SJohannes Berg set_bit(WLAN_STA_AUTHORIZED, &sta->_flags); 1586f09603a2SJohannes Berg } 1587f09603a2SJohannes Berg break; 1588f09603a2SJohannes Berg default: 1589f09603a2SJohannes Berg break; 1590f09603a2SJohannes Berg } 1591f09603a2SJohannes Berg 1592d9a7ddb0SJohannes Berg sta->sta_state = new_state; 1593d9a7ddb0SJohannes Berg 1594d9a7ddb0SJohannes Berg return 0; 1595d9a7ddb0SJohannes Berg } 1596687da132SEmmanuel Grumbach 1597687da132SEmmanuel Grumbach u8 sta_info_tx_streams(struct sta_info *sta) 1598687da132SEmmanuel Grumbach { 1599687da132SEmmanuel Grumbach struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap; 1600687da132SEmmanuel Grumbach u8 rx_streams; 1601687da132SEmmanuel Grumbach 1602687da132SEmmanuel Grumbach if (!sta->sta.ht_cap.ht_supported) 1603687da132SEmmanuel Grumbach return 1; 1604687da132SEmmanuel Grumbach 1605687da132SEmmanuel Grumbach if (sta->sta.vht_cap.vht_supported) { 1606687da132SEmmanuel Grumbach int i; 1607687da132SEmmanuel Grumbach u16 tx_mcs_map = 1608687da132SEmmanuel Grumbach le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map); 1609687da132SEmmanuel Grumbach 1610687da132SEmmanuel Grumbach for (i = 7; i >= 0; i--) 1611687da132SEmmanuel Grumbach if ((tx_mcs_map & (0x3 << (i * 2))) != 1612687da132SEmmanuel Grumbach IEEE80211_VHT_MCS_NOT_SUPPORTED) 1613687da132SEmmanuel Grumbach return i + 1; 1614687da132SEmmanuel Grumbach } 1615687da132SEmmanuel Grumbach 1616687da132SEmmanuel Grumbach if (ht_cap->mcs.rx_mask[3]) 1617687da132SEmmanuel Grumbach rx_streams = 4; 1618687da132SEmmanuel Grumbach else if (ht_cap->mcs.rx_mask[2]) 1619687da132SEmmanuel Grumbach rx_streams = 3; 1620687da132SEmmanuel Grumbach else if (ht_cap->mcs.rx_mask[1]) 1621687da132SEmmanuel Grumbach rx_streams = 2; 1622687da132SEmmanuel Grumbach else 1623687da132SEmmanuel Grumbach rx_streams = 1; 1624687da132SEmmanuel Grumbach 1625687da132SEmmanuel Grumbach if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF)) 1626687da132SEmmanuel Grumbach return rx_streams; 1627687da132SEmmanuel Grumbach 1628687da132SEmmanuel Grumbach return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK) 1629687da132SEmmanuel Grumbach >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1; 1630687da132SEmmanuel Grumbach } 1631