Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
f0706e82 JB |
2 | /* |
3 | * Copyright 2002-2005, Instant802 Networks, Inc. | |
4 | * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> | |
d98ad83e | 5 | * Copyright 2013-2014 Intel Mobile Communications GmbH |
dcba665b | 6 | * Copyright (C) 2015 - 2017 Intel Deutschland GmbH |
3a11ce08 | 7 | * Copyright (C) 2018-2021 Intel Corporation |
f0706e82 JB |
8 | */ |
9 | ||
10 | #include <linux/module.h> | |
11 | #include <linux/init.h> | |
888d04df | 12 | #include <linux/etherdevice.h> |
f0706e82 JB |
13 | #include <linux/netdevice.h> |
14 | #include <linux/types.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/skbuff.h> | |
17 | #include <linux/if_arp.h> | |
0d174406 | 18 | #include <linux/timer.h> |
d0709a65 | 19 | #include <linux/rtnetlink.h> |
f0706e82 | 20 | |
484a54c2 | 21 | #include <net/codel.h> |
f0706e82 JB |
22 | #include <net/mac80211.h> |
23 | #include "ieee80211_i.h" | |
24487981 | 24 | #include "driver-ops.h" |
2c8dccc7 | 25 | #include "rate.h" |
f0706e82 | 26 | #include "sta_info.h" |
e9f207f0 | 27 | #include "debugfs_sta.h" |
ee385855 | 28 | #include "mesh.h" |
ce662b44 | 29 | #include "wme.h" |
f0706e82 | 30 | |
d0709a65 JB |
31 | /** |
32 | * DOC: STA information lifetime rules | |
33 | * | |
34 | * STA info structures (&struct sta_info) are managed in a hash table | |
35 | * for faster lookup and a list for iteration. They are managed using | |
36 | * RCU, i.e. access to the list and hash table is protected by RCU. | |
37 | * | |
34e89507 JB |
38 | * Upon allocating a STA info structure with sta_info_alloc(), the caller |
39 | * owns that structure. It must then insert it into the hash table using | |
40 | * either sta_info_insert() or sta_info_insert_rcu(); only in the latter | |
41 | * case (which acquires an rcu read section but must not be called from | |
42 | * within one) will the pointer still be valid after the call. Note that | |
43 | * the caller may not do much with the STA info before inserting it, in | |
44 | * particular, it may not start any mesh peer link management or add | |
45 | * encryption keys. | |
93e5deb1 JB |
46 | * |
47 | * When the insertion fails (sta_info_insert()) returns non-zero), the | |
48 | * structure will have been freed by sta_info_insert()! | |
d0709a65 | 49 | * |
34e89507 | 50 | * Station entries are added by mac80211 when you establish a link with a |
7e189a12 LR |
51 | * peer. This means different things for the different type of interfaces |
52 | * we support. For a regular station this mean we add the AP sta when we | |
25985edc | 53 | * receive an association response from the AP. For IBSS this occurs when |
34e89507 | 54 | * get to know about a peer on the same IBSS. For WDS we add the sta for |
25985edc | 55 | * the peer immediately upon device open. When using AP mode we add stations |
34e89507 | 56 | * for each respective station upon request from userspace through nl80211. |
7e189a12 | 57 | * |
34e89507 JB |
58 | * In order to remove a STA info structure, various sta_info_destroy_*() |
59 | * calls are available. | |
d0709a65 | 60 | * |
34e89507 JB |
61 | * There is no concept of ownership on a STA entry, each structure is |
62 | * owned by the global hash table/list until it is removed. All users of | |
63 | * the structure need to be RCU protected so that the structure won't be | |
64 | * freed before they are done using it. | |
d0709a65 | 65 | */ |
f0706e82 | 66 | |
7bedd0cf JB |
67 | static const struct rhashtable_params sta_rht_params = { |
68 | .nelem_hint = 3, /* start small */ | |
caf22d31 | 69 | .automatic_shrinking = true, |
7bedd0cf | 70 | .head_offset = offsetof(struct sta_info, hash_node), |
ac100ce5 | 71 | .key_offset = offsetof(struct sta_info, addr), |
7bedd0cf | 72 | .key_len = ETH_ALEN, |
ebd82b39 | 73 | .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE, |
7bedd0cf JB |
74 | }; |
75 | ||
4d33960b | 76 | /* Caller must hold local->sta_mtx */ |
be8755e1 MW |
77 | static int sta_info_hash_del(struct ieee80211_local *local, |
78 | struct sta_info *sta) | |
f0706e82 | 79 | { |
83e7e4ce HX |
80 | return rhltable_remove(&local->sta_hash, &sta->hash_node, |
81 | sta_rht_params); | |
f0706e82 JB |
82 | } |
83 | ||
5108ca82 | 84 | static void __cleanup_single_sta(struct sta_info *sta) |
b22cfcfc | 85 | { |
b22cfcfc EP |
86 | int ac, i; |
87 | struct tid_ampdu_tx *tid_tx; | |
88 | struct ieee80211_sub_if_data *sdata = sta->sdata; | |
89 | struct ieee80211_local *local = sdata->local; | |
d012a605 | 90 | struct ps_data *ps; |
b22cfcfc | 91 | |
e3685e03 | 92 | if (test_sta_flag(sta, WLAN_STA_PS_STA) || |
5ac2e350 JB |
93 | test_sta_flag(sta, WLAN_STA_PS_DRIVER) || |
94 | test_sta_flag(sta, WLAN_STA_PS_DELIVER)) { | |
d012a605 MP |
95 | if (sta->sdata->vif.type == NL80211_IFTYPE_AP || |
96 | sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) | |
97 | ps = &sdata->bss->ps; | |
3f52b7e3 MP |
98 | else if (ieee80211_vif_is_mesh(&sdata->vif)) |
99 | ps = &sdata->u.mesh.ps; | |
d012a605 MP |
100 | else |
101 | return; | |
b22cfcfc EP |
102 | |
103 | clear_sta_flag(sta, WLAN_STA_PS_STA); | |
e3685e03 | 104 | clear_sta_flag(sta, WLAN_STA_PS_DRIVER); |
5ac2e350 | 105 | clear_sta_flag(sta, WLAN_STA_PS_DELIVER); |
b22cfcfc | 106 | |
d012a605 | 107 | atomic_dec(&ps->num_sta_ps); |
b22cfcfc EP |
108 | } |
109 | ||
ba8c3d6f FF |
110 | if (sta->sta.txq[0]) { |
111 | for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) { | |
adf8ed01 JB |
112 | struct txq_info *txqi; |
113 | ||
114 | if (!sta->sta.txq[i]) | |
115 | continue; | |
116 | ||
117 | txqi = to_txq_info(sta->sta.txq[i]); | |
ba8c3d6f | 118 | |
fa962b92 | 119 | ieee80211_txq_purge(local, txqi); |
ba8c3d6f FF |
120 | } |
121 | } | |
122 | ||
b22cfcfc EP |
123 | for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { |
124 | local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]); | |
1f98ab7f FF |
125 | ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]); |
126 | ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]); | |
b22cfcfc EP |
127 | } |
128 | ||
45b5028e TP |
129 | if (ieee80211_vif_is_mesh(&sdata->vif)) |
130 | mesh_sta_cleanup(sta); | |
b22cfcfc | 131 | |
5ac2e350 | 132 | cancel_work_sync(&sta->drv_deliver_wk); |
b22cfcfc EP |
133 | |
134 | /* | |
135 | * Destroy aggregation state here. It would be nice to wait for the | |
136 | * driver to finish aggregation stop and then clean up, but for now | |
137 | * drivers have to handle aggregation stop being requested, followed | |
138 | * directly by station destruction. | |
139 | */ | |
5a306f58 | 140 | for (i = 0; i < IEEE80211_NUM_TIDS; i++) { |
661eb381 | 141 | kfree(sta->ampdu_mlme.tid_start_tx[i]); |
b22cfcfc EP |
142 | tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]); |
143 | if (!tid_tx) | |
144 | continue; | |
1f98ab7f | 145 | ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending); |
b22cfcfc EP |
146 | kfree(tid_tx); |
147 | } | |
5108ca82 | 148 | } |
b22cfcfc | 149 | |
5108ca82 JB |
150 | static void cleanup_single_sta(struct sta_info *sta) |
151 | { | |
152 | struct ieee80211_sub_if_data *sdata = sta->sdata; | |
153 | struct ieee80211_local *local = sdata->local; | |
154 | ||
155 | __cleanup_single_sta(sta); | |
b22cfcfc EP |
156 | sta_info_free(local, sta); |
157 | } | |
158 | ||
83e7e4ce HX |
159 | struct rhlist_head *sta_info_hash_lookup(struct ieee80211_local *local, |
160 | const u8 *addr) | |
161 | { | |
162 | return rhltable_lookup(&local->sta_hash, addr, sta_rht_params); | |
163 | } | |
164 | ||
d0709a65 | 165 | /* protected by RCU */ |
abe60632 JB |
166 | struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata, |
167 | const u8 *addr) | |
f0706e82 | 168 | { |
abe60632 | 169 | struct ieee80211_local *local = sdata->local; |
83e7e4ce | 170 | struct rhlist_head *tmp; |
60f4b626 | 171 | struct sta_info *sta; |
f0706e82 | 172 | |
60f4b626 | 173 | rcu_read_lock(); |
83e7e4ce | 174 | for_each_sta_info(local, addr, sta, tmp) { |
60f4b626 JB |
175 | if (sta->sdata == sdata) { |
176 | rcu_read_unlock(); | |
177 | /* this is safe as the caller must already hold | |
178 | * another rcu read section or the mutex | |
179 | */ | |
180 | return sta; | |
181 | } | |
182 | } | |
183 | rcu_read_unlock(); | |
184 | return NULL; | |
43ba7e95 JB |
185 | } |
186 | ||
0e5ded5a FF |
187 | /* |
188 | * Get sta info either from the specified interface | |
189 | * or from one of its vlans | |
190 | */ | |
191 | struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata, | |
192 | const u8 *addr) | |
193 | { | |
194 | struct ieee80211_local *local = sdata->local; | |
83e7e4ce | 195 | struct rhlist_head *tmp; |
0e5ded5a FF |
196 | struct sta_info *sta; |
197 | ||
7bedd0cf | 198 | rcu_read_lock(); |
83e7e4ce | 199 | for_each_sta_info(local, addr, sta, tmp) { |
7bedd0cf JB |
200 | if (sta->sdata == sdata || |
201 | (sta->sdata->bss && sta->sdata->bss == sdata->bss)) { | |
202 | rcu_read_unlock(); | |
203 | /* this is safe as the caller must already hold | |
204 | * another rcu read section or the mutex | |
205 | */ | |
206 | return sta; | |
207 | } | |
0e5ded5a | 208 | } |
7bedd0cf JB |
209 | rcu_read_unlock(); |
210 | return NULL; | |
0e5ded5a FF |
211 | } |
212 | ||
5072f73c THJ |
213 | struct sta_info *sta_info_get_by_addrs(struct ieee80211_local *local, |
214 | const u8 *sta_addr, const u8 *vif_addr) | |
215 | { | |
216 | struct rhlist_head *tmp; | |
217 | struct sta_info *sta; | |
218 | ||
219 | for_each_sta_info(local, sta_addr, sta, tmp) { | |
220 | if (ether_addr_equal(vif_addr, sta->sdata->vif.addr)) | |
221 | return sta; | |
222 | } | |
223 | ||
224 | return NULL; | |
225 | } | |
226 | ||
3b53fde8 JB |
227 | struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata, |
228 | int idx) | |
ee385855 | 229 | { |
3b53fde8 | 230 | struct ieee80211_local *local = sdata->local; |
ee385855 LCC |
231 | struct sta_info *sta; |
232 | int i = 0; | |
233 | ||
8ca47eb9 MB |
234 | list_for_each_entry_rcu(sta, &local->sta_list, list, |
235 | lockdep_is_held(&local->sta_mtx)) { | |
3b53fde8 | 236 | if (sdata != sta->sdata) |
2a8ca29a | 237 | continue; |
ee385855 LCC |
238 | if (i < idx) { |
239 | ++i; | |
240 | continue; | |
ee385855 | 241 | } |
2a8ca29a | 242 | return sta; |
ee385855 | 243 | } |
ee385855 LCC |
244 | |
245 | return NULL; | |
246 | } | |
f0706e82 | 247 | |
246b39e4 JB |
248 | static void sta_info_free_links(struct sta_info *sta) |
249 | { | |
250 | unsigned int link_id; | |
251 | ||
252 | for (link_id = 0; link_id < ARRAY_SIZE(sta->link); link_id++) { | |
253 | if (!sta->link[link_id]) | |
254 | continue; | |
255 | free_percpu(sta->link[link_id]->pcpu_rx_stats); | |
256 | ||
257 | if (sta->link[link_id] != &sta->deflink) | |
258 | kfree(sta->link[link_id]); | |
259 | } | |
260 | } | |
261 | ||
93e5deb1 | 262 | /** |
d9a7ddb0 | 263 | * sta_info_free - free STA |
93e5deb1 | 264 | * |
6ef307bc | 265 | * @local: pointer to the global information |
93e5deb1 JB |
266 | * @sta: STA info to free |
267 | * | |
268 | * This function must undo everything done by sta_info_alloc() | |
d9a7ddb0 JB |
269 | * that may happen before sta_info_insert(). It may only be |
270 | * called when sta_info_insert() has not been attempted (and | |
271 | * if that fails, the station is freed anyway.) | |
93e5deb1 | 272 | */ |
d9a7ddb0 | 273 | void sta_info_free(struct ieee80211_local *local, struct sta_info *sta) |
93e5deb1 | 274 | { |
dcd479e1 JB |
275 | /* |
276 | * If we had used sta_info_pre_move_state() then we might not | |
277 | * have gone through the state transitions down again, so do | |
278 | * it here now (and warn if it's inserted). | |
279 | * | |
280 | * This will clear state such as fast TX/RX that may have been | |
281 | * allocated during state transitions. | |
282 | */ | |
283 | while (sta->sta_state > IEEE80211_STA_NONE) { | |
284 | int ret; | |
285 | ||
286 | WARN_ON_ONCE(test_sta_flag(sta, WLAN_STA_INSERTED)); | |
287 | ||
288 | ret = sta_info_move_state(sta, sta->sta_state - 1); | |
289 | if (WARN_ONCE(ret, "sta_info_move_state() returned %d\n", ret)) | |
290 | break; | |
291 | } | |
292 | ||
889cbb91 | 293 | if (sta->rate_ctrl) |
af65cd96 | 294 | rate_control_free_sta(sta); |
93e5deb1 | 295 | |
bdcbd8e0 | 296 | sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr); |
93e5deb1 | 297 | |
ba8c3d6f FF |
298 | if (sta->sta.txq[0]) |
299 | kfree(to_txq_info(sta->sta.txq[0])); | |
53d04525 | 300 | kfree(rcu_dereference_raw(sta->sta.rates)); |
433f5bc1 JB |
301 | #ifdef CONFIG_MAC80211_MESH |
302 | kfree(sta->mesh); | |
303 | #endif | |
246b39e4 JB |
304 | |
305 | sta_info_free_links(sta); | |
93e5deb1 JB |
306 | kfree(sta); |
307 | } | |
308 | ||
4d33960b | 309 | /* Caller must hold local->sta_mtx */ |
62b14b24 JB |
310 | static int sta_info_hash_add(struct ieee80211_local *local, |
311 | struct sta_info *sta) | |
f0706e82 | 312 | { |
83e7e4ce HX |
313 | return rhltable_insert(&local->sta_hash, &sta->hash_node, |
314 | sta_rht_params); | |
f0706e82 | 315 | } |
f0706e82 | 316 | |
5ac2e350 | 317 | static void sta_deliver_ps_frames(struct work_struct *wk) |
af818581 JB |
318 | { |
319 | struct sta_info *sta; | |
320 | ||
5ac2e350 | 321 | sta = container_of(wk, struct sta_info, drv_deliver_wk); |
af818581 JB |
322 | |
323 | if (sta->dead) | |
324 | return; | |
325 | ||
5ac2e350 JB |
326 | local_bh_disable(); |
327 | if (!test_sta_flag(sta, WLAN_STA_PS_STA)) | |
af818581 | 328 | ieee80211_sta_ps_deliver_wakeup(sta); |
5ac2e350 | 329 | else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) |
af818581 | 330 | ieee80211_sta_ps_deliver_poll_response(sta); |
5ac2e350 | 331 | else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) |
47086fc5 | 332 | ieee80211_sta_ps_deliver_uapsd(sta); |
5ac2e350 | 333 | local_bh_enable(); |
af818581 JB |
334 | } |
335 | ||
af65cd96 JB |
336 | static int sta_prepare_rate_control(struct ieee80211_local *local, |
337 | struct sta_info *sta, gfp_t gfp) | |
338 | { | |
30686bf7 | 339 | if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) |
af65cd96 JB |
340 | return 0; |
341 | ||
889cbb91 | 342 | sta->rate_ctrl = local->rate_ctrl; |
af65cd96 | 343 | sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, |
35c347ac | 344 | sta, gfp); |
889cbb91 | 345 | if (!sta->rate_ctrl_priv) |
af65cd96 | 346 | return -ENOMEM; |
af65cd96 JB |
347 | |
348 | return 0; | |
349 | } | |
350 | ||
246b39e4 JB |
351 | static int sta_info_init_link(struct sta_info *sta, |
352 | unsigned int link_id, | |
353 | struct link_sta_info *link_info, | |
354 | struct ieee80211_link_sta *link_sta, | |
355 | gfp_t gfp) | |
356 | { | |
357 | struct ieee80211_local *local = sta->local; | |
358 | struct ieee80211_hw *hw = &local->hw; | |
359 | int i; | |
360 | ||
361 | link_info->sta = sta; | |
362 | link_info->link_id = link_id; | |
363 | ||
364 | if (ieee80211_hw_check(hw, USES_RSS)) { | |
365 | link_info->pcpu_rx_stats = | |
366 | alloc_percpu_gfp(struct ieee80211_sta_rx_stats, gfp); | |
367 | if (!link_info->pcpu_rx_stats) | |
368 | return -ENOMEM; | |
369 | } | |
370 | ||
371 | sta->link[link_id] = link_info; | |
372 | sta->sta.link[link_id] = link_sta; | |
373 | ||
374 | link_info->rx_stats.last_rx = jiffies; | |
375 | u64_stats_init(&link_info->rx_stats.syncp); | |
376 | ||
377 | ewma_signal_init(&link_info->rx_stats_avg.signal); | |
378 | ewma_avg_signal_init(&link_info->status_stats.avg_ack_signal); | |
379 | for (i = 0; i < ARRAY_SIZE(link_info->rx_stats_avg.chain_signal); i++) | |
380 | ewma_signal_init(&link_info->rx_stats_avg.chain_signal[i]); | |
381 | ||
382 | return 0; | |
383 | } | |
384 | ||
73651ee6 | 385 | struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, |
56544160 | 386 | const u8 *addr, gfp_t gfp) |
f0706e82 | 387 | { |
d0709a65 | 388 | struct ieee80211_local *local = sdata->local; |
ba8c3d6f | 389 | struct ieee80211_hw *hw = &local->hw; |
f0706e82 | 390 | struct sta_info *sta; |
16c5f15c | 391 | int i; |
f0706e82 | 392 | |
ba8c3d6f | 393 | sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp); |
f0706e82 | 394 | if (!sta) |
73651ee6 | 395 | return NULL; |
f0706e82 | 396 | |
246b39e4 JB |
397 | sta->local = local; |
398 | sta->sdata = sdata; | |
399 | ||
400 | if (sta_info_init_link(sta, 0, &sta->deflink, &sta->sta.deflink, gfp)) | |
401 | return NULL; | |
c9c5962b | 402 | |
07346f81 | 403 | spin_lock_init(&sta->lock); |
1d147bfa | 404 | spin_lock_init(&sta->ps_lock); |
5ac2e350 | 405 | INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames); |
67c282c0 | 406 | INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work); |
a93e3644 | 407 | mutex_init(&sta->ampdu_mlme.mtx); |
87f59c70 | 408 | #ifdef CONFIG_MAC80211_MESH |
433f5bc1 JB |
409 | if (ieee80211_vif_is_mesh(&sdata->vif)) { |
410 | sta->mesh = kzalloc(sizeof(*sta->mesh), gfp); | |
411 | if (!sta->mesh) | |
412 | goto free; | |
4c02d62f | 413 | sta->mesh->plink_sta = sta; |
433f5bc1 | 414 | spin_lock_init(&sta->mesh->plink_lock); |
45d33746 | 415 | if (!sdata->u.mesh.user_mpm) |
4c02d62f KC |
416 | timer_setup(&sta->mesh->plink_timer, mesh_plink_timer, |
417 | 0); | |
433f5bc1 JB |
418 | sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE; |
419 | } | |
87f59c70 | 420 | #endif |
07346f81 | 421 | |
ac100ce5 | 422 | memcpy(sta->addr, addr, ETH_ALEN); |
17741cdc | 423 | memcpy(sta->sta.addr, addr, ETH_ALEN); |
480dd46b MA |
424 | sta->sta.max_rx_aggregation_subframes = |
425 | local->hw.max_rx_aggregation_subframes; | |
426 | ||
046d2e7c S |
427 | /* TODO link specific alloc and assignments for MLO Link STA */ |
428 | ||
96fc6efb AW |
429 | /* Extended Key ID needs to install keys for keyid 0 and 1 Rx-only. |
430 | * The Tx path starts to use a key as soon as the key slot ptk_idx | |
431 | * references to is not NULL. To not use the initial Rx-only key | |
432 | * prematurely for Tx initialize ptk_idx to an impossible PTK keyid | |
433 | * which always will refer to a NULL key. | |
434 | */ | |
435 | BUILD_BUG_ON(ARRAY_SIZE(sta->ptk) <= INVALID_PTK_KEYIDX); | |
436 | sta->ptk_idx = INVALID_PTK_KEYIDX; | |
437 | ||
0f9c5a61 | 438 | |
3a11ce08 JB |
439 | ieee80211_init_frag_cache(&sta->frags); |
440 | ||
71ec375c JB |
441 | sta->sta_state = IEEE80211_STA_NONE; |
442 | ||
b6da911b LK |
443 | /* Mark TID as unreserved */ |
444 | sta->reserved_tid = IEEE80211_TID_UNRESERVED; | |
445 | ||
84b00607 | 446 | sta->last_connected = ktime_get_seconds(); |
541a45a1 | 447 | |
ba8c3d6f FF |
448 | if (local->ops->wake_tx_queue) { |
449 | void *txq_data; | |
450 | int size = sizeof(struct txq_info) + | |
451 | ALIGN(hw->txq_data_size, sizeof(void *)); | |
452 | ||
453 | txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp); | |
454 | if (!txq_data) | |
455 | goto free; | |
456 | ||
457 | for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) { | |
458 | struct txq_info *txq = txq_data + i * size; | |
459 | ||
adf8ed01 | 460 | /* might not do anything for the bufferable MMPDU TXQ */ |
fa962b92 | 461 | ieee80211_txq_init(sdata, sta, txq, i); |
ba8c3d6f | 462 | } |
abfbc3af | 463 | } |
f0706e82 | 464 | |
ba8c3d6f FF |
465 | if (sta_prepare_rate_control(local, sta, gfp)) |
466 | goto free_txq; | |
467 | ||
b4809e94 | 468 | |
948d887d JB |
469 | for (i = 0; i < IEEE80211_NUM_ACS; i++) { |
470 | skb_queue_head_init(&sta->ps_tx_buf[i]); | |
471 | skb_queue_head_init(&sta->tx_filtered[i]); | |
2433647b | 472 | init_airtime_info(&sta->airtime[i], &local->airtime[i]); |
948d887d | 473 | } |
73651ee6 | 474 | |
5a306f58 | 475 | for (i = 0; i < IEEE80211_NUM_TIDS; i++) |
4be929be | 476 | sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX); |
cccaec98 | 477 | |
bd718fc1 JB |
478 | for (i = 0; i < NUM_NL80211_BANDS; i++) { |
479 | u32 mandatory = 0; | |
480 | int r; | |
481 | ||
482 | if (!hw->wiphy->bands[i]) | |
483 | continue; | |
484 | ||
485 | switch (i) { | |
486 | case NL80211_BAND_2GHZ: | |
63fa0426 | 487 | case NL80211_BAND_LC: |
bd718fc1 JB |
488 | /* |
489 | * We use both here, even if we cannot really know for | |
490 | * sure the station will support both, but the only use | |
491 | * for this is when we don't know anything yet and send | |
492 | * management frames, and then we'll pick the lowest | |
493 | * possible rate anyway. | |
494 | * If we don't include _G here, we cannot find a rate | |
495 | * in P2P, and thus trigger the WARN_ONCE() in rate.c | |
496 | */ | |
497 | mandatory = IEEE80211_RATE_MANDATORY_B | | |
498 | IEEE80211_RATE_MANDATORY_G; | |
499 | break; | |
500 | case NL80211_BAND_5GHZ: | |
501 | mandatory = IEEE80211_RATE_MANDATORY_A; | |
502 | break; | |
503 | case NL80211_BAND_60GHZ: | |
504 | WARN_ON(1); | |
505 | mandatory = 0; | |
506 | break; | |
507 | } | |
508 | ||
509 | for (r = 0; r < hw->wiphy->bands[i]->n_bitrates; r++) { | |
510 | struct ieee80211_rate *rate; | |
511 | ||
512 | rate = &hw->wiphy->bands[i]->bitrates[r]; | |
513 | ||
514 | if (!(rate->flags & mandatory)) | |
515 | continue; | |
046d2e7c | 516 | sta->sta.deflink.supp_rates[i] |= BIT(r); |
bd718fc1 JB |
517 | } |
518 | } | |
519 | ||
af0ed69b | 520 | sta->sta.smps_mode = IEEE80211_SMPS_OFF; |
687da132 EG |
521 | if (sdata->vif.type == NL80211_IFTYPE_AP || |
522 | sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { | |
21a8e9dd MSS |
523 | struct ieee80211_supported_band *sband; |
524 | u8 smps; | |
525 | ||
526 | sband = ieee80211_get_sband(sdata); | |
527 | if (!sband) | |
528 | goto free_txq; | |
529 | ||
530 | smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> | |
531 | IEEE80211_HT_CAP_SM_PS_SHIFT; | |
687da132 EG |
532 | /* |
533 | * Assume that hostapd advertises our caps in the beacon and | |
534 | * this is the known_smps_mode for a station that just assciated | |
535 | */ | |
536 | switch (smps) { | |
537 | case WLAN_HT_SMPS_CONTROL_DISABLED: | |
538 | sta->known_smps_mode = IEEE80211_SMPS_OFF; | |
539 | break; | |
540 | case WLAN_HT_SMPS_CONTROL_STATIC: | |
541 | sta->known_smps_mode = IEEE80211_SMPS_STATIC; | |
542 | break; | |
543 | case WLAN_HT_SMPS_CONTROL_DYNAMIC: | |
544 | sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC; | |
545 | break; | |
546 | default: | |
547 | WARN_ON(1); | |
548 | } | |
549 | } | |
af0ed69b | 550 | |
6e0456b5 FF |
551 | sta->sta.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA; |
552 | ||
484a54c2 THJ |
553 | sta->cparams.ce_threshold = CODEL_DISABLED_THRESHOLD; |
554 | sta->cparams.target = MS2TIME(20); | |
555 | sta->cparams.interval = MS2TIME(100); | |
556 | sta->cparams.ecn = true; | |
dfcb63ce THJ |
557 | sta->cparams.ce_threshold_selector = 0; |
558 | sta->cparams.ce_threshold_mask = 0; | |
484a54c2 | 559 | |
bdcbd8e0 | 560 | sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr); |
ef04a297 | 561 | |
abfbc3af | 562 | return sta; |
ba8c3d6f FF |
563 | |
564 | free_txq: | |
565 | if (sta->sta.txq[0]) | |
566 | kfree(to_txq_info(sta->sta.txq[0])); | |
567 | free: | |
246b39e4 | 568 | sta_info_free_links(sta); |
433f5bc1 JB |
569 | #ifdef CONFIG_MAC80211_MESH |
570 | kfree(sta->mesh); | |
571 | #endif | |
ba8c3d6f FF |
572 | kfree(sta); |
573 | return NULL; | |
73651ee6 JB |
574 | } |
575 | ||
8c71df7a | 576 | static int sta_info_insert_check(struct sta_info *sta) |
34e89507 | 577 | { |
34e89507 | 578 | struct ieee80211_sub_if_data *sdata = sta->sdata; |
34e89507 | 579 | |
03e4497e JB |
580 | /* |
581 | * Can't be a WARN_ON because it can be triggered through a race: | |
582 | * something inserts a STA (on one CPU) without holding the RTNL | |
583 | * and another CPU turns off the net device. | |
584 | */ | |
8c71df7a GE |
585 | if (unlikely(!ieee80211_sdata_running(sdata))) |
586 | return -ENETDOWN; | |
03e4497e | 587 | |
b203ca39 | 588 | if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) || |
deebea0a | 589 | !is_valid_ether_addr(sta->sta.addr))) |
8c71df7a GE |
590 | return -EINVAL; |
591 | ||
83e7e4ce HX |
592 | /* The RCU read lock is required by rhashtable due to |
593 | * asynchronous resize/rehash. We also require the mutex | |
594 | * for correctness. | |
31104891 JB |
595 | */ |
596 | rcu_read_lock(); | |
597 | lockdep_assert_held(&sdata->local->sta_mtx); | |
598 | if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) && | |
599 | ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) { | |
600 | rcu_read_unlock(); | |
601 | return -ENOTUNIQ; | |
602 | } | |
603 | rcu_read_unlock(); | |
604 | ||
8c71df7a GE |
605 | return 0; |
606 | } | |
607 | ||
f09603a2 JB |
608 | static int sta_info_insert_drv_state(struct ieee80211_local *local, |
609 | struct ieee80211_sub_if_data *sdata, | |
610 | struct sta_info *sta) | |
611 | { | |
612 | enum ieee80211_sta_state state; | |
613 | int err = 0; | |
614 | ||
615 | for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) { | |
616 | err = drv_sta_state(local, sdata, sta, state, state + 1); | |
617 | if (err) | |
618 | break; | |
619 | } | |
620 | ||
621 | if (!err) { | |
a4ec45a4 JB |
622 | /* |
623 | * Drivers using legacy sta_add/sta_remove callbacks only | |
624 | * get uploaded set to true after sta_add is called. | |
625 | */ | |
626 | if (!local->ops->sta_add) | |
627 | sta->uploaded = true; | |
f09603a2 JB |
628 | return 0; |
629 | } | |
630 | ||
631 | if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { | |
bdcbd8e0 JB |
632 | sdata_info(sdata, |
633 | "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n", | |
634 | sta->sta.addr, state + 1, err); | |
f09603a2 JB |
635 | err = 0; |
636 | } | |
637 | ||
638 | /* unwind on error */ | |
639 | for (; state > IEEE80211_STA_NOTEXIST; state--) | |
640 | WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1)); | |
641 | ||
642 | return err; | |
643 | } | |
644 | ||
d405fd8c GG |
645 | static void |
646 | ieee80211_recalc_p2p_go_ps_allowed(struct ieee80211_sub_if_data *sdata) | |
647 | { | |
648 | struct ieee80211_local *local = sdata->local; | |
649 | bool allow_p2p_go_ps = sdata->vif.p2p; | |
650 | struct sta_info *sta; | |
651 | ||
652 | rcu_read_lock(); | |
653 | list_for_each_entry_rcu(sta, &local->sta_list, list) { | |
654 | if (sdata != sta->sdata || | |
655 | !test_sta_flag(sta, WLAN_STA_ASSOC)) | |
656 | continue; | |
657 | if (!sta->sta.support_p2p_ps) { | |
658 | allow_p2p_go_ps = false; | |
659 | break; | |
660 | } | |
661 | } | |
662 | rcu_read_unlock(); | |
663 | ||
664 | if (allow_p2p_go_ps != sdata->vif.bss_conf.allow_p2p_go_ps) { | |
665 | sdata->vif.bss_conf.allow_p2p_go_ps = allow_p2p_go_ps; | |
7b7090b4 | 666 | ieee80211_link_info_change_notify(sdata, 0, BSS_CHANGED_P2P_PS); |
d405fd8c GG |
667 | } |
668 | } | |
669 | ||
8c71df7a GE |
670 | /* |
671 | * should be called with sta_mtx locked | |
672 | * this function replaces the mutex lock | |
673 | * with a RCU lock | |
674 | */ | |
4d33960b | 675 | static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU) |
8c71df7a GE |
676 | { |
677 | struct ieee80211_local *local = sta->local; | |
678 | struct ieee80211_sub_if_data *sdata = sta->sdata; | |
0c2e3842 | 679 | struct station_info *sinfo = NULL; |
8c71df7a GE |
680 | int err = 0; |
681 | ||
682 | lockdep_assert_held(&local->sta_mtx); | |
34e89507 | 683 | |
7852e361 JB |
684 | /* check if STA exists already */ |
685 | if (sta_info_get_bss(sdata, sta->sta.addr)) { | |
686 | err = -EEXIST; | |
8f9dcc29 | 687 | goto out_cleanup; |
4d33960b | 688 | } |
32bfd35d | 689 | |
0c2e3842 KV |
690 | sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL); |
691 | if (!sinfo) { | |
692 | err = -ENOMEM; | |
8f9dcc29 | 693 | goto out_cleanup; |
0c2e3842 KV |
694 | } |
695 | ||
7852e361 JB |
696 | local->num_sta++; |
697 | local->sta_generation++; | |
698 | smp_mb(); | |
4d33960b | 699 | |
5108ca82 JB |
700 | /* simplify things and don't accept BA sessions yet */ |
701 | set_sta_flag(sta, WLAN_STA_BLOCK_BA); | |
702 | ||
7852e361 | 703 | /* make the station visible */ |
62b14b24 JB |
704 | err = sta_info_hash_add(local, sta); |
705 | if (err) | |
706 | goto out_drop_sta; | |
83d5cc01 | 707 | |
2bad7748 | 708 | list_add_tail_rcu(&sta->list, &local->sta_list); |
4d33960b | 709 | |
4dde3c36 MG |
710 | /* update channel context before notifying the driver about state |
711 | * change, this enables driver using the updated channel context right away. | |
712 | */ | |
713 | if (sta->sta_state >= IEEE80211_STA_ASSOC) { | |
714 | ieee80211_recalc_min_chandef(sta->sdata); | |
715 | if (!sta->sta.support_p2p_ps) | |
716 | ieee80211_recalc_p2p_go_ps_allowed(sta->sdata); | |
717 | } | |
718 | ||
5108ca82 JB |
719 | /* notify driver */ |
720 | err = sta_info_insert_drv_state(local, sdata, sta); | |
721 | if (err) | |
722 | goto out_remove; | |
723 | ||
7852e361 | 724 | set_sta_flag(sta, WLAN_STA_INSERTED); |
d405fd8c | 725 | |
5108ca82 JB |
726 | /* accept BA sessions now */ |
727 | clear_sta_flag(sta, WLAN_STA_BLOCK_BA); | |
4d33960b | 728 | |
7852e361 JB |
729 | ieee80211_sta_debugfs_add(sta); |
730 | rate_control_add_sta_debugfs(sta); | |
4d33960b | 731 | |
0ef049dc AB |
732 | sinfo->generation = local->sta_generation; |
733 | cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL); | |
734 | kfree(sinfo); | |
d0709a65 | 735 | |
bdcbd8e0 | 736 | sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr); |
f0706e82 | 737 | |
34e89507 JB |
738 | /* move reference to rcu-protected */ |
739 | rcu_read_lock(); | |
740 | mutex_unlock(&local->sta_mtx); | |
e9f207f0 | 741 | |
73651ee6 JB |
742 | if (ieee80211_vif_is_mesh(&sdata->vif)) |
743 | mesh_accept_plinks_update(sdata); | |
744 | ||
8c71df7a | 745 | return 0; |
5108ca82 JB |
746 | out_remove: |
747 | sta_info_hash_del(local, sta); | |
748 | list_del_rcu(&sta->list); | |
62b14b24 | 749 | out_drop_sta: |
5108ca82 JB |
750 | local->num_sta--; |
751 | synchronize_net(); | |
8f9dcc29 | 752 | out_cleanup: |
7bc40aed | 753 | cleanup_single_sta(sta); |
4d33960b | 754 | mutex_unlock(&local->sta_mtx); |
ea32f065 | 755 | kfree(sinfo); |
4d33960b JB |
756 | rcu_read_lock(); |
757 | return err; | |
8c71df7a GE |
758 | } |
759 | ||
760 | int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) | |
761 | { | |
762 | struct ieee80211_local *local = sta->local; | |
308f7fcf | 763 | int err; |
8c71df7a | 764 | |
4d33960b JB |
765 | might_sleep(); |
766 | ||
31104891 JB |
767 | mutex_lock(&local->sta_mtx); |
768 | ||
8c71df7a GE |
769 | err = sta_info_insert_check(sta); |
770 | if (err) { | |
7bc40aed | 771 | sta_info_free(local, sta); |
31104891 | 772 | mutex_unlock(&local->sta_mtx); |
8c71df7a | 773 | rcu_read_lock(); |
7bc40aed | 774 | return err; |
8c71df7a GE |
775 | } |
776 | ||
7bc40aed | 777 | return sta_info_insert_finish(sta); |
f0706e82 JB |
778 | } |
779 | ||
34e89507 JB |
780 | int sta_info_insert(struct sta_info *sta) |
781 | { | |
782 | int err = sta_info_insert_rcu(sta); | |
783 | ||
784 | rcu_read_unlock(); | |
785 | ||
786 | return err; | |
787 | } | |
788 | ||
d012a605 | 789 | static inline void __bss_tim_set(u8 *tim, u16 id) |
004c872e JB |
790 | { |
791 | /* | |
792 | * This format has been mandated by the IEEE specifications, | |
793 | * so this line may not be changed to use the __set_bit() format. | |
794 | */ | |
d012a605 | 795 | tim[id / 8] |= (1 << (id % 8)); |
004c872e JB |
796 | } |
797 | ||
d012a605 | 798 | static inline void __bss_tim_clear(u8 *tim, u16 id) |
004c872e JB |
799 | { |
800 | /* | |
801 | * This format has been mandated by the IEEE specifications, | |
802 | * so this line may not be changed to use the __clear_bit() format. | |
803 | */ | |
d012a605 | 804 | tim[id / 8] &= ~(1 << (id % 8)); |
004c872e JB |
805 | } |
806 | ||
3d5839b6 IP |
807 | static inline bool __bss_tim_get(u8 *tim, u16 id) |
808 | { | |
809 | /* | |
810 | * This format has been mandated by the IEEE specifications, | |
811 | * so this line may not be changed to use the test_bit() format. | |
812 | */ | |
813 | return tim[id / 8] & (1 << (id % 8)); | |
814 | } | |
815 | ||
948d887d | 816 | static unsigned long ieee80211_tids_for_ac(int ac) |
004c872e | 817 | { |
948d887d JB |
818 | /* If we ever support TIDs > 7, this obviously needs to be adjusted */ |
819 | switch (ac) { | |
820 | case IEEE80211_AC_VO: | |
821 | return BIT(6) | BIT(7); | |
822 | case IEEE80211_AC_VI: | |
823 | return BIT(4) | BIT(5); | |
824 | case IEEE80211_AC_BE: | |
825 | return BIT(0) | BIT(3); | |
826 | case IEEE80211_AC_BK: | |
827 | return BIT(1) | BIT(2); | |
828 | default: | |
829 | WARN_ON(1); | |
830 | return 0; | |
d0709a65 | 831 | } |
004c872e JB |
832 | } |
833 | ||
9b7a86f3 | 834 | static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending) |
004c872e | 835 | { |
c868cb35 | 836 | struct ieee80211_local *local = sta->local; |
d012a605 | 837 | struct ps_data *ps; |
948d887d JB |
838 | bool indicate_tim = false; |
839 | u8 ignore_for_tim = sta->sta.uapsd_queues; | |
840 | int ac; | |
a69bd8e6 | 841 | u16 id = sta->sta.aid; |
d012a605 MP |
842 | |
843 | if (sta->sdata->vif.type == NL80211_IFTYPE_AP || | |
844 | sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { | |
845 | if (WARN_ON_ONCE(!sta->sdata->bss)) | |
846 | return; | |
004c872e | 847 | |
d012a605 | 848 | ps = &sta->sdata->bss->ps; |
3f52b7e3 MP |
849 | #ifdef CONFIG_MAC80211_MESH |
850 | } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) { | |
851 | ps = &sta->sdata->u.mesh.ps; | |
3f52b7e3 | 852 | #endif |
d012a605 | 853 | } else { |
c868cb35 | 854 | return; |
d012a605 | 855 | } |
3e122be0 | 856 | |
c868cb35 | 857 | /* No need to do anything if the driver does all */ |
d98937f4 | 858 | if (ieee80211_hw_check(&local->hw, AP_LINK_PS) && !local->ops->set_tim) |
c868cb35 | 859 | return; |
004c872e | 860 | |
c868cb35 JB |
861 | if (sta->dead) |
862 | goto done; | |
3e122be0 | 863 | |
948d887d JB |
864 | /* |
865 | * If all ACs are delivery-enabled then we should build | |
866 | * the TIM bit for all ACs anyway; if only some are then | |
867 | * we ignore those and build the TIM bit using only the | |
868 | * non-enabled ones. | |
869 | */ | |
870 | if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1) | |
871 | ignore_for_tim = 0; | |
872 | ||
9b7a86f3 JB |
873 | if (ignore_pending) |
874 | ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1; | |
875 | ||
948d887d JB |
876 | for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { |
877 | unsigned long tids; | |
3e122be0 | 878 | |
f438ceb8 | 879 | if (ignore_for_tim & ieee80211_ac_to_qos_mask[ac]) |
948d887d JB |
880 | continue; |
881 | ||
882 | indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) || | |
883 | !skb_queue_empty(&sta->ps_tx_buf[ac]); | |
884 | if (indicate_tim) | |
885 | break; | |
3e122be0 | 886 | |
948d887d JB |
887 | tids = ieee80211_tids_for_ac(ac); |
888 | ||
889 | indicate_tim |= | |
890 | sta->driver_buffered_tids & tids; | |
ba8c3d6f FF |
891 | indicate_tim |= |
892 | sta->txq_buffered_tids & tids; | |
d0709a65 | 893 | } |
004c872e | 894 | |
c868cb35 | 895 | done: |
65f704a5 | 896 | spin_lock_bh(&local->tim_lock); |
004c872e | 897 | |
3d5839b6 IP |
898 | if (indicate_tim == __bss_tim_get(ps->tim, id)) |
899 | goto out_unlock; | |
900 | ||
948d887d | 901 | if (indicate_tim) |
d012a605 | 902 | __bss_tim_set(ps->tim, id); |
c868cb35 | 903 | else |
d012a605 | 904 | __bss_tim_clear(ps->tim, id); |
004c872e | 905 | |
9b7a86f3 | 906 | if (local->ops->set_tim && !WARN_ON(sta->dead)) { |
c868cb35 | 907 | local->tim_in_locked_section = true; |
948d887d | 908 | drv_set_tim(local, &sta->sta, indicate_tim); |
c868cb35 JB |
909 | local->tim_in_locked_section = false; |
910 | } | |
3e122be0 | 911 | |
3d5839b6 | 912 | out_unlock: |
65f704a5 | 913 | spin_unlock_bh(&local->tim_lock); |
004c872e JB |
914 | } |
915 | ||
9b7a86f3 JB |
916 | void sta_info_recalc_tim(struct sta_info *sta) |
917 | { | |
918 | __sta_info_recalc_tim(sta, false); | |
919 | } | |
920 | ||
cd0b8d89 | 921 | static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb) |
f0706e82 | 922 | { |
e039fa4a | 923 | struct ieee80211_tx_info *info; |
f0706e82 JB |
924 | int timeout; |
925 | ||
926 | if (!skb) | |
cd0b8d89 | 927 | return false; |
f0706e82 | 928 | |
e039fa4a | 929 | info = IEEE80211_SKB_CB(skb); |
f0706e82 JB |
930 | |
931 | /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ | |
57c4d7b4 JB |
932 | timeout = (sta->listen_interval * |
933 | sta->sdata->vif.bss_conf.beacon_int * | |
934 | 32 / 15625) * HZ; | |
f0706e82 JB |
935 | if (timeout < STA_TX_BUFFER_EXPIRE) |
936 | timeout = STA_TX_BUFFER_EXPIRE; | |
e039fa4a | 937 | return time_after(jiffies, info->control.jiffies + timeout); |
f0706e82 JB |
938 | } |
939 | ||
940 | ||
948d887d JB |
941 | static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local, |
942 | struct sta_info *sta, int ac) | |
f0706e82 JB |
943 | { |
944 | unsigned long flags; | |
945 | struct sk_buff *skb; | |
946 | ||
60750397 JB |
947 | /* |
948 | * First check for frames that should expire on the filtered | |
949 | * queue. Frames here were rejected by the driver and are on | |
950 | * a separate queue to avoid reordering with normal PS-buffered | |
951 | * frames. They also aren't accounted for right now in the | |
952 | * total_ps_buffered counter. | |
953 | */ | |
954 | for (;;) { | |
948d887d JB |
955 | spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); |
956 | skb = skb_peek(&sta->tx_filtered[ac]); | |
60750397 | 957 | if (sta_info_buffer_expired(sta, skb)) |
948d887d | 958 | skb = __skb_dequeue(&sta->tx_filtered[ac]); |
60750397 JB |
959 | else |
960 | skb = NULL; | |
948d887d | 961 | spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); |
60750397 JB |
962 | |
963 | /* | |
964 | * Frames are queued in order, so if this one | |
965 | * hasn't expired yet we can stop testing. If | |
966 | * we actually reached the end of the queue we | |
967 | * also need to stop, of course. | |
968 | */ | |
969 | if (!skb) | |
970 | break; | |
d4fa14cd | 971 | ieee80211_free_txskb(&local->hw, skb); |
60750397 JB |
972 | } |
973 | ||
974 | /* | |
975 | * Now also check the normal PS-buffered queue, this will | |
976 | * only find something if the filtered queue was emptied | |
977 | * since the filtered frames are all before the normal PS | |
978 | * buffered frames. | |
979 | */ | |
f0706e82 | 980 | for (;;) { |
948d887d JB |
981 | spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); |
982 | skb = skb_peek(&sta->ps_tx_buf[ac]); | |
57c4d7b4 | 983 | if (sta_info_buffer_expired(sta, skb)) |
948d887d | 984 | skb = __skb_dequeue(&sta->ps_tx_buf[ac]); |
836341a7 | 985 | else |
f0706e82 | 986 | skb = NULL; |
948d887d | 987 | spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); |
f0706e82 | 988 | |
60750397 JB |
989 | /* |
990 | * frames are queued in order, so if this one | |
991 | * hasn't expired yet (or we reached the end of | |
992 | * the queue) we can stop testing | |
993 | */ | |
836341a7 | 994 | if (!skb) |
f0706e82 | 995 | break; |
836341a7 | 996 | |
836341a7 | 997 | local->total_ps_buffered--; |
bdcbd8e0 JB |
998 | ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n", |
999 | sta->sta.addr); | |
d4fa14cd | 1000 | ieee80211_free_txskb(&local->hw, skb); |
f0706e82 | 1001 | } |
3393a608 | 1002 | |
60750397 JB |
1003 | /* |
1004 | * Finally, recalculate the TIM bit for this station -- it might | |
1005 | * now be clear because the station was too slow to retrieve its | |
1006 | * frames. | |
1007 | */ | |
1008 | sta_info_recalc_tim(sta); | |
1009 | ||
1010 | /* | |
1011 | * Return whether there are any frames still buffered, this is | |
1012 | * used to check whether the cleanup timer still needs to run, | |
1013 | * if there are no frames we don't need to rearm the timer. | |
1014 | */ | |
948d887d JB |
1015 | return !(skb_queue_empty(&sta->ps_tx_buf[ac]) && |
1016 | skb_queue_empty(&sta->tx_filtered[ac])); | |
1017 | } | |
1018 | ||
1019 | static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local, | |
1020 | struct sta_info *sta) | |
1021 | { | |
1022 | bool have_buffered = false; | |
1023 | int ac; | |
1024 | ||
3f52b7e3 MP |
1025 | /* This is only necessary for stations on BSS/MBSS interfaces */ |
1026 | if (!sta->sdata->bss && | |
1027 | !ieee80211_vif_is_mesh(&sta->sdata->vif)) | |
948d887d JB |
1028 | return false; |
1029 | ||
1030 | for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) | |
1031 | have_buffered |= | |
1032 | sta_info_cleanup_expire_buffered_ac(local, sta, ac); | |
1033 | ||
1034 | return have_buffered; | |
f0706e82 JB |
1035 | } |
1036 | ||
d778207b | 1037 | static int __must_check __sta_info_destroy_part1(struct sta_info *sta) |
f0706e82 | 1038 | { |
34e89507 JB |
1039 | struct ieee80211_local *local; |
1040 | struct ieee80211_sub_if_data *sdata; | |
6d10e46b | 1041 | int ret; |
f0706e82 | 1042 | |
34e89507 | 1043 | might_sleep(); |
f0706e82 | 1044 | |
34e89507 JB |
1045 | if (!sta) |
1046 | return -ENOENT; | |
5bb644a0 | 1047 | |
34e89507 JB |
1048 | local = sta->local; |
1049 | sdata = sta->sdata; | |
f0706e82 | 1050 | |
83d5cc01 JB |
1051 | lockdep_assert_held(&local->sta_mtx); |
1052 | ||
098a6070 JB |
1053 | /* |
1054 | * Before removing the station from the driver and | |
1055 | * rate control, it might still start new aggregation | |
1056 | * sessions -- block that to make sure the tear-down | |
1057 | * will be sufficient. | |
1058 | */ | |
c2c98fde | 1059 | set_sta_flag(sta, WLAN_STA_BLOCK_BA); |
c82c4a80 | 1060 | ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA); |
098a6070 | 1061 | |
f59374eb SS |
1062 | /* |
1063 | * Before removing the station from the driver there might be pending | |
1064 | * rx frames on RSS queues sent prior to the disassociation - wait for | |
1065 | * all such frames to be processed. | |
1066 | */ | |
1067 | drv_sync_rx_queues(local, sta); | |
1068 | ||
34e89507 | 1069 | ret = sta_info_hash_del(local, sta); |
b01711be | 1070 | if (WARN_ON(ret)) |
34e89507 JB |
1071 | return ret; |
1072 | ||
a7a6bdd0 AN |
1073 | /* |
1074 | * for TDLS peers, make sure to return to the base channel before | |
1075 | * removal. | |
1076 | */ | |
1077 | if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) { | |
1078 | drv_tdls_cancel_channel_switch(local, sdata, &sta->sta); | |
1079 | clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL); | |
1080 | } | |
1081 | ||
794454ce | 1082 | list_del_rcu(&sta->list); |
ef044763 | 1083 | sta->removed = true; |
4d33960b | 1084 | |
6a9d1b91 JB |
1085 | drv_sta_pre_rcu_remove(local, sta->sdata, sta); |
1086 | ||
a710c816 JB |
1087 | if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN && |
1088 | rcu_access_pointer(sdata->u.vlan.sta) == sta) | |
1089 | RCU_INIT_POINTER(sdata->u.vlan.sta, NULL); | |
1090 | ||
d778207b JB |
1091 | return 0; |
1092 | } | |
1093 | ||
1094 | static void __sta_info_destroy_part2(struct sta_info *sta) | |
1095 | { | |
1096 | struct ieee80211_local *local = sta->local; | |
1097 | struct ieee80211_sub_if_data *sdata = sta->sdata; | |
0ef049dc | 1098 | struct station_info *sinfo; |
d778207b JB |
1099 | int ret; |
1100 | ||
1101 | /* | |
1102 | * NOTE: This assumes at least synchronize_net() was done | |
1103 | * after _part1 and before _part2! | |
1104 | */ | |
1105 | ||
1106 | might_sleep(); | |
1107 | lockdep_assert_held(&local->sta_mtx); | |
1108 | ||
5981fe5b | 1109 | if (sta->sta_state == IEEE80211_STA_AUTHORIZED) { |
b16798f5 JB |
1110 | ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC); |
1111 | WARN_ON_ONCE(ret); | |
1112 | } | |
1113 | ||
c8782078 | 1114 | /* now keys can no longer be reached */ |
6d10e46b | 1115 | ieee80211_free_sta_keys(local, sta); |
34e89507 | 1116 | |
9b7a86f3 JB |
1117 | /* disable TIM bit - last chance to tell driver */ |
1118 | __sta_info_recalc_tim(sta, true); | |
1119 | ||
34e89507 JB |
1120 | sta->dead = true; |
1121 | ||
34e89507 JB |
1122 | local->num_sta--; |
1123 | local->sta_generation++; | |
1124 | ||
83d5cc01 | 1125 | while (sta->sta_state > IEEE80211_STA_NONE) { |
f09603a2 JB |
1126 | ret = sta_info_move_state(sta, sta->sta_state - 1); |
1127 | if (ret) { | |
83d5cc01 JB |
1128 | WARN_ON_ONCE(1); |
1129 | break; | |
1130 | } | |
1131 | } | |
d9a7ddb0 | 1132 | |
f09603a2 | 1133 | if (sta->uploaded) { |
f09603a2 JB |
1134 | ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE, |
1135 | IEEE80211_STA_NOTEXIST); | |
1136 | WARN_ON_ONCE(ret != 0); | |
1137 | } | |
34e89507 | 1138 | |
bdcbd8e0 JB |
1139 | sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr); |
1140 | ||
0ef049dc AB |
1141 | sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL); |
1142 | if (sinfo) | |
0fdf1493 | 1143 | sta_set_sinfo(sta, sinfo, true); |
0ef049dc AB |
1144 | cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL); |
1145 | kfree(sinfo); | |
ec15e68b | 1146 | |
34e89507 JB |
1147 | ieee80211_sta_debugfs_remove(sta); |
1148 | ||
3a11ce08 JB |
1149 | ieee80211_destroy_frag_cache(&sta->frags); |
1150 | ||
d34ba216 | 1151 | cleanup_single_sta(sta); |
d778207b JB |
1152 | } |
1153 | ||
1154 | int __must_check __sta_info_destroy(struct sta_info *sta) | |
1155 | { | |
1156 | int err = __sta_info_destroy_part1(sta); | |
1157 | ||
1158 | if (err) | |
1159 | return err; | |
1160 | ||
1161 | synchronize_net(); | |
1162 | ||
1163 | __sta_info_destroy_part2(sta); | |
34e89507 JB |
1164 | |
1165 | return 0; | |
4d6141c3 JS |
1166 | } |
1167 | ||
34e89507 | 1168 | int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr) |
4d6141c3 | 1169 | { |
34e89507 JB |
1170 | struct sta_info *sta; |
1171 | int ret; | |
4d6141c3 | 1172 | |
34e89507 | 1173 | mutex_lock(&sdata->local->sta_mtx); |
7852e361 | 1174 | sta = sta_info_get(sdata, addr); |
34e89507 JB |
1175 | ret = __sta_info_destroy(sta); |
1176 | mutex_unlock(&sdata->local->sta_mtx); | |
4d6141c3 JS |
1177 | |
1178 | return ret; | |
1179 | } | |
1180 | ||
34e89507 JB |
1181 | int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata, |
1182 | const u8 *addr) | |
e9f207f0 | 1183 | { |
34e89507 JB |
1184 | struct sta_info *sta; |
1185 | int ret; | |
e9f207f0 | 1186 | |
34e89507 | 1187 | mutex_lock(&sdata->local->sta_mtx); |
7852e361 | 1188 | sta = sta_info_get_bss(sdata, addr); |
34e89507 JB |
1189 | ret = __sta_info_destroy(sta); |
1190 | mutex_unlock(&sdata->local->sta_mtx); | |
d0709a65 | 1191 | |
34e89507 JB |
1192 | return ret; |
1193 | } | |
e9f207f0 | 1194 | |
34f11cd3 | 1195 | static void sta_info_cleanup(struct timer_list *t) |
34e89507 | 1196 | { |
34f11cd3 | 1197 | struct ieee80211_local *local = from_timer(local, t, sta_cleanup); |
34e89507 | 1198 | struct sta_info *sta; |
3393a608 | 1199 | bool timer_needed = false; |
34e89507 JB |
1200 | |
1201 | rcu_read_lock(); | |
1202 | list_for_each_entry_rcu(sta, &local->sta_list, list) | |
3393a608 JO |
1203 | if (sta_info_cleanup_expire_buffered(local, sta)) |
1204 | timer_needed = true; | |
34e89507 | 1205 | rcu_read_unlock(); |
e9f207f0 | 1206 | |
34e89507 JB |
1207 | if (local->quiescing) |
1208 | return; | |
d0709a65 | 1209 | |
3393a608 JO |
1210 | if (!timer_needed) |
1211 | return; | |
1212 | ||
26d59535 JB |
1213 | mod_timer(&local->sta_cleanup, |
1214 | round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); | |
e9f207f0 | 1215 | } |
e9f207f0 | 1216 | |
7bedd0cf JB |
1217 | int sta_info_init(struct ieee80211_local *local) |
1218 | { | |
1219 | int err; | |
1220 | ||
83e7e4ce | 1221 | err = rhltable_init(&local->sta_hash, &sta_rht_params); |
7bedd0cf JB |
1222 | if (err) |
1223 | return err; | |
1224 | ||
4d33960b | 1225 | spin_lock_init(&local->tim_lock); |
34e89507 | 1226 | mutex_init(&local->sta_mtx); |
f0706e82 | 1227 | INIT_LIST_HEAD(&local->sta_list); |
f0706e82 | 1228 | |
34f11cd3 | 1229 | timer_setup(&local->sta_cleanup, sta_info_cleanup, 0); |
7bedd0cf | 1230 | return 0; |
f0706e82 JB |
1231 | } |
1232 | ||
1233 | void sta_info_stop(struct ieee80211_local *local) | |
1234 | { | |
a56f992c | 1235 | del_timer_sync(&local->sta_cleanup); |
83e7e4ce | 1236 | rhltable_destroy(&local->sta_hash); |
f0706e82 JB |
1237 | } |
1238 | ||
051007d9 | 1239 | |
e716251d | 1240 | int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans) |
f0706e82 | 1241 | { |
b998e8bb | 1242 | struct ieee80211_local *local = sdata->local; |
f0706e82 | 1243 | struct sta_info *sta, *tmp; |
d778207b | 1244 | LIST_HEAD(free_list); |
44213b5e | 1245 | int ret = 0; |
f0706e82 | 1246 | |
d0709a65 | 1247 | might_sleep(); |
be8755e1 | 1248 | |
e716251d JB |
1249 | WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP); |
1250 | WARN_ON(vlans && !sdata->bss); | |
1251 | ||
34e89507 | 1252 | mutex_lock(&local->sta_mtx); |
d0709a65 | 1253 | list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { |
e716251d JB |
1254 | if (sdata == sta->sdata || |
1255 | (vlans && sdata->bss == sta->sdata->bss)) { | |
d778207b JB |
1256 | if (!WARN_ON(__sta_info_destroy_part1(sta))) |
1257 | list_add(&sta->free_list, &free_list); | |
34316837 JB |
1258 | ret++; |
1259 | } | |
be8755e1 | 1260 | } |
d778207b JB |
1261 | |
1262 | if (!list_empty(&free_list)) { | |
1263 | synchronize_net(); | |
1264 | list_for_each_entry_safe(sta, tmp, &free_list, free_list) | |
1265 | __sta_info_destroy_part2(sta); | |
1266 | } | |
34e89507 | 1267 | mutex_unlock(&local->sta_mtx); |
44213b5e | 1268 | |
051007d9 JB |
1269 | return ret; |
1270 | } | |
1271 | ||
24723d1b JB |
1272 | void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, |
1273 | unsigned long exp_time) | |
1274 | { | |
1275 | struct ieee80211_local *local = sdata->local; | |
1276 | struct sta_info *sta, *tmp; | |
24723d1b | 1277 | |
34e89507 | 1278 | mutex_lock(&local->sta_mtx); |
e46a2cf9 MSS |
1279 | |
1280 | list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { | |
b8da6b6a JB |
1281 | unsigned long last_active = ieee80211_sta_last_active(sta); |
1282 | ||
ec2b774e ML |
1283 | if (sdata != sta->sdata) |
1284 | continue; | |
1285 | ||
b8da6b6a | 1286 | if (time_is_before_jiffies(last_active + exp_time)) { |
eea57d42 MSS |
1287 | sta_dbg(sta->sdata, "expiring inactive STA %pM\n", |
1288 | sta->sta.addr); | |
3f52b7e3 MP |
1289 | |
1290 | if (ieee80211_vif_is_mesh(&sdata->vif) && | |
1291 | test_sta_flag(sta, WLAN_STA_PS_STA)) | |
1292 | atomic_dec(&sdata->u.mesh.ps.num_sta_ps); | |
1293 | ||
34e89507 | 1294 | WARN_ON(__sta_info_destroy(sta)); |
24723d1b | 1295 | } |
e46a2cf9 MSS |
1296 | } |
1297 | ||
34e89507 | 1298 | mutex_unlock(&local->sta_mtx); |
24723d1b | 1299 | } |
17741cdc | 1300 | |
686b9cb9 | 1301 | struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, |
7bedd0cf JB |
1302 | const u8 *addr, |
1303 | const u8 *localaddr) | |
17741cdc | 1304 | { |
7bedd0cf | 1305 | struct ieee80211_local *local = hw_to_local(hw); |
83e7e4ce | 1306 | struct rhlist_head *tmp; |
7bedd0cf | 1307 | struct sta_info *sta; |
17741cdc | 1308 | |
686b9cb9 BG |
1309 | /* |
1310 | * Just return a random station if localaddr is NULL | |
1311 | * ... first in list. | |
1312 | */ | |
83e7e4ce | 1313 | for_each_sta_info(local, addr, sta, tmp) { |
686b9cb9 | 1314 | if (localaddr && |
b203ca39 | 1315 | !ether_addr_equal(sta->sdata->vif.addr, localaddr)) |
686b9cb9 | 1316 | continue; |
f7c65594 JB |
1317 | if (!sta->uploaded) |
1318 | return NULL; | |
abe60632 | 1319 | return &sta->sta; |
f7c65594 JB |
1320 | } |
1321 | ||
abe60632 | 1322 | return NULL; |
17741cdc | 1323 | } |
686b9cb9 | 1324 | EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr); |
5ed176e1 JB |
1325 | |
1326 | struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif, | |
1327 | const u8 *addr) | |
1328 | { | |
f7c65594 | 1329 | struct sta_info *sta; |
5ed176e1 JB |
1330 | |
1331 | if (!vif) | |
1332 | return NULL; | |
1333 | ||
f7c65594 JB |
1334 | sta = sta_info_get_bss(vif_to_sdata(vif), addr); |
1335 | if (!sta) | |
1336 | return NULL; | |
1337 | ||
1338 | if (!sta->uploaded) | |
1339 | return NULL; | |
5ed176e1 | 1340 | |
f7c65594 | 1341 | return &sta->sta; |
5ed176e1 | 1342 | } |
17741cdc | 1343 | EXPORT_SYMBOL(ieee80211_find_sta); |
af818581 | 1344 | |
e3685e03 JB |
1345 | /* powersave support code */ |
1346 | void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) | |
50a9432d | 1347 | { |
608383bf | 1348 | struct ieee80211_sub_if_data *sdata = sta->sdata; |
e3685e03 JB |
1349 | struct ieee80211_local *local = sdata->local; |
1350 | struct sk_buff_head pending; | |
ba8c3d6f | 1351 | int filtered = 0, buffered = 0, ac, i; |
e3685e03 | 1352 | unsigned long flags; |
d012a605 MP |
1353 | struct ps_data *ps; |
1354 | ||
3918edb0 FF |
1355 | if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) |
1356 | sdata = container_of(sdata->bss, struct ieee80211_sub_if_data, | |
1357 | u.ap); | |
1358 | ||
1359 | if (sdata->vif.type == NL80211_IFTYPE_AP) | |
d012a605 | 1360 | ps = &sdata->bss->ps; |
3f52b7e3 MP |
1361 | else if (ieee80211_vif_is_mesh(&sdata->vif)) |
1362 | ps = &sdata->u.mesh.ps; | |
d012a605 MP |
1363 | else |
1364 | return; | |
50a9432d | 1365 | |
c2c98fde | 1366 | clear_sta_flag(sta, WLAN_STA_SP); |
47086fc5 | 1367 | |
5a306f58 | 1368 | BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1); |
948d887d | 1369 | sta->driver_buffered_tids = 0; |
ba8c3d6f | 1370 | sta->txq_buffered_tids = 0; |
af818581 | 1371 | |
30686bf7 | 1372 | if (!ieee80211_hw_check(&local->hw, AP_LINK_PS)) |
d057e5a3 | 1373 | drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); |
af818581 | 1374 | |
adf8ed01 JB |
1375 | for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) { |
1376 | if (!sta->sta.txq[i] || !txq_has_queue(sta->sta.txq[i])) | |
1377 | continue; | |
ba8c3d6f | 1378 | |
18667600 | 1379 | schedule_and_wake_txq(local, to_txq_info(sta->sta.txq[i])); |
ba8c3d6f FF |
1380 | } |
1381 | ||
948d887d | 1382 | skb_queue_head_init(&pending); |
af818581 | 1383 | |
1d147bfa EG |
1384 | /* sync with ieee80211_tx_h_unicast_ps_buf */ |
1385 | spin_lock(&sta->ps_lock); | |
af818581 | 1386 | /* Send all buffered frames to the station */ |
948d887d JB |
1387 | for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { |
1388 | int count = skb_queue_len(&pending), tmp; | |
1389 | ||
987c285c | 1390 | spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); |
948d887d | 1391 | skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending); |
987c285c | 1392 | spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); |
948d887d JB |
1393 | tmp = skb_queue_len(&pending); |
1394 | filtered += tmp - count; | |
1395 | count = tmp; | |
1396 | ||
987c285c | 1397 | spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); |
948d887d | 1398 | skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending); |
987c285c | 1399 | spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); |
948d887d JB |
1400 | tmp = skb_queue_len(&pending); |
1401 | buffered += tmp - count; | |
1402 | } | |
1403 | ||
e3685e03 | 1404 | ieee80211_add_pending_skbs(local, &pending); |
5ac2e350 JB |
1405 | |
1406 | /* now we're no longer in the deliver code */ | |
1407 | clear_sta_flag(sta, WLAN_STA_PS_DELIVER); | |
1408 | ||
1409 | /* The station might have polled and then woken up before we responded, | |
1410 | * so clear these flags now to avoid them sticking around. | |
1411 | */ | |
1412 | clear_sta_flag(sta, WLAN_STA_PSPOLL); | |
1413 | clear_sta_flag(sta, WLAN_STA_UAPSD); | |
1d147bfa | 1414 | spin_unlock(&sta->ps_lock); |
948d887d | 1415 | |
e3685e03 JB |
1416 | atomic_dec(&ps->num_sta_ps); |
1417 | ||
af818581 JB |
1418 | local->total_ps_buffered -= buffered; |
1419 | ||
c868cb35 JB |
1420 | sta_info_recalc_tim(sta); |
1421 | ||
bdcbd8e0 | 1422 | ps_dbg(sdata, |
2595d259 | 1423 | "STA %pM aid %d sending %d filtered/%d PS frames since STA woke up\n", |
bdcbd8e0 | 1424 | sta->sta.addr, sta->sta.aid, filtered, buffered); |
17c18bf8 JB |
1425 | |
1426 | ieee80211_check_fast_xmit(sta); | |
af818581 JB |
1427 | } |
1428 | ||
0ead2510 | 1429 | static void ieee80211_send_null_response(struct sta_info *sta, int tid, |
b77cf4f8 | 1430 | enum ieee80211_frame_release_type reason, |
0ead2510 | 1431 | bool call_driver, bool more_data) |
af818581 | 1432 | { |
0ead2510 | 1433 | struct ieee80211_sub_if_data *sdata = sta->sdata; |
af818581 | 1434 | struct ieee80211_local *local = sdata->local; |
ce662b44 | 1435 | struct ieee80211_qos_hdr *nullfunc; |
af818581 | 1436 | struct sk_buff *skb; |
ce662b44 JB |
1437 | int size = sizeof(*nullfunc); |
1438 | __le16 fc; | |
a74a8c84 | 1439 | bool qos = sta->sta.wme; |
ce662b44 | 1440 | struct ieee80211_tx_info *info; |
55de908a | 1441 | struct ieee80211_chanctx_conf *chanctx_conf; |
af818581 | 1442 | |
ce662b44 JB |
1443 | if (qos) { |
1444 | fc = cpu_to_le16(IEEE80211_FTYPE_DATA | | |
1445 | IEEE80211_STYPE_QOS_NULLFUNC | | |
1446 | IEEE80211_FCTL_FROMDS); | |
1447 | } else { | |
1448 | size -= 2; | |
1449 | fc = cpu_to_le16(IEEE80211_FTYPE_DATA | | |
1450 | IEEE80211_STYPE_NULLFUNC | | |
1451 | IEEE80211_FCTL_FROMDS); | |
af818581 | 1452 | } |
af818581 | 1453 | |
ce662b44 JB |
1454 | skb = dev_alloc_skb(local->hw.extra_tx_headroom + size); |
1455 | if (!skb) | |
1456 | return; | |
1457 | ||
1458 | skb_reserve(skb, local->hw.extra_tx_headroom); | |
1459 | ||
4df864c1 | 1460 | nullfunc = skb_put(skb, size); |
ce662b44 JB |
1461 | nullfunc->frame_control = fc; |
1462 | nullfunc->duration_id = 0; | |
1463 | memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN); | |
1464 | memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN); | |
1465 | memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN); | |
864a6040 | 1466 | nullfunc->seq_ctrl = 0; |
ce662b44 | 1467 | |
59b66255 JB |
1468 | skb->priority = tid; |
1469 | skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]); | |
ce662b44 | 1470 | if (qos) { |
ce662b44 JB |
1471 | nullfunc->qos_ctrl = cpu_to_le16(tid); |
1472 | ||
0ead2510 | 1473 | if (reason == IEEE80211_FRAME_RELEASE_UAPSD) { |
ce662b44 JB |
1474 | nullfunc->qos_ctrl |= |
1475 | cpu_to_le16(IEEE80211_QOS_CTL_EOSP); | |
0ead2510 EG |
1476 | if (more_data) |
1477 | nullfunc->frame_control |= | |
1478 | cpu_to_le16(IEEE80211_FCTL_MOREDATA); | |
1479 | } | |
ce662b44 JB |
1480 | } |
1481 | ||
1482 | info = IEEE80211_SKB_CB(skb); | |
1483 | ||
1484 | /* | |
1485 | * Tell TX path to send this frame even though the | |
1486 | * STA may still remain is PS mode after this frame | |
deeaee19 JB |
1487 | * exchange. Also set EOSP to indicate this packet |
1488 | * ends the poll/service period. | |
ce662b44 | 1489 | */ |
02f2f1a9 | 1490 | info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER | |
deeaee19 JB |
1491 | IEEE80211_TX_STATUS_EOSP | |
1492 | IEEE80211_TX_CTL_REQ_TX_STATUS; | |
ce662b44 | 1493 | |
6b127c71 SM |
1494 | info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE; |
1495 | ||
b77cf4f8 JB |
1496 | if (call_driver) |
1497 | drv_allow_buffered_frames(local, sta, BIT(tid), 1, | |
1498 | reason, false); | |
40b96408 | 1499 | |
89afe614 JB |
1500 | skb->dev = sdata->dev; |
1501 | ||
55de908a | 1502 | rcu_read_lock(); |
d0a9123e | 1503 | chanctx_conf = rcu_dereference(sdata->vif.bss_conf.chanctx_conf); |
55de908a JB |
1504 | if (WARN_ON(!chanctx_conf)) { |
1505 | rcu_read_unlock(); | |
1506 | kfree_skb(skb); | |
1507 | return; | |
1508 | } | |
1509 | ||
73c4e195 | 1510 | info->band = chanctx_conf->def.chan->band; |
08aca29a | 1511 | ieee80211_xmit(sdata, sta, skb); |
55de908a | 1512 | rcu_read_unlock(); |
ce662b44 JB |
1513 | } |
1514 | ||
0a1cb809 JB |
1515 | static int find_highest_prio_tid(unsigned long tids) |
1516 | { | |
1517 | /* lower 3 TIDs aren't ordered perfectly */ | |
1518 | if (tids & 0xF8) | |
1519 | return fls(tids) - 1; | |
1520 | /* TID 0 is BE just like TID 3 */ | |
1521 | if (tids & BIT(0)) | |
1522 | return 0; | |
1523 | return fls(tids) - 1; | |
1524 | } | |
1525 | ||
0ead2510 EG |
1526 | /* Indicates if the MORE_DATA bit should be set in the last |
1527 | * frame obtained by ieee80211_sta_ps_get_frames. | |
1528 | * Note that driver_release_tids is relevant only if | |
1529 | * reason = IEEE80211_FRAME_RELEASE_PSPOLL | |
1530 | */ | |
1531 | static bool | |
1532 | ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs, | |
1533 | enum ieee80211_frame_release_type reason, | |
1534 | unsigned long driver_release_tids) | |
1535 | { | |
1536 | int ac; | |
1537 | ||
1538 | /* If the driver has data on more than one TID then | |
1539 | * certainly there's more data if we release just a | |
1540 | * single frame now (from a single TID). This will | |
1541 | * only happen for PS-Poll. | |
1542 | */ | |
1543 | if (reason == IEEE80211_FRAME_RELEASE_PSPOLL && | |
1544 | hweight16(driver_release_tids) > 1) | |
1545 | return true; | |
1546 | ||
1547 | for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { | |
f438ceb8 | 1548 | if (ignored_acs & ieee80211_ac_to_qos_mask[ac]) |
0ead2510 EG |
1549 | continue; |
1550 | ||
1551 | if (!skb_queue_empty(&sta->tx_filtered[ac]) || | |
1552 | !skb_queue_empty(&sta->ps_tx_buf[ac])) | |
1553 | return true; | |
1554 | } | |
1555 | ||
1556 | return false; | |
1557 | } | |
1558 | ||
47086fc5 | 1559 | static void |
0ead2510 EG |
1560 | ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs, |
1561 | enum ieee80211_frame_release_type reason, | |
1562 | struct sk_buff_head *frames, | |
1563 | unsigned long *driver_release_tids) | |
af818581 JB |
1564 | { |
1565 | struct ieee80211_sub_if_data *sdata = sta->sdata; | |
1566 | struct ieee80211_local *local = sdata->local; | |
948d887d | 1567 | int ac; |
948d887d | 1568 | |
f9f760b4 | 1569 | /* Get response frame(s) and more data bit for the last one. */ |
948d887d | 1570 | for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { |
4049e09a JB |
1571 | unsigned long tids; |
1572 | ||
f438ceb8 | 1573 | if (ignored_acs & ieee80211_ac_to_qos_mask[ac]) |
948d887d JB |
1574 | continue; |
1575 | ||
4049e09a JB |
1576 | tids = ieee80211_tids_for_ac(ac); |
1577 | ||
f9f760b4 JB |
1578 | /* if we already have frames from software, then we can't also |
1579 | * release from hardware queues | |
1580 | */ | |
0ead2510 EG |
1581 | if (skb_queue_empty(frames)) { |
1582 | *driver_release_tids |= | |
1583 | sta->driver_buffered_tids & tids; | |
1584 | *driver_release_tids |= sta->txq_buffered_tids & tids; | |
ba8c3d6f | 1585 | } |
948d887d | 1586 | |
0ead2510 | 1587 | if (!*driver_release_tids) { |
f9f760b4 JB |
1588 | struct sk_buff *skb; |
1589 | ||
1590 | while (n_frames > 0) { | |
1591 | skb = skb_dequeue(&sta->tx_filtered[ac]); | |
1592 | if (!skb) { | |
1593 | skb = skb_dequeue( | |
1594 | &sta->ps_tx_buf[ac]); | |
1595 | if (skb) | |
1596 | local->total_ps_buffered--; | |
1597 | } | |
1598 | if (!skb) | |
1599 | break; | |
1600 | n_frames--; | |
0ead2510 | 1601 | __skb_queue_tail(frames, skb); |
f9f760b4 | 1602 | } |
4049e09a | 1603 | } |
948d887d | 1604 | |
0ead2510 EG |
1605 | /* If we have more frames buffered on this AC, then abort the |
1606 | * loop since we can't send more data from other ACs before | |
1607 | * the buffered frames from this. | |
f9f760b4 | 1608 | */ |
948d887d | 1609 | if (!skb_queue_empty(&sta->tx_filtered[ac]) || |
0ead2510 | 1610 | !skb_queue_empty(&sta->ps_tx_buf[ac])) |
948d887d | 1611 | break; |
af818581 | 1612 | } |
0ead2510 EG |
1613 | } |
1614 | ||
1615 | static void | |
1616 | ieee80211_sta_ps_deliver_response(struct sta_info *sta, | |
1617 | int n_frames, u8 ignored_acs, | |
1618 | enum ieee80211_frame_release_type reason) | |
1619 | { | |
1620 | struct ieee80211_sub_if_data *sdata = sta->sdata; | |
1621 | struct ieee80211_local *local = sdata->local; | |
1622 | unsigned long driver_release_tids = 0; | |
1623 | struct sk_buff_head frames; | |
1624 | bool more_data; | |
1625 | ||
1626 | /* Service or PS-Poll period starts */ | |
1627 | set_sta_flag(sta, WLAN_STA_SP); | |
1628 | ||
1629 | __skb_queue_head_init(&frames); | |
1630 | ||
1631 | ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason, | |
1632 | &frames, &driver_release_tids); | |
1633 | ||
1634 | more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids); | |
1635 | ||
1a57081a | 1636 | if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL) |
0ead2510 EG |
1637 | driver_release_tids = |
1638 | BIT(find_highest_prio_tid(driver_release_tids)); | |
af818581 | 1639 | |
f9f760b4 | 1640 | if (skb_queue_empty(&frames) && !driver_release_tids) { |
f438ceb8 | 1641 | int tid, ac; |
af818581 JB |
1642 | |
1643 | /* | |
ce662b44 JB |
1644 | * For PS-Poll, this can only happen due to a race condition |
1645 | * when we set the TIM bit and the station notices it, but | |
1646 | * before it can poll for the frame we expire it. | |
1647 | * | |
1648 | * For uAPSD, this is said in the standard (11.2.1.5 h): | |
1649 | * At each unscheduled SP for a non-AP STA, the AP shall | |
1650 | * attempt to transmit at least one MSDU or MMPDU, but no | |
1651 | * more than the value specified in the Max SP Length field | |
1652 | * in the QoS Capability element from delivery-enabled ACs, | |
1653 | * that are destined for the non-AP STA. | |
1654 | * | |
1655 | * Since we have no other MSDU/MMPDU, transmit a QoS null frame. | |
af818581 | 1656 | */ |
af818581 | 1657 | |
ce662b44 | 1658 | /* This will evaluate to 1, 3, 5 or 7. */ |
f438ceb8 | 1659 | for (ac = IEEE80211_AC_VO; ac < IEEE80211_NUM_ACS; ac++) |
d7f84244 EG |
1660 | if (!(ignored_acs & ieee80211_ac_to_qos_mask[ac])) |
1661 | break; | |
f438ceb8 | 1662 | tid = 7 - 2 * ac; |
af818581 | 1663 | |
0ead2510 | 1664 | ieee80211_send_null_response(sta, tid, reason, true, false); |
f9f760b4 | 1665 | } else if (!driver_release_tids) { |
47086fc5 JB |
1666 | struct sk_buff_head pending; |
1667 | struct sk_buff *skb; | |
40b96408 JB |
1668 | int num = 0; |
1669 | u16 tids = 0; | |
b77cf4f8 | 1670 | bool need_null = false; |
af818581 | 1671 | |
47086fc5 | 1672 | skb_queue_head_init(&pending); |
af818581 | 1673 | |
47086fc5 JB |
1674 | while ((skb = __skb_dequeue(&frames))) { |
1675 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); | |
1676 | struct ieee80211_hdr *hdr = (void *) skb->data; | |
40b96408 JB |
1677 | u8 *qoshdr = NULL; |
1678 | ||
1679 | num++; | |
af818581 | 1680 | |
47086fc5 JB |
1681 | /* |
1682 | * Tell TX path to send this frame even though the | |
1683 | * STA may still remain is PS mode after this frame | |
1684 | * exchange. | |
1685 | */ | |
6b127c71 SM |
1686 | info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER; |
1687 | info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE; | |
47086fc5 JB |
1688 | |
1689 | /* | |
1690 | * Use MoreData flag to indicate whether there are | |
1691 | * more buffered frames for this STA | |
1692 | */ | |
24b9c373 | 1693 | if (more_data || !skb_queue_empty(&frames)) |
47086fc5 JB |
1694 | hdr->frame_control |= |
1695 | cpu_to_le16(IEEE80211_FCTL_MOREDATA); | |
24b9c373 JD |
1696 | else |
1697 | hdr->frame_control &= | |
1698 | cpu_to_le16(~IEEE80211_FCTL_MOREDATA); | |
47086fc5 | 1699 | |
40b96408 JB |
1700 | if (ieee80211_is_data_qos(hdr->frame_control) || |
1701 | ieee80211_is_qos_nullfunc(hdr->frame_control)) | |
1702 | qoshdr = ieee80211_get_qos_ctl(hdr); | |
1703 | ||
b77cf4f8 | 1704 | tids |= BIT(skb->priority); |
52a3f20c | 1705 | |
b77cf4f8 JB |
1706 | __skb_queue_tail(&pending, skb); |
1707 | ||
1708 | /* end service period after last frame or add one */ | |
1709 | if (!skb_queue_empty(&frames)) | |
1710 | continue; | |
1711 | ||
1712 | if (reason != IEEE80211_FRAME_RELEASE_UAPSD) { | |
1713 | /* for PS-Poll, there's only one frame */ | |
52a3f20c MP |
1714 | info->flags |= IEEE80211_TX_STATUS_EOSP | |
1715 | IEEE80211_TX_CTL_REQ_TX_STATUS; | |
b77cf4f8 | 1716 | break; |
52a3f20c | 1717 | } |
deeaee19 | 1718 | |
b77cf4f8 JB |
1719 | /* For uAPSD, things are a bit more complicated. If the |
1720 | * last frame has a QoS header (i.e. is a QoS-data or | |
1721 | * QoS-nulldata frame) then just set the EOSP bit there | |
1722 | * and be done. | |
1723 | * If the frame doesn't have a QoS header (which means | |
1724 | * it should be a bufferable MMPDU) then we can't set | |
1725 | * the EOSP bit in the QoS header; add a QoS-nulldata | |
1726 | * frame to the list to send it after the MMPDU. | |
1727 | * | |
1728 | * Note that this code is only in the mac80211-release | |
1729 | * code path, we assume that the driver will not buffer | |
1730 | * anything but QoS-data frames, or if it does, will | |
1731 | * create the QoS-nulldata frame by itself if needed. | |
1732 | * | |
1733 | * Cf. 802.11-2012 10.2.1.10 (c). | |
1734 | */ | |
1735 | if (qoshdr) { | |
1736 | *qoshdr |= IEEE80211_QOS_CTL_EOSP; | |
40b96408 | 1737 | |
b77cf4f8 JB |
1738 | info->flags |= IEEE80211_TX_STATUS_EOSP | |
1739 | IEEE80211_TX_CTL_REQ_TX_STATUS; | |
1740 | } else { | |
1741 | /* The standard isn't completely clear on this | |
1742 | * as it says the more-data bit should be set | |
1743 | * if there are more BUs. The QoS-Null frame | |
1744 | * we're about to send isn't buffered yet, we | |
1745 | * only create it below, but let's pretend it | |
1746 | * was buffered just in case some clients only | |
1747 | * expect more-data=0 when eosp=1. | |
1748 | */ | |
1749 | hdr->frame_control |= | |
1750 | cpu_to_le16(IEEE80211_FCTL_MOREDATA); | |
1751 | need_null = true; | |
1752 | num++; | |
1753 | } | |
1754 | break; | |
47086fc5 | 1755 | } |
af818581 | 1756 | |
40b96408 JB |
1757 | drv_allow_buffered_frames(local, sta, tids, num, |
1758 | reason, more_data); | |
1759 | ||
47086fc5 | 1760 | ieee80211_add_pending_skbs(local, &pending); |
af818581 | 1761 | |
b77cf4f8 JB |
1762 | if (need_null) |
1763 | ieee80211_send_null_response( | |
0ead2510 EG |
1764 | sta, find_highest_prio_tid(tids), |
1765 | reason, false, false); | |
b77cf4f8 | 1766 | |
c868cb35 | 1767 | sta_info_recalc_tim(sta); |
af818581 | 1768 | } else { |
ba8c3d6f FF |
1769 | int tid; |
1770 | ||
af818581 | 1771 | /* |
4049e09a JB |
1772 | * We need to release a frame that is buffered somewhere in the |
1773 | * driver ... it'll have to handle that. | |
f9f760b4 JB |
1774 | * Note that the driver also has to check the number of frames |
1775 | * on the TIDs we're releasing from - if there are more than | |
1776 | * n_frames it has to set the more-data bit (if we didn't ask | |
1777 | * it to set it anyway due to other buffered frames); if there | |
1778 | * are fewer than n_frames it has to make sure to adjust that | |
1779 | * to allow the service period to end properly. | |
4049e09a JB |
1780 | */ |
1781 | drv_release_buffered_frames(local, sta, driver_release_tids, | |
47086fc5 | 1782 | n_frames, reason, more_data); |
4049e09a JB |
1783 | |
1784 | /* | |
1785 | * Note that we don't recalculate the TIM bit here as it would | |
1786 | * most likely have no effect at all unless the driver told us | |
f9f760b4 | 1787 | * that the TID(s) became empty before returning here from the |
4049e09a | 1788 | * release function. |
f9f760b4 | 1789 | * Either way, however, when the driver tells us that the TID(s) |
ba8c3d6f FF |
1790 | * became empty or we find that a txq became empty, we'll do the |
1791 | * TIM recalculation. | |
af818581 | 1792 | */ |
ba8c3d6f FF |
1793 | |
1794 | if (!sta->sta.txq[0]) | |
1795 | return; | |
1796 | ||
1797 | for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) { | |
adf8ed01 JB |
1798 | if (!sta->sta.txq[tid] || |
1799 | !(driver_release_tids & BIT(tid)) || | |
1e1430d5 | 1800 | txq_has_queue(sta->sta.txq[tid])) |
ba8c3d6f FF |
1801 | continue; |
1802 | ||
1803 | sta_info_recalc_tim(sta); | |
1804 | break; | |
1805 | } | |
af818581 JB |
1806 | } |
1807 | } | |
1808 | ||
47086fc5 JB |
1809 | void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) |
1810 | { | |
1811 | u8 ignore_for_response = sta->sta.uapsd_queues; | |
1812 | ||
1813 | /* | |
1814 | * If all ACs are delivery-enabled then we should reply | |
1815 | * from any of them, if only some are enabled we reply | |
1816 | * only from the non-enabled ones. | |
1817 | */ | |
1818 | if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1) | |
1819 | ignore_for_response = 0; | |
1820 | ||
1821 | ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response, | |
1822 | IEEE80211_FRAME_RELEASE_PSPOLL); | |
1823 | } | |
1824 | ||
1825 | void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta) | |
1826 | { | |
1827 | int n_frames = sta->sta.max_sp; | |
1828 | u8 delivery_enabled = sta->sta.uapsd_queues; | |
1829 | ||
1830 | /* | |
1831 | * If we ever grow support for TSPEC this might happen if | |
1832 | * the TSPEC update from hostapd comes in between a trigger | |
1833 | * frame setting WLAN_STA_UAPSD in the RX path and this | |
1834 | * actually getting called. | |
1835 | */ | |
1836 | if (!delivery_enabled) | |
1837 | return; | |
1838 | ||
47086fc5 JB |
1839 | switch (sta->sta.max_sp) { |
1840 | case 1: | |
1841 | n_frames = 2; | |
1842 | break; | |
1843 | case 2: | |
1844 | n_frames = 4; | |
1845 | break; | |
1846 | case 3: | |
1847 | n_frames = 6; | |
1848 | break; | |
1849 | case 0: | |
1850 | /* XXX: what is a good value? */ | |
13a8098a | 1851 | n_frames = 128; |
47086fc5 JB |
1852 | break; |
1853 | } | |
1854 | ||
1855 | ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled, | |
1856 | IEEE80211_FRAME_RELEASE_UAPSD); | |
1857 | } | |
1858 | ||
af818581 JB |
1859 | void ieee80211_sta_block_awake(struct ieee80211_hw *hw, |
1860 | struct ieee80211_sta *pubsta, bool block) | |
1861 | { | |
1862 | struct sta_info *sta = container_of(pubsta, struct sta_info, sta); | |
1863 | ||
b5878a2d JB |
1864 | trace_api_sta_block_awake(sta->local, pubsta, block); |
1865 | ||
5ac2e350 | 1866 | if (block) { |
c2c98fde | 1867 | set_sta_flag(sta, WLAN_STA_PS_DRIVER); |
17c18bf8 | 1868 | ieee80211_clear_fast_xmit(sta); |
5ac2e350 JB |
1869 | return; |
1870 | } | |
1871 | ||
1872 | if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER)) | |
1873 | return; | |
1874 | ||
1875 | if (!test_sta_flag(sta, WLAN_STA_PS_STA)) { | |
1876 | set_sta_flag(sta, WLAN_STA_PS_DELIVER); | |
1877 | clear_sta_flag(sta, WLAN_STA_PS_DRIVER); | |
1878 | ieee80211_queue_work(hw, &sta->drv_deliver_wk); | |
1879 | } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) || | |
1880 | test_sta_flag(sta, WLAN_STA_UAPSD)) { | |
1881 | /* must be asleep in this case */ | |
1882 | clear_sta_flag(sta, WLAN_STA_PS_DRIVER); | |
1883 | ieee80211_queue_work(hw, &sta->drv_deliver_wk); | |
1884 | } else { | |
1885 | clear_sta_flag(sta, WLAN_STA_PS_DRIVER); | |
17c18bf8 | 1886 | ieee80211_check_fast_xmit(sta); |
5ac2e350 | 1887 | } |
af818581 JB |
1888 | } |
1889 | EXPORT_SYMBOL(ieee80211_sta_block_awake); | |
dcf55fb5 | 1890 | |
e943789e | 1891 | void ieee80211_sta_eosp(struct ieee80211_sta *pubsta) |
37fbd908 JB |
1892 | { |
1893 | struct sta_info *sta = container_of(pubsta, struct sta_info, sta); | |
1894 | struct ieee80211_local *local = sta->local; | |
37fbd908 JB |
1895 | |
1896 | trace_api_eosp(local, pubsta); | |
1897 | ||
e943789e | 1898 | clear_sta_flag(sta, WLAN_STA_SP); |
37fbd908 | 1899 | } |
e943789e | 1900 | EXPORT_SYMBOL(ieee80211_sta_eosp); |
37fbd908 | 1901 | |
0ead2510 EG |
1902 | void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid) |
1903 | { | |
1904 | struct sta_info *sta = container_of(pubsta, struct sta_info, sta); | |
1905 | enum ieee80211_frame_release_type reason; | |
1906 | bool more_data; | |
1907 | ||
1908 | trace_api_send_eosp_nullfunc(sta->local, pubsta, tid); | |
1909 | ||
1910 | reason = IEEE80211_FRAME_RELEASE_UAPSD; | |
1911 | more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues, | |
1912 | reason, 0); | |
1913 | ||
1914 | ieee80211_send_null_response(sta, tid, reason, false, more_data); | |
1915 | } | |
1916 | EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc); | |
1917 | ||
042ec453 JB |
1918 | void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta, |
1919 | u8 tid, bool buffered) | |
dcf55fb5 FF |
1920 | { |
1921 | struct sta_info *sta = container_of(pubsta, struct sta_info, sta); | |
1922 | ||
5a306f58 | 1923 | if (WARN_ON(tid >= IEEE80211_NUM_TIDS)) |
042ec453 JB |
1924 | return; |
1925 | ||
1b000789 JB |
1926 | trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered); |
1927 | ||
948d887d JB |
1928 | if (buffered) |
1929 | set_bit(tid, &sta->driver_buffered_tids); | |
1930 | else | |
1931 | clear_bit(tid, &sta->driver_buffered_tids); | |
1932 | ||
c868cb35 | 1933 | sta_info_recalc_tim(sta); |
dcf55fb5 | 1934 | } |
042ec453 | 1935 | EXPORT_SYMBOL(ieee80211_sta_set_buffered); |
d9a7ddb0 | 1936 | |
2433647b THJ |
1937 | void ieee80211_register_airtime(struct ieee80211_txq *txq, |
1938 | u32 tx_airtime, u32 rx_airtime) | |
b4809e94 | 1939 | { |
2433647b THJ |
1940 | struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif); |
1941 | struct ieee80211_local *local = sdata->local; | |
1942 | u64 weight_sum, weight_sum_reciprocal; | |
1943 | struct airtime_sched_info *air_sched; | |
1944 | struct airtime_info *air_info; | |
b4809e94 THJ |
1945 | u32 airtime = 0; |
1946 | ||
2433647b THJ |
1947 | air_sched = &local->airtime[txq->ac]; |
1948 | air_info = to_airtime_info(txq); | |
1949 | ||
1950 | if (local->airtime_flags & AIRTIME_USE_TX) | |
b4809e94 | 1951 | airtime += tx_airtime; |
2433647b | 1952 | if (local->airtime_flags & AIRTIME_USE_RX) |
b4809e94 THJ |
1953 | airtime += rx_airtime; |
1954 | ||
2433647b THJ |
1955 | /* Weights scale so the unit weight is 256 */ |
1956 | airtime <<= 8; | |
1957 | ||
1958 | spin_lock_bh(&air_sched->lock); | |
1959 | ||
1960 | air_info->tx_airtime += tx_airtime; | |
1961 | air_info->rx_airtime += rx_airtime; | |
1962 | ||
1963 | if (air_sched->weight_sum) { | |
1964 | weight_sum = air_sched->weight_sum; | |
1965 | weight_sum_reciprocal = air_sched->weight_sum_reciprocal; | |
1966 | } else { | |
1967 | weight_sum = air_info->weight; | |
1968 | weight_sum_reciprocal = air_info->weight_reciprocal; | |
1969 | } | |
1970 | ||
1971 | /* Round the calculation of global vt */ | |
1972 | air_sched->v_t += (u64)((airtime + (weight_sum >> 1)) * | |
1973 | weight_sum_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_64; | |
1974 | air_info->v_t += (u32)((airtime + (air_info->weight >> 1)) * | |
1975 | air_info->weight_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_32; | |
1976 | ieee80211_resort_txq(&local->hw, txq); | |
1977 | ||
1978 | spin_unlock_bh(&air_sched->lock); | |
1979 | } | |
1980 | ||
1981 | void ieee80211_sta_register_airtime(struct ieee80211_sta *pubsta, u8 tid, | |
1982 | u32 tx_airtime, u32 rx_airtime) | |
1983 | { | |
1984 | struct ieee80211_txq *txq = pubsta->txq[tid]; | |
1985 | ||
1986 | if (!txq) | |
1987 | return; | |
1988 | ||
1989 | ieee80211_register_airtime(txq, tx_airtime, rx_airtime); | |
b4809e94 THJ |
1990 | } |
1991 | EXPORT_SYMBOL(ieee80211_sta_register_airtime); | |
1992 | ||
3ace10f5 KY |
1993 | void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local, |
1994 | struct sta_info *sta, u8 ac, | |
1995 | u16 tx_airtime, bool tx_completed) | |
1996 | { | |
1997 | int tx_pending; | |
1998 | ||
911bde0f THJ |
1999 | if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) |
2000 | return; | |
2001 | ||
3ace10f5 KY |
2002 | if (!tx_completed) { |
2003 | if (sta) | |
2004 | atomic_add(tx_airtime, | |
2005 | &sta->airtime[ac].aql_tx_pending); | |
2006 | ||
2007 | atomic_add(tx_airtime, &local->aql_total_pending_airtime); | |
2008 | return; | |
2009 | } | |
2010 | ||
2011 | if (sta) { | |
2012 | tx_pending = atomic_sub_return(tx_airtime, | |
2013 | &sta->airtime[ac].aql_tx_pending); | |
04e35caa | 2014 | if (tx_pending < 0) |
3ace10f5 KY |
2015 | atomic_cmpxchg(&sta->airtime[ac].aql_tx_pending, |
2016 | tx_pending, 0); | |
2017 | } | |
2018 | ||
2019 | tx_pending = atomic_sub_return(tx_airtime, | |
2020 | &local->aql_total_pending_airtime); | |
2021 | if (WARN_ONCE(tx_pending < 0, | |
2022 | "Device %s AC %d pending airtime underflow: %u, %u", | |
2023 | wiphy_name(local->hw.wiphy), ac, tx_pending, | |
2024 | tx_airtime)) | |
2025 | atomic_cmpxchg(&local->aql_total_pending_airtime, | |
2026 | tx_pending, 0); | |
2027 | } | |
2028 | ||
83d5cc01 JB |
2029 | int sta_info_move_state(struct sta_info *sta, |
2030 | enum ieee80211_sta_state new_state) | |
d9a7ddb0 | 2031 | { |
8bf11d8d | 2032 | might_sleep(); |
d9a7ddb0 JB |
2033 | |
2034 | if (sta->sta_state == new_state) | |
2035 | return 0; | |
2036 | ||
f09603a2 JB |
2037 | /* check allowed transitions first */ |
2038 | ||
2039 | switch (new_state) { | |
2040 | case IEEE80211_STA_NONE: | |
2041 | if (sta->sta_state != IEEE80211_STA_AUTH) | |
2042 | return -EINVAL; | |
2043 | break; | |
2044 | case IEEE80211_STA_AUTH: | |
2045 | if (sta->sta_state != IEEE80211_STA_NONE && | |
2046 | sta->sta_state != IEEE80211_STA_ASSOC) | |
2047 | return -EINVAL; | |
2048 | break; | |
2049 | case IEEE80211_STA_ASSOC: | |
2050 | if (sta->sta_state != IEEE80211_STA_AUTH && | |
2051 | sta->sta_state != IEEE80211_STA_AUTHORIZED) | |
2052 | return -EINVAL; | |
2053 | break; | |
2054 | case IEEE80211_STA_AUTHORIZED: | |
2055 | if (sta->sta_state != IEEE80211_STA_ASSOC) | |
2056 | return -EINVAL; | |
2057 | break; | |
2058 | default: | |
2059 | WARN(1, "invalid state %d", new_state); | |
2060 | return -EINVAL; | |
2061 | } | |
2062 | ||
bdcbd8e0 JB |
2063 | sta_dbg(sta->sdata, "moving STA %pM to state %d\n", |
2064 | sta->sta.addr, new_state); | |
f09603a2 JB |
2065 | |
2066 | /* | |
2067 | * notify the driver before the actual changes so it can | |
2068 | * fail the transition | |
2069 | */ | |
2070 | if (test_sta_flag(sta, WLAN_STA_INSERTED)) { | |
2071 | int err = drv_sta_state(sta->local, sta->sdata, sta, | |
2072 | sta->sta_state, new_state); | |
2073 | if (err) | |
2074 | return err; | |
2075 | } | |
2076 | ||
2077 | /* reflect the change in all state variables */ | |
2078 | ||
d9a7ddb0 JB |
2079 | switch (new_state) { |
2080 | case IEEE80211_STA_NONE: | |
2081 | if (sta->sta_state == IEEE80211_STA_AUTH) | |
2082 | clear_bit(WLAN_STA_AUTH, &sta->_flags); | |
d9a7ddb0 JB |
2083 | break; |
2084 | case IEEE80211_STA_AUTH: | |
a7201a6c | 2085 | if (sta->sta_state == IEEE80211_STA_NONE) { |
d9a7ddb0 | 2086 | set_bit(WLAN_STA_AUTH, &sta->_flags); |
a7201a6c | 2087 | } else if (sta->sta_state == IEEE80211_STA_ASSOC) { |
d9a7ddb0 | 2088 | clear_bit(WLAN_STA_ASSOC, &sta->_flags); |
a7201a6c | 2089 | ieee80211_recalc_min_chandef(sta->sdata); |
52cfa1d6 AB |
2090 | if (!sta->sta.support_p2p_ps) |
2091 | ieee80211_recalc_p2p_go_ps_allowed(sta->sdata); | |
a7201a6c | 2092 | } |
d9a7ddb0 JB |
2093 | break; |
2094 | case IEEE80211_STA_ASSOC: | |
29623892 | 2095 | if (sta->sta_state == IEEE80211_STA_AUTH) { |
d9a7ddb0 | 2096 | set_bit(WLAN_STA_ASSOC, &sta->_flags); |
9cf02338 | 2097 | sta->assoc_at = ktime_get_boottime_ns(); |
a7201a6c | 2098 | ieee80211_recalc_min_chandef(sta->sdata); |
52cfa1d6 AB |
2099 | if (!sta->sta.support_p2p_ps) |
2100 | ieee80211_recalc_p2p_go_ps_allowed(sta->sdata); | |
29623892 | 2101 | } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) { |
72f15d53 | 2102 | ieee80211_vif_dec_num_mcast(sta->sdata); |
d9a7ddb0 | 2103 | clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags); |
17c18bf8 | 2104 | ieee80211_clear_fast_xmit(sta); |
49ddf8e6 | 2105 | ieee80211_clear_fast_rx(sta); |
f09603a2 | 2106 | } |
d9a7ddb0 JB |
2107 | break; |
2108 | case IEEE80211_STA_AUTHORIZED: | |
29623892 | 2109 | if (sta->sta_state == IEEE80211_STA_ASSOC) { |
72f15d53 | 2110 | ieee80211_vif_inc_num_mcast(sta->sdata); |
d9a7ddb0 | 2111 | set_bit(WLAN_STA_AUTHORIZED, &sta->_flags); |
17c18bf8 | 2112 | ieee80211_check_fast_xmit(sta); |
49ddf8e6 | 2113 | ieee80211_check_fast_rx(sta); |
f09603a2 | 2114 | } |
3e493173 JM |
2115 | if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN || |
2116 | sta->sdata->vif.type == NL80211_IFTYPE_AP) | |
2117 | cfg80211_send_layer2_update(sta->sdata->dev, | |
2118 | sta->sta.addr); | |
d9a7ddb0 JB |
2119 | break; |
2120 | default: | |
f09603a2 | 2121 | break; |
d9a7ddb0 JB |
2122 | } |
2123 | ||
d9a7ddb0 JB |
2124 | sta->sta_state = new_state; |
2125 | ||
2126 | return 0; | |
2127 | } | |
687da132 | 2128 | |
c9c5962b JB |
2129 | static struct ieee80211_sta_rx_stats * |
2130 | sta_get_last_rx_stats(struct sta_info *sta) | |
2131 | { | |
046d2e7c | 2132 | struct ieee80211_sta_rx_stats *stats = &sta->deflink.rx_stats; |
c9c5962b JB |
2133 | int cpu; |
2134 | ||
046d2e7c | 2135 | if (!sta->deflink.pcpu_rx_stats) |
c9c5962b JB |
2136 | return stats; |
2137 | ||
2138 | for_each_possible_cpu(cpu) { | |
2139 | struct ieee80211_sta_rx_stats *cpustats; | |
2140 | ||
046d2e7c | 2141 | cpustats = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu); |
c9c5962b JB |
2142 | |
2143 | if (time_after(cpustats->last_rx, stats->last_rx)) | |
2144 | stats = cpustats; | |
2145 | } | |
2146 | ||
2147 | return stats; | |
2148 | } | |
2149 | ||
41cbb0f5 | 2150 | static void sta_stats_decode_rate(struct ieee80211_local *local, u32 rate, |
4f6b1b3d | 2151 | struct rate_info *rinfo) |
fbd6ff5c | 2152 | { |
dcba665b | 2153 | rinfo->bw = STA_STATS_GET(BW, rate); |
4f6b1b3d | 2154 | |
dcba665b | 2155 | switch (STA_STATS_GET(TYPE, rate)) { |
7f406cd1 | 2156 | case STA_STATS_RATE_TYPE_VHT: |
4f6b1b3d | 2157 | rinfo->flags = RATE_INFO_FLAGS_VHT_MCS; |
dcba665b JB |
2158 | rinfo->mcs = STA_STATS_GET(VHT_MCS, rate); |
2159 | rinfo->nss = STA_STATS_GET(VHT_NSS, rate); | |
2160 | if (STA_STATS_GET(SGI, rate)) | |
2161 | rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI; | |
7f406cd1 JB |
2162 | break; |
2163 | case STA_STATS_RATE_TYPE_HT: | |
4f6b1b3d | 2164 | rinfo->flags = RATE_INFO_FLAGS_MCS; |
dcba665b JB |
2165 | rinfo->mcs = STA_STATS_GET(HT_MCS, rate); |
2166 | if (STA_STATS_GET(SGI, rate)) | |
2167 | rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI; | |
7f406cd1 JB |
2168 | break; |
2169 | case STA_STATS_RATE_TYPE_LEGACY: { | |
fbd6ff5c | 2170 | struct ieee80211_supported_band *sband; |
fbd6ff5c | 2171 | u16 brate; |
4f6b1b3d | 2172 | unsigned int shift; |
dcba665b JB |
2173 | int band = STA_STATS_GET(LEGACY_BAND, rate); |
2174 | int rate_idx = STA_STATS_GET(LEGACY_IDX, rate); | |
4f6b1b3d | 2175 | |
dcba665b | 2176 | sband = local->hw.wiphy->bands[band]; |
8b783d10 TP |
2177 | |
2178 | if (WARN_ON_ONCE(!sband->bitrates)) | |
2179 | break; | |
2180 | ||
dcba665b | 2181 | brate = sband->bitrates[rate_idx].bitrate; |
4f6b1b3d JB |
2182 | if (rinfo->bw == RATE_INFO_BW_5) |
2183 | shift = 2; | |
2184 | else if (rinfo->bw == RATE_INFO_BW_10) | |
2185 | shift = 1; | |
2186 | else | |
2187 | shift = 0; | |
fbd6ff5c | 2188 | rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift); |
7f406cd1 JB |
2189 | break; |
2190 | } | |
41cbb0f5 LC |
2191 | case STA_STATS_RATE_TYPE_HE: |
2192 | rinfo->flags = RATE_INFO_FLAGS_HE_MCS; | |
2193 | rinfo->mcs = STA_STATS_GET(HE_MCS, rate); | |
2194 | rinfo->nss = STA_STATS_GET(HE_NSS, rate); | |
2195 | rinfo->he_gi = STA_STATS_GET(HE_GI, rate); | |
2196 | rinfo->he_ru_alloc = STA_STATS_GET(HE_RU, rate); | |
2197 | rinfo->he_dcm = STA_STATS_GET(HE_DCM, rate); | |
2198 | break; | |
fbd6ff5c | 2199 | } |
4f6b1b3d JB |
2200 | } |
2201 | ||
a17d93ff | 2202 | static int sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo) |
4f6b1b3d | 2203 | { |
6aa7de05 | 2204 | u16 rate = READ_ONCE(sta_get_last_rx_stats(sta)->last_rate); |
fbd6ff5c | 2205 | |
4f6b1b3d | 2206 | if (rate == STA_STATS_RATE_INVALID) |
a17d93ff BG |
2207 | return -EINVAL; |
2208 | ||
2209 | sta_stats_decode_rate(sta->local, rate, rinfo); | |
2210 | return 0; | |
fbd6ff5c JB |
2211 | } |
2212 | ||
b255b72b SM |
2213 | static inline u64 sta_get_tidstats_msdu(struct ieee80211_sta_rx_stats *rxstats, |
2214 | int tid) | |
2215 | { | |
2216 | unsigned int start; | |
2217 | u64 value; | |
2218 | ||
2219 | do { | |
2220 | start = u64_stats_fetch_begin(&rxstats->syncp); | |
2221 | value = rxstats->msdu[tid]; | |
2222 | } while (u64_stats_fetch_retry(&rxstats->syncp, start)); | |
2223 | ||
2224 | return value; | |
2225 | } | |
2226 | ||
0f9c5a61 JB |
2227 | static void sta_set_tidstats(struct sta_info *sta, |
2228 | struct cfg80211_tid_stats *tidstats, | |
2229 | int tid) | |
2230 | { | |
2231 | struct ieee80211_local *local = sta->local; | |
b255b72b | 2232 | int cpu; |
0f9c5a61 JB |
2233 | |
2234 | if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) { | |
046d2e7c S |
2235 | tidstats->rx_msdu += sta_get_tidstats_msdu(&sta->deflink.rx_stats, |
2236 | tid); | |
0f9c5a61 | 2237 | |
046d2e7c | 2238 | if (sta->deflink.pcpu_rx_stats) { |
b255b72b SM |
2239 | for_each_possible_cpu(cpu) { |
2240 | struct ieee80211_sta_rx_stats *cpurxs; | |
2241 | ||
046d2e7c S |
2242 | cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, |
2243 | cpu); | |
b255b72b SM |
2244 | tidstats->rx_msdu += |
2245 | sta_get_tidstats_msdu(cpurxs, tid); | |
2246 | } | |
2247 | } | |
0f9c5a61 JB |
2248 | |
2249 | tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU); | |
2250 | } | |
2251 | ||
2252 | if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) { | |
2253 | tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU); | |
046d2e7c | 2254 | tidstats->tx_msdu = sta->deflink.tx_stats.msdu[tid]; |
0f9c5a61 JB |
2255 | } |
2256 | ||
2257 | if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) && | |
2258 | ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) { | |
2259 | tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_RETRIES); | |
046d2e7c | 2260 | tidstats->tx_msdu_retries = sta->deflink.status_stats.msdu_retries[tid]; |
0f9c5a61 JB |
2261 | } |
2262 | ||
2263 | if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) && | |
2264 | ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) { | |
2265 | tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU_FAILED); | |
046d2e7c | 2266 | tidstats->tx_msdu_failed = sta->deflink.status_stats.msdu_failed[tid]; |
0f9c5a61 | 2267 | } |
2fe4a29a THJ |
2268 | |
2269 | if (local->ops->wake_tx_queue && tid < IEEE80211_NUM_TIDS) { | |
2270 | spin_lock_bh(&local->fq.lock); | |
2271 | rcu_read_lock(); | |
2272 | ||
2273 | tidstats->filled |= BIT(NL80211_TID_STATS_TXQ_STATS); | |
2274 | ieee80211_fill_txq_stats(&tidstats->txq_stats, | |
2275 | to_txq_info(sta->sta.txq[tid])); | |
2276 | ||
2277 | rcu_read_unlock(); | |
2278 | spin_unlock_bh(&local->fq.lock); | |
2279 | } | |
0f9c5a61 JB |
2280 | } |
2281 | ||
c9c5962b JB |
2282 | static inline u64 sta_get_stats_bytes(struct ieee80211_sta_rx_stats *rxstats) |
2283 | { | |
2284 | unsigned int start; | |
2285 | u64 value; | |
2286 | ||
2287 | do { | |
2288 | start = u64_stats_fetch_begin(&rxstats->syncp); | |
2289 | value = rxstats->bytes; | |
2290 | } while (u64_stats_fetch_retry(&rxstats->syncp, start)); | |
2291 | ||
2292 | return value; | |
2293 | } | |
2294 | ||
0fdf1493 JB |
2295 | void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo, |
2296 | bool tidstats) | |
b7ffbd7e JB |
2297 | { |
2298 | struct ieee80211_sub_if_data *sdata = sta->sdata; | |
2299 | struct ieee80211_local *local = sdata->local; | |
b7ffbd7e | 2300 | u32 thr = 0; |
c9c5962b JB |
2301 | int i, ac, cpu; |
2302 | struct ieee80211_sta_rx_stats *last_rxstats; | |
2303 | ||
2304 | last_rxstats = sta_get_last_rx_stats(sta); | |
b7ffbd7e JB |
2305 | |
2306 | sinfo->generation = sdata->local->sta_generation; | |
2307 | ||
225b8189 JB |
2308 | /* do before driver, so beacon filtering drivers have a |
2309 | * chance to e.g. just add the number of filtered beacons | |
2310 | * (or just modify the value entirely, of course) | |
2311 | */ | |
2312 | if (sdata->vif.type == NL80211_IFTYPE_STATION) | |
bfd8403a | 2313 | sinfo->rx_beacon = sdata->deflink.u.mgd.count_beacon_signal; |
225b8189 | 2314 | |
2b9a7e1b | 2315 | drv_sta_statistics(local, sdata, &sta->sta, sinfo); |
a4217750 OE |
2316 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME) | |
2317 | BIT_ULL(NL80211_STA_INFO_STA_FLAGS) | | |
2318 | BIT_ULL(NL80211_STA_INFO_BSS_PARAM) | | |
2319 | BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) | | |
9cf02338 | 2320 | BIT_ULL(NL80211_STA_INFO_ASSOC_AT_BOOTTIME) | |
a4217750 | 2321 | BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC); |
976bd9ef JB |
2322 | |
2323 | if (sdata->vif.type == NL80211_IFTYPE_STATION) { | |
bfd8403a JB |
2324 | sinfo->beacon_loss_count = |
2325 | sdata->deflink.u.mgd.beacon_loss_count; | |
a4217750 | 2326 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_LOSS); |
976bd9ef | 2327 | } |
b7ffbd7e | 2328 | |
84b00607 | 2329 | sinfo->connected_time = ktime_get_seconds() - sta->last_connected; |
9cf02338 | 2330 | sinfo->assoc_at = sta->assoc_at; |
e5a9f8d0 | 2331 | sinfo->inactive_time = |
b8da6b6a | 2332 | jiffies_to_msecs(jiffies - ieee80211_sta_last_active(sta)); |
2b9a7e1b | 2333 | |
a4217750 OE |
2334 | if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES64) | |
2335 | BIT_ULL(NL80211_STA_INFO_TX_BYTES)))) { | |
2b9a7e1b JB |
2336 | sinfo->tx_bytes = 0; |
2337 | for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) | |
046d2e7c | 2338 | sinfo->tx_bytes += sta->deflink.tx_stats.bytes[ac]; |
a4217750 | 2339 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES64); |
2b9a7e1b JB |
2340 | } |
2341 | ||
a4217750 | 2342 | if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_PACKETS))) { |
2b9a7e1b JB |
2343 | sinfo->tx_packets = 0; |
2344 | for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) | |
046d2e7c | 2345 | sinfo->tx_packets += sta->deflink.tx_stats.packets[ac]; |
a4217750 | 2346 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS); |
2b9a7e1b JB |
2347 | } |
2348 | ||
a4217750 OE |
2349 | if (!(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES64) | |
2350 | BIT_ULL(NL80211_STA_INFO_RX_BYTES)))) { | |
046d2e7c | 2351 | sinfo->rx_bytes += sta_get_stats_bytes(&sta->deflink.rx_stats); |
c9c5962b | 2352 | |
046d2e7c | 2353 | if (sta->deflink.pcpu_rx_stats) { |
c9c5962b JB |
2354 | for_each_possible_cpu(cpu) { |
2355 | struct ieee80211_sta_rx_stats *cpurxs; | |
2356 | ||
046d2e7c S |
2357 | cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, |
2358 | cpu); | |
c9c5962b JB |
2359 | sinfo->rx_bytes += sta_get_stats_bytes(cpurxs); |
2360 | } | |
2361 | } | |
0f9c5a61 | 2362 | |
a4217750 | 2363 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES64); |
2b9a7e1b JB |
2364 | } |
2365 | ||
a4217750 | 2366 | if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_PACKETS))) { |
046d2e7c S |
2367 | sinfo->rx_packets = sta->deflink.rx_stats.packets; |
2368 | if (sta->deflink.pcpu_rx_stats) { | |
c9c5962b JB |
2369 | for_each_possible_cpu(cpu) { |
2370 | struct ieee80211_sta_rx_stats *cpurxs; | |
2371 | ||
046d2e7c S |
2372 | cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, |
2373 | cpu); | |
c9c5962b JB |
2374 | sinfo->rx_packets += cpurxs->packets; |
2375 | } | |
2376 | } | |
a4217750 | 2377 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS); |
2b9a7e1b JB |
2378 | } |
2379 | ||
a4217750 | 2380 | if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_RETRIES))) { |
046d2e7c | 2381 | sinfo->tx_retries = sta->deflink.status_stats.retry_count; |
a4217750 | 2382 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES); |
2b9a7e1b JB |
2383 | } |
2384 | ||
a4217750 | 2385 | if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))) { |
046d2e7c | 2386 | sinfo->tx_failed = sta->deflink.status_stats.retry_failed; |
a4217750 | 2387 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED); |
b7ffbd7e | 2388 | } |
2b9a7e1b | 2389 | |
b4809e94 THJ |
2390 | if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_DURATION))) { |
2391 | for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) | |
2392 | sinfo->rx_duration += sta->airtime[ac].rx_airtime; | |
2393 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION); | |
2394 | } | |
2395 | ||
2396 | if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_DURATION))) { | |
2397 | for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) | |
2398 | sinfo->tx_duration += sta->airtime[ac].tx_airtime; | |
2399 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION); | |
2400 | } | |
2401 | ||
2402 | if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT))) { | |
2433647b | 2403 | sinfo->airtime_weight = sta->airtime[0].weight; |
b4809e94 THJ |
2404 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_WEIGHT); |
2405 | } | |
2406 | ||
046d2e7c S |
2407 | sinfo->rx_dropped_misc = sta->deflink.rx_stats.dropped; |
2408 | if (sta->deflink.pcpu_rx_stats) { | |
c9c5962b JB |
2409 | for_each_possible_cpu(cpu) { |
2410 | struct ieee80211_sta_rx_stats *cpurxs; | |
2411 | ||
046d2e7c | 2412 | cpurxs = per_cpu_ptr(sta->deflink.pcpu_rx_stats, cpu); |
e165bc02 | 2413 | sinfo->rx_dropped_misc += cpurxs->dropped; |
c9c5962b JB |
2414 | } |
2415 | } | |
b7ffbd7e | 2416 | |
225b8189 JB |
2417 | if (sdata->vif.type == NL80211_IFTYPE_STATION && |
2418 | !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) { | |
a4217750 OE |
2419 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) | |
2420 | BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG); | |
225b8189 JB |
2421 | sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif); |
2422 | } | |
2423 | ||
30686bf7 JB |
2424 | if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) || |
2425 | ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) { | |
a4217750 | 2426 | if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL))) { |
c9c5962b | 2427 | sinfo->signal = (s8)last_rxstats->last_signal; |
a4217750 | 2428 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL); |
2b9a7e1b JB |
2429 | } |
2430 | ||
046d2e7c | 2431 | if (!sta->deflink.pcpu_rx_stats && |
a4217750 | 2432 | !(sinfo->filled & BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG))) { |
40d9a38a | 2433 | sinfo->signal_avg = |
046d2e7c | 2434 | -ewma_signal_read(&sta->deflink.rx_stats_avg.signal); |
a4217750 | 2435 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG); |
2b9a7e1b | 2436 | } |
b7ffbd7e | 2437 | } |
2b9a7e1b | 2438 | |
c9c5962b JB |
2439 | /* for the average - if pcpu_rx_stats isn't set - rxstats must point to |
2440 | * the sta->rx_stats struct, so the check here is fine with and without | |
2441 | * pcpu statistics | |
2442 | */ | |
2443 | if (last_rxstats->chains && | |
a4217750 OE |
2444 | !(sinfo->filled & (BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL) | |
2445 | BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) { | |
2446 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL); | |
046d2e7c | 2447 | if (!sta->deflink.pcpu_rx_stats) |
a4217750 | 2448 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG); |
c9c5962b JB |
2449 | |
2450 | sinfo->chains = last_rxstats->chains; | |
b7ffbd7e | 2451 | |
b7ffbd7e | 2452 | for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) { |
e5a9f8d0 | 2453 | sinfo->chain_signal[i] = |
c9c5962b | 2454 | last_rxstats->chain_signal_last[i]; |
b7ffbd7e | 2455 | sinfo->chain_signal_avg[i] = |
046d2e7c | 2456 | -ewma_signal_read(&sta->deflink.rx_stats_avg.chain_signal[i]); |
b7ffbd7e JB |
2457 | } |
2458 | } | |
2459 | ||
a4217750 | 2460 | if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) { |
046d2e7c | 2461 | sta_set_rate_info_tx(sta, &sta->deflink.tx_stats.last_rate, |
e5a9f8d0 | 2462 | &sinfo->txrate); |
a4217750 | 2463 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE); |
2b9a7e1b JB |
2464 | } |
2465 | ||
a4217750 | 2466 | if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE))) { |
a17d93ff | 2467 | if (sta_set_rate_info_rx(sta, &sinfo->rxrate) == 0) |
a4217750 | 2468 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE); |
2b9a7e1b | 2469 | } |
b7ffbd7e | 2470 | |
0fdf1493 | 2471 | if (tidstats && !cfg80211_sinfo_alloc_tid_stats(sinfo, GFP_KERNEL)) { |
6af8354f JB |
2472 | for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) |
2473 | sta_set_tidstats(sta, &sinfo->pertid[i], i); | |
79c892b8 JB |
2474 | } |
2475 | ||
b7ffbd7e JB |
2476 | if (ieee80211_vif_is_mesh(&sdata->vif)) { |
2477 | #ifdef CONFIG_MAC80211_MESH | |
a4217750 OE |
2478 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_LLID) | |
2479 | BIT_ULL(NL80211_STA_INFO_PLID) | | |
2480 | BIT_ULL(NL80211_STA_INFO_PLINK_STATE) | | |
2481 | BIT_ULL(NL80211_STA_INFO_LOCAL_PM) | | |
2482 | BIT_ULL(NL80211_STA_INFO_PEER_PM) | | |
dbdaee7a | 2483 | BIT_ULL(NL80211_STA_INFO_NONPEER_PM) | |
1303a51c MT |
2484 | BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_GATE) | |
2485 | BIT_ULL(NL80211_STA_INFO_CONNECTED_TO_AS); | |
b7ffbd7e | 2486 | |
433f5bc1 JB |
2487 | sinfo->llid = sta->mesh->llid; |
2488 | sinfo->plid = sta->mesh->plid; | |
2489 | sinfo->plink_state = sta->mesh->plink_state; | |
b7ffbd7e | 2490 | if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) { |
a4217750 | 2491 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_T_OFFSET); |
433f5bc1 | 2492 | sinfo->t_offset = sta->mesh->t_offset; |
b7ffbd7e | 2493 | } |
433f5bc1 JB |
2494 | sinfo->local_pm = sta->mesh->local_pm; |
2495 | sinfo->peer_pm = sta->mesh->peer_pm; | |
2496 | sinfo->nonpeer_pm = sta->mesh->nonpeer_pm; | |
dbdaee7a | 2497 | sinfo->connected_to_gate = sta->mesh->connected_to_gate; |
1303a51c | 2498 | sinfo->connected_to_as = sta->mesh->connected_to_as; |
b7ffbd7e JB |
2499 | #endif |
2500 | } | |
2501 | ||
2502 | sinfo->bss_param.flags = 0; | |
2503 | if (sdata->vif.bss_conf.use_cts_prot) | |
2504 | sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT; | |
2505 | if (sdata->vif.bss_conf.use_short_preamble) | |
2506 | sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE; | |
2507 | if (sdata->vif.bss_conf.use_short_slot) | |
2508 | sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME; | |
785e21a8 | 2509 | sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period; |
b7ffbd7e JB |
2510 | sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int; |
2511 | ||
2512 | sinfo->sta_flags.set = 0; | |
2513 | sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) | | |
2514 | BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) | | |
2515 | BIT(NL80211_STA_FLAG_WME) | | |
2516 | BIT(NL80211_STA_FLAG_MFP) | | |
2517 | BIT(NL80211_STA_FLAG_AUTHENTICATED) | | |
2518 | BIT(NL80211_STA_FLAG_ASSOCIATED) | | |
2519 | BIT(NL80211_STA_FLAG_TDLS_PEER); | |
2520 | if (test_sta_flag(sta, WLAN_STA_AUTHORIZED)) | |
2521 | sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED); | |
2522 | if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE)) | |
2523 | sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE); | |
a74a8c84 | 2524 | if (sta->sta.wme) |
b7ffbd7e JB |
2525 | sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME); |
2526 | if (test_sta_flag(sta, WLAN_STA_MFP)) | |
2527 | sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP); | |
2528 | if (test_sta_flag(sta, WLAN_STA_AUTH)) | |
2529 | sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED); | |
2530 | if (test_sta_flag(sta, WLAN_STA_ASSOC)) | |
2531 | sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED); | |
2532 | if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) | |
2533 | sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER); | |
2534 | ||
3b17fbf8 MA |
2535 | thr = sta_get_expected_throughput(sta); |
2536 | ||
2537 | if (thr != 0) { | |
a4217750 | 2538 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_EXPECTED_THROUGHPUT); |
3b17fbf8 MA |
2539 | sinfo->expected_throughput = thr; |
2540 | } | |
a78b26ff VN |
2541 | |
2542 | if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL)) && | |
046d2e7c S |
2543 | sta->deflink.status_stats.ack_signal_filled) { |
2544 | sinfo->ack_signal = sta->deflink.status_stats.last_ack_signal; | |
a78b26ff VN |
2545 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL); |
2546 | } | |
cc60dbbf | 2547 | |
9c06602b | 2548 | if (!(sinfo->filled & BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG)) && |
046d2e7c | 2549 | sta->deflink.status_stats.ack_signal_filled) { |
cc60dbbf BP |
2550 | sinfo->avg_ack_signal = |
2551 | -(s8)ewma_avg_signal_read( | |
046d2e7c | 2552 | &sta->deflink.status_stats.avg_ack_signal); |
cc60dbbf | 2553 | sinfo->filled |= |
9c06602b | 2554 | BIT_ULL(NL80211_STA_INFO_ACK_SIGNAL_AVG); |
cc60dbbf | 2555 | } |
ab60633c NM |
2556 | |
2557 | if (ieee80211_vif_is_mesh(&sdata->vif)) { | |
2558 | sinfo->filled |= BIT_ULL(NL80211_STA_INFO_AIRTIME_LINK_METRIC); | |
2559 | sinfo->airtime_link_metric = | |
2560 | airtime_link_metric_get(local, sta); | |
2561 | } | |
3b17fbf8 MA |
2562 | } |
2563 | ||
2564 | u32 sta_get_expected_throughput(struct sta_info *sta) | |
2565 | { | |
2566 | struct ieee80211_sub_if_data *sdata = sta->sdata; | |
2567 | struct ieee80211_local *local = sdata->local; | |
2568 | struct rate_control_ref *ref = NULL; | |
2569 | u32 thr = 0; | |
2570 | ||
2571 | if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) | |
2572 | ref = local->rate_ctrl; | |
2573 | ||
b7ffbd7e JB |
2574 | /* check if the driver has a SW RC implementation */ |
2575 | if (ref && ref->ops->get_expected_throughput) | |
2576 | thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv); | |
2577 | else | |
4fdbc67a | 2578 | thr = drv_get_expected_throughput(local, sta); |
b7ffbd7e | 2579 | |
3b17fbf8 | 2580 | return thr; |
b7ffbd7e | 2581 | } |
b8da6b6a JB |
2582 | |
2583 | unsigned long ieee80211_sta_last_active(struct sta_info *sta) | |
2584 | { | |
c9c5962b JB |
2585 | struct ieee80211_sta_rx_stats *stats = sta_get_last_rx_stats(sta); |
2586 | ||
046d2e7c S |
2587 | if (!sta->deflink.status_stats.last_ack || |
2588 | time_after(stats->last_rx, sta->deflink.status_stats.last_ack)) | |
c9c5962b | 2589 | return stats->last_rx; |
046d2e7c | 2590 | return sta->deflink.status_stats.last_ack; |
b8da6b6a | 2591 | } |
484a54c2 THJ |
2592 | |
2593 | static void sta_update_codel_params(struct sta_info *sta, u32 thr) | |
2594 | { | |
2595 | if (!sta->sdata->local->ops->wake_tx_queue) | |
2596 | return; | |
2597 | ||
2598 | if (thr && thr < STA_SLOW_THRESHOLD * sta->local->num_sta) { | |
2599 | sta->cparams.target = MS2TIME(50); | |
2600 | sta->cparams.interval = MS2TIME(300); | |
2601 | sta->cparams.ecn = false; | |
2602 | } else { | |
2603 | sta->cparams.target = MS2TIME(20); | |
2604 | sta->cparams.interval = MS2TIME(100); | |
2605 | sta->cparams.ecn = true; | |
2606 | } | |
2607 | } | |
2608 | ||
2609 | void ieee80211_sta_set_expected_throughput(struct ieee80211_sta *pubsta, | |
2610 | u32 thr) | |
2611 | { | |
2612 | struct sta_info *sta = container_of(pubsta, struct sta_info, sta); | |
2613 | ||
2614 | sta_update_codel_params(sta, thr); | |
2615 | } |