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