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