mac80211: fix AP mode action frame handling
[openwrt.git] / package / mac80211 / patches / 300-pending_work.patch
1 --- a/drivers/net/wireless/ath/ath9k/ath9k.h
2 +++ b/drivers/net/wireless/ath/ath9k/ath9k.h
3 @@ -234,6 +234,7 @@ struct ath_buf {
4         dma_addr_t bf_daddr;            /* physical addr of desc */
5         dma_addr_t bf_buf_addr; /* physical addr of data buffer, for DMA */
6         bool bf_stale;
7 +       struct ieee80211_tx_rate rates[4];
8         struct ath_buf_state bf_state;
9  };
10  
11 @@ -658,11 +659,10 @@ enum sc_op_flags {
12  struct ath_rate_table;
13  
14  struct ath9k_vif_iter_data {
15 -       const u8 *hw_macaddr; /* phy's hardware address, set
16 -                              * before starting iteration for
17 -                              * valid bssid mask.
18 -                              */
19 +       u8 hw_macaddr[ETH_ALEN]; /* address of the first vif */
20         u8 mask[ETH_ALEN]; /* bssid mask */
21 +       bool has_hw_macaddr;
22 +
23         int naps;      /* number of AP vifs */
24         int nmeshes;   /* number of mesh vifs */
25         int nstations; /* number of station vifs */
26 --- a/drivers/net/wireless/ath/ath9k/hw.c
27 +++ b/drivers/net/wireless/ath/ath9k/hw.c
28 @@ -1366,7 +1366,10 @@ static bool ath9k_hw_set_reset(struct at
29  
30         REGWRITE_BUFFER_FLUSH(ah);
31  
32 -       udelay(50);
33 +       if (AR_SREV_9100(ah))
34 +               mdelay(10);
35 +       else
36 +               udelay(50);
37  
38         REG_WRITE(ah, AR_RTC_RC, 0);
39         if (!ath9k_hw_wait(ah, AR_RTC_RC, AR_RTC_RC_M, 0, AH_WAIT_TIMEOUT)) {
40 @@ -1377,8 +1380,12 @@ static bool ath9k_hw_set_reset(struct at
41         if (!AR_SREV_9100(ah))
42                 REG_WRITE(ah, AR_RC, 0);
43  
44 -       if (AR_SREV_9100(ah))
45 +       if (AR_SREV_9100(ah) && type != ATH9K_RESET_WARM) {
46 +               if (ah->external_reset)
47 +                       ah->external_reset();
48 +
49                 udelay(50);
50 +       }
51  
52         return true;
53  }
54 @@ -1464,7 +1471,8 @@ static bool ath9k_hw_chip_reset(struct a
55                         reset_type = ATH9K_RESET_POWER_ON;
56                 else
57                         reset_type = ATH9K_RESET_COLD;
58 -       } else if (ah->chip_fullsleep || REG_READ(ah, AR_Q_TXE) ||
59 +       } else if (ah->chip_fullsleep ||
60 +                  REG_READ(ah, AR_Q_TXE) ||
61                    (REG_READ(ah, AR_CR) & AR_CR_RXE))
62                 reset_type = ATH9K_RESET_COLD;
63  
64 @@ -1698,12 +1706,11 @@ static void ath9k_hw_reset_opmode(struct
65  
66         ENABLE_REGWRITE_BUFFER(ah);
67  
68 -       REG_WRITE(ah, AR_STA_ID0, get_unaligned_le32(common->macaddr));
69 -       REG_WRITE(ah, AR_STA_ID1, get_unaligned_le16(common->macaddr + 4)
70 -                 | macStaId1
71 +       REG_RMW(ah, AR_STA_ID1, macStaId1
72                   | AR_STA_ID1_RTS_USE_DEF
73                   | (ah->config.ack_6mb ? AR_STA_ID1_ACKCTS_6MB : 0)
74 -                 | ah->sta_id1_defaults);
75 +                 | ah->sta_id1_defaults,
76 +                 ~AR_STA_ID1_SADH_MASK);
77         ath_hw_setbssidmask(common);
78         REG_WRITE(ah, AR_DEF_ANTENNA, saveDefAntenna);
79         ath9k_hw_write_associd(ah);
80 --- a/drivers/net/wireless/ath/ath9k/main.c
81 +++ b/drivers/net/wireless/ath/ath9k/main.c
82 @@ -839,10 +839,14 @@ static void ath9k_vif_iter(void *data, u
83         struct ath9k_vif_iter_data *iter_data = data;
84         int i;
85  
86 -       if (iter_data->hw_macaddr)
87 +       if (iter_data->has_hw_macaddr) {
88                 for (i = 0; i < ETH_ALEN; i++)
89                         iter_data->mask[i] &=
90                                 ~(iter_data->hw_macaddr[i] ^ mac[i]);
91 +       } else {
92 +               memcpy(iter_data->hw_macaddr, mac, ETH_ALEN);
93 +               iter_data->has_hw_macaddr = true;
94 +       }
95  
96         switch (vif->type) {
97         case NL80211_IFTYPE_AP:
98 @@ -891,7 +895,6 @@ void ath9k_calculate_iter_data(struct ie
99          * together with the BSSID mask when matching addresses.
100          */
101         memset(iter_data, 0, sizeof(*iter_data));
102 -       iter_data->hw_macaddr = common->macaddr;
103         memset(&iter_data->mask, 0xff, ETH_ALEN);
104  
105         if (vif)
106 @@ -901,6 +904,8 @@ void ath9k_calculate_iter_data(struct ie
107         ieee80211_iterate_active_interfaces_atomic(
108                 sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
109                 ath9k_vif_iter, iter_data);
110 +
111 +       memcpy(common->macaddr, iter_data->hw_macaddr, ETH_ALEN);
112  }
113  
114  /* Called with sc->mutex held. */
115 @@ -1327,6 +1332,7 @@ static int ath9k_sta_add(struct ieee8021
116         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
117         struct ath_node *an = (struct ath_node *) sta->drv_priv;
118         struct ieee80211_key_conf ps_key = { };
119 +       int key;
120  
121         ath_node_attach(sc, sta, vif);
122  
123 @@ -1334,7 +1340,9 @@ static int ath9k_sta_add(struct ieee8021
124             vif->type != NL80211_IFTYPE_AP_VLAN)
125                 return 0;
126  
127 -       an->ps_key = ath_key_config(common, vif, sta, &ps_key);
128 +       key = ath_key_config(common, vif, sta, &ps_key);
129 +       if (key > 0)
130 +               an->ps_key = key;
131  
132         return 0;
133  }
134 @@ -1351,6 +1359,7 @@ static void ath9k_del_ps_key(struct ath_
135             return;
136  
137         ath_key_delete(common, &ps_key);
138 +       an->ps_key = 0;
139  }
140  
141  static int ath9k_sta_remove(struct ieee80211_hw *hw,
142 --- a/drivers/net/wireless/ath/ath9k/reg.h
143 +++ b/drivers/net/wireless/ath/ath9k/reg.h
144 @@ -1493,9 +1493,6 @@ enum {
145  #define AR9271_RADIO_RF_RST                    0x20
146  #define AR9271_GATE_MAC_CTL                    0x4000
147  
148 -#define AR_STA_ID0                 0x8000
149 -#define AR_STA_ID1                 0x8004
150 -#define AR_STA_ID1_SADH_MASK       0x0000FFFF
151  #define AR_STA_ID1_STA_AP          0x00010000
152  #define AR_STA_ID1_ADHOC           0x00020000
153  #define AR_STA_ID1_PWR_SAV         0x00040000
154 --- a/drivers/net/wireless/ath/hw.c
155 +++ b/drivers/net/wireless/ath/hw.c
156 @@ -118,6 +118,12 @@
157  void ath_hw_setbssidmask(struct ath_common *common)
158  {
159         void *ah = common->ah;
160 +       u32 id1;
161 +
162 +       REG_WRITE(ah, AR_STA_ID0, get_unaligned_le32(common->macaddr));
163 +       id1 = REG_READ(ah, AR_STA_ID1) & ~AR_STA_ID1_SADH_MASK;
164 +       id1 |= get_unaligned_le16(common->macaddr + 4);
165 +       REG_WRITE(ah, AR_STA_ID1, id1);
166  
167         REG_WRITE(ah, AR_BSSMSKL, get_unaligned_le32(common->bssidmask));
168         REG_WRITE(ah, AR_BSSMSKU, get_unaligned_le16(common->bssidmask + 4));
169 --- a/drivers/net/wireless/ath/reg.h
170 +++ b/drivers/net/wireless/ath/reg.h
171 @@ -23,6 +23,10 @@
172  #define AR_MIBC_CMC            0x00000004
173  #define AR_MIBC_MCS            0x00000008
174  
175 +#define AR_STA_ID0             0x8000
176 +#define AR_STA_ID1             0x8004
177 +#define AR_STA_ID1_SADH_MASK   0x0000ffff
178 +
179  /*
180   * BSSID mask registers. See ath_hw_set_bssid_mask()
181   * for detailed documentation about these registers.
182 --- a/drivers/net/wireless/iwlegacy/4965-mac.c
183 +++ b/drivers/net/wireless/iwlegacy/4965-mac.c
184 @@ -6059,7 +6059,7 @@ il4965_mac_channel_switch(struct ieee802
185         struct il_priv *il = hw->priv;
186         const struct il_channel_info *ch_info;
187         struct ieee80211_conf *conf = &hw->conf;
188 -       struct ieee80211_channel *channel = ch_switch->channel;
189 +       struct ieee80211_channel *channel = ch_switch->chandef.chan;
190         struct il_ht_config *ht_conf = &il->current_ht_config;
191         u16 ch;
192  
193 @@ -6096,23 +6096,21 @@ il4965_mac_channel_switch(struct ieee802
194         il->current_ht_config.smps = conf->smps_mode;
195  
196         /* Configure HT40 channels */
197 -       il->ht.enabled = conf_is_ht(conf);
198 -       if (il->ht.enabled) {
199 -               if (conf_is_ht40_minus(conf)) {
200 -                       il->ht.extension_chan_offset =
201 -                           IEEE80211_HT_PARAM_CHA_SEC_BELOW;
202 -                       il->ht.is_40mhz = true;
203 -               } else if (conf_is_ht40_plus(conf)) {
204 -                       il->ht.extension_chan_offset =
205 -                           IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
206 -                       il->ht.is_40mhz = true;
207 -               } else {
208 -                       il->ht.extension_chan_offset =
209 -                           IEEE80211_HT_PARAM_CHA_SEC_NONE;
210 -                       il->ht.is_40mhz = false;
211 -               }
212 -       } else
213 +       switch (cfg80211_get_chandef_type(&ch_switch->chandef)) {
214 +       case NL80211_CHAN_NO_HT:
215 +       case NL80211_CHAN_HT20:
216                 il->ht.is_40mhz = false;
217 +               il->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
218 +               break;
219 +       case NL80211_CHAN_HT40MINUS:
220 +               il->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
221 +               il->ht.is_40mhz = true;
222 +               break;
223 +       case NL80211_CHAN_HT40PLUS:
224 +               il->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
225 +               il->ht.is_40mhz = true;
226 +               break;
227 +       }
228  
229         if ((le16_to_cpu(il->staging.channel) != ch))
230                 il->staging.flags = 0;
231 --- a/drivers/net/wireless/iwlegacy/4965.c
232 +++ b/drivers/net/wireless/iwlegacy/4965.c
233 @@ -1493,7 +1493,7 @@ il4965_hw_channel_switch(struct il_priv 
234  
235         cmd.band = band;
236         cmd.expect_beacon = 0;
237 -       ch = ch_switch->channel->hw_value;
238 +       ch = ch_switch->chandef.chan->hw_value;
239         cmd.channel = cpu_to_le16(ch);
240         cmd.rxon_flags = il->staging.flags;
241         cmd.rxon_filter_flags = il->staging.filter_flags;
242 --- a/drivers/net/wireless/iwlwifi/dvm/devices.c
243 +++ b/drivers/net/wireless/iwlwifi/dvm/devices.c
244 @@ -379,7 +379,7 @@ static int iwl5000_hw_channel_switch(str
245         };
246  
247         cmd.band = priv->band == IEEE80211_BAND_2GHZ;
248 -       ch = ch_switch->channel->hw_value;
249 +       ch = ch_switch->chandef.chan->hw_value;
250         IWL_DEBUG_11H(priv, "channel switch from %d to %d\n",
251                       ctx->active.channel, ch);
252         cmd.channel = cpu_to_le16(ch);
253 @@ -414,7 +414,8 @@ static int iwl5000_hw_channel_switch(str
254         }
255         IWL_DEBUG_11H(priv, "uCode time for the switch is 0x%x\n",
256                       cmd.switch_time);
257 -       cmd.expect_beacon = ch_switch->channel->flags & IEEE80211_CHAN_RADAR;
258 +       cmd.expect_beacon =
259 +               ch_switch->chandef.chan->flags & IEEE80211_CHAN_RADAR;
260  
261         return iwl_dvm_send_cmd(priv, &hcmd);
262  }
263 @@ -540,7 +541,7 @@ static int iwl6000_hw_channel_switch(str
264         hcmd.data[0] = cmd;
265  
266         cmd->band = priv->band == IEEE80211_BAND_2GHZ;
267 -       ch = ch_switch->channel->hw_value;
268 +       ch = ch_switch->chandef.chan->hw_value;
269         IWL_DEBUG_11H(priv, "channel switch from %u to %u\n",
270                       ctx->active.channel, ch);
271         cmd->channel = cpu_to_le16(ch);
272 @@ -575,7 +576,8 @@ static int iwl6000_hw_channel_switch(str
273         }
274         IWL_DEBUG_11H(priv, "uCode time for the switch is 0x%x\n",
275                       cmd->switch_time);
276 -       cmd->expect_beacon = ch_switch->channel->flags & IEEE80211_CHAN_RADAR;
277 +       cmd->expect_beacon =
278 +               ch_switch->chandef.chan->flags & IEEE80211_CHAN_RADAR;
279  
280         err = iwl_dvm_send_cmd(priv, &hcmd);
281         kfree(cmd);
282 --- a/drivers/net/wireless/iwlwifi/dvm/mac80211.c
283 +++ b/drivers/net/wireless/iwlwifi/dvm/mac80211.c
284 @@ -970,7 +970,7 @@ static void iwlagn_mac_channel_switch(st
285  {
286         struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
287         struct ieee80211_conf *conf = &hw->conf;
288 -       struct ieee80211_channel *channel = ch_switch->channel;
289 +       struct ieee80211_channel *channel = ch_switch->chandef.chan;
290         struct iwl_ht_config *ht_conf = &priv->current_ht_config;
291         /*
292          * MULTI-FIXME
293 @@ -1008,11 +1008,21 @@ static void iwlagn_mac_channel_switch(st
294         priv->current_ht_config.smps = conf->smps_mode;
295  
296         /* Configure HT40 channels */
297 -       ctx->ht.enabled = conf_is_ht(conf);
298 -       if (ctx->ht.enabled)
299 -               iwlagn_config_ht40(conf, ctx);
300 -       else
301 +       switch (cfg80211_get_chandef_type(&ch_switch->chandef)) {
302 +       case NL80211_CHAN_NO_HT:
303 +       case NL80211_CHAN_HT20:
304                 ctx->ht.is_40mhz = false;
305 +               ctx->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
306 +               break;
307 +       case NL80211_CHAN_HT40MINUS:
308 +               ctx->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
309 +               ctx->ht.is_40mhz = true;
310 +               break;
311 +       case NL80211_CHAN_HT40PLUS:
312 +               ctx->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
313 +               ctx->ht.is_40mhz = true;
314 +               break;
315 +       }
316  
317         if ((le16_to_cpu(ctx->staging.channel) != ch))
318                 ctx->staging.flags = 0;
319 --- a/drivers/net/wireless/iwlwifi/dvm/rxon.c
320 +++ b/drivers/net/wireless/iwlwifi/dvm/rxon.c
321 @@ -1160,7 +1160,7 @@ int iwlagn_commit_rxon(struct iwl_priv *
322  }
323  
324  void iwlagn_config_ht40(struct ieee80211_conf *conf,
325 -       struct iwl_rxon_context *ctx)
326 +                       struct iwl_rxon_context *ctx)
327  {
328         if (conf_is_ht40_minus(conf)) {
329                 ctx->ht.extension_chan_offset =
330 --- a/drivers/net/wireless/mac80211_hwsim.c
331 +++ b/drivers/net/wireless/mac80211_hwsim.c
332 @@ -25,6 +25,7 @@
333  #include <linux/if_arp.h>
334  #include <linux/rtnetlink.h>
335  #include <linux/etherdevice.h>
336 +#include <linux/platform_device.h>
337  #include <linux/debugfs.h>
338  #include <linux/module.h>
339  #include <linux/ktime.h>
340 @@ -717,9 +718,17 @@ static bool mac80211_hwsim_tx_frame_no_n
341         rx_status.flag |= RX_FLAG_MACTIME_START;
342         rx_status.freq = chan->center_freq;
343         rx_status.band = chan->band;
344 -       rx_status.rate_idx = info->control.rates[0].idx;
345 -       if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
346 -               rx_status.flag |= RX_FLAG_HT;
347 +       if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
348 +               rx_status.rate_idx =
349 +                       ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
350 +               rx_status.vht_nss =
351 +                       ieee80211_rate_get_vht_nss(&info->control.rates[0]);
352 +               rx_status.flag |= RX_FLAG_VHT;
353 +       } else {
354 +               rx_status.rate_idx = info->control.rates[0].idx;
355 +               if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
356 +                       rx_status.flag |= RX_FLAG_HT;
357 +       }
358         if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
359                 rx_status.flag |= RX_FLAG_40MHZ;
360         if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
361 @@ -1687,6 +1696,7 @@ static void mac80211_hwsim_free(void)
362                 debugfs_remove(data->debugfs_ps);
363                 debugfs_remove(data->debugfs);
364                 ieee80211_unregister_hw(data->hw);
365 +               device_release_driver(data->dev);
366                 device_unregister(data->dev);
367                 ieee80211_free_hw(data->hw);
368         }
369 @@ -1695,7 +1705,9 @@ static void mac80211_hwsim_free(void)
370  
371  
372  static struct device_driver mac80211_hwsim_driver = {
373 -       .name = "mac80211_hwsim"
374 +       .name = "mac80211_hwsim",
375 +       .bus = &platform_bus_type,
376 +       .owner = THIS_MODULE,
377  };
378  
379  static const struct net_device_ops hwsim_netdev_ops = {
380 @@ -2187,9 +2199,15 @@ static int __init init_mac80211_hwsim(vo
381         spin_lock_init(&hwsim_radio_lock);
382         INIT_LIST_HEAD(&hwsim_radios);
383  
384 +       err = driver_register(&mac80211_hwsim_driver);
385 +       if (err)
386 +               return err;
387 +
388         hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
389 -       if (IS_ERR(hwsim_class))
390 -               return PTR_ERR(hwsim_class);
391 +       if (IS_ERR(hwsim_class)) {
392 +               err = PTR_ERR(hwsim_class);
393 +               goto failed_unregister_driver;
394 +       }
395  
396         memset(addr, 0, ETH_ALEN);
397         addr[0] = 0x02;
398 @@ -2211,12 +2229,20 @@ static int __init init_mac80211_hwsim(vo
399                                           "hwsim%d", i);
400                 if (IS_ERR(data->dev)) {
401                         printk(KERN_DEBUG
402 -                              "mac80211_hwsim: device_create "
403 -                              "failed (%ld)\n", PTR_ERR(data->dev));
404 +                              "mac80211_hwsim: device_create failed (%ld)\n",
405 +                              PTR_ERR(data->dev));
406                         err = -ENOMEM;
407                         goto failed_drvdata;
408                 }
409                 data->dev->driver = &mac80211_hwsim_driver;
410 +               err = device_bind_driver(data->dev);
411 +               if (err != 0) {
412 +                       printk(KERN_DEBUG
413 +                              "mac80211_hwsim: device_bind_driver failed (%d)\n",
414 +                              err);
415 +                       goto failed_hw;
416 +               }
417 +
418                 skb_queue_head_init(&data->pending);
419  
420                 SET_IEEE80211_DEV(hw, data->dev);
421 @@ -2515,6 +2541,8 @@ failed_drvdata:
422         ieee80211_free_hw(hw);
423  failed:
424         mac80211_hwsim_free();
425 +failed_unregister_driver:
426 +       driver_unregister(&mac80211_hwsim_driver);
427         return err;
428  }
429  module_init(init_mac80211_hwsim);
430 @@ -2527,5 +2555,6 @@ static void __exit exit_mac80211_hwsim(v
431  
432         mac80211_hwsim_free();
433         unregister_netdev(hwsim_mon);
434 +       driver_unregister(&mac80211_hwsim_driver);
435  }
436  module_exit(exit_mac80211_hwsim);
437 --- a/drivers/net/wireless/ti/wl12xx/cmd.c
438 +++ b/drivers/net/wireless/ti/wl12xx/cmd.c
439 @@ -301,7 +301,7 @@ int wl12xx_cmd_channel_switch(struct wl1
440         }
441  
442         cmd->role_id = wlvif->role_id;
443 -       cmd->channel = ch_switch->channel->hw_value;
444 +       cmd->channel = ch_switch->chandef.chan->hw_value;
445         cmd->switch_time = ch_switch->count;
446         cmd->stop_tx = ch_switch->block_tx;
447  
448 --- a/drivers/net/wireless/ti/wl18xx/cmd.c
449 +++ b/drivers/net/wireless/ti/wl18xx/cmd.c
450 @@ -42,11 +42,11 @@ int wl18xx_cmd_channel_switch(struct wl1
451         }
452  
453         cmd->role_id = wlvif->role_id;
454 -       cmd->channel = ch_switch->channel->hw_value;
455 +       cmd->channel = ch_switch->chandef.chan->hw_value;
456         cmd->switch_time = ch_switch->count;
457         cmd->stop_tx = ch_switch->block_tx;
458  
459 -       switch (ch_switch->channel->band) {
460 +       switch (ch_switch->chandef.chan->band) {
461         case IEEE80211_BAND_2GHZ:
462                 cmd->band = WLCORE_BAND_2_4GHZ;
463                 break;
464 @@ -55,7 +55,7 @@ int wl18xx_cmd_channel_switch(struct wl1
465                 break;
466         default:
467                 wl1271_error("invalid channel switch band: %d",
468 -                            ch_switch->channel->band);
469 +                            ch_switch->chandef.chan->band);
470                 ret = -EINVAL;
471                 goto out_free;
472         }
473 --- a/include/linux/ieee80211.h
474 +++ b/include/linux/ieee80211.h
475 @@ -673,6 +673,36 @@ struct ieee80211_channel_sw_ie {
476  } __packed;
477  
478  /**
479 + * struct ieee80211_ext_chansw_ie
480 + *
481 + * This structure represents the "Extended Channel Switch Announcement element"
482 + */
483 +struct ieee80211_ext_chansw_ie {
484 +       u8 mode;
485 +       u8 new_operating_class;
486 +       u8 new_ch_num;
487 +       u8 count;
488 +} __packed;
489 +
490 +/**
491 + * struct ieee80211_sec_chan_offs_ie - secondary channel offset IE
492 + * @sec_chan_offs: secondary channel offset, uses IEEE80211_HT_PARAM_CHA_SEC_*
493 + *     values here
494 + * This structure represents the "Secondary Channel Offset element"
495 + */
496 +struct ieee80211_sec_chan_offs_ie {
497 +       u8 sec_chan_offs;
498 +} __packed;
499 +
500 +/**
501 + * struct ieee80211_wide_bw_chansw_ie - wide bandwidth channel switch IE
502 + */
503 +struct ieee80211_wide_bw_chansw_ie {
504 +       u8 new_channel_width;
505 +       u8 new_center_freq_seg0, new_center_freq_seg1;
506 +} __packed;
507 +
508 +/**
509   * struct ieee80211_tim
510   *
511   * This structure refers to "Traffic Indication Map information element"
512 @@ -840,12 +870,15 @@ struct ieee80211_mgmt {
513                                 } __packed wme_action;
514                                 struct{
515                                         u8 action_code;
516 -                                       u8 element_id;
517 -                                       u8 length;
518 -                                       struct ieee80211_channel_sw_ie sw_elem;
519 +                                       u8 variable[0];
520                                 } __packed chan_switch;
521                                 struct{
522                                         u8 action_code;
523 +                                       struct ieee80211_ext_chansw_ie data;
524 +                                       u8 variable[0];
525 +                               } __packed ext_chan_switch;
526 +                               struct{
527 +                                       u8 action_code;
528                                         u8 dialog_token;
529                                         u8 element_id;
530                                         u8 length;
531 @@ -1638,6 +1671,7 @@ enum ieee80211_eid {
532  
533         WLAN_EID_HT_CAPABILITY = 45,
534         WLAN_EID_HT_OPERATION = 61,
535 +       WLAN_EID_SECONDARY_CHANNEL_OFFSET = 62,
536  
537         WLAN_EID_RSN = 48,
538         WLAN_EID_MMIE = 76,
539 @@ -1672,6 +1706,8 @@ enum ieee80211_eid {
540         WLAN_EID_VHT_CAPABILITY = 191,
541         WLAN_EID_VHT_OPERATION = 192,
542         WLAN_EID_OPMODE_NOTIF = 199,
543 +       WLAN_EID_WIDE_BW_CHANNEL_SWITCH = 194,
544 +       WLAN_EID_CHANNEL_SWITCH_WRAPPER = 196,
545  
546         /* 802.11ad */
547         WLAN_EID_NON_TX_BSSID_CAP =  83,
548 @@ -1795,6 +1831,7 @@ enum ieee80211_key_len {
549  
550  /* Public action codes */
551  enum ieee80211_pub_actioncode {
552 +       WLAN_PUB_ACTION_EXT_CHANSW_ANN = 4,
553         WLAN_PUB_ACTION_TDLS_DISCOVER_RES = 14,
554  };
555  
556 --- a/include/net/cfg80211.h
557 +++ b/include/net/cfg80211.h
558 @@ -753,6 +753,8 @@ int cfg80211_check_station_change(struct
559   * @STATION_INFO_LOCAL_PM: @local_pm filled
560   * @STATION_INFO_PEER_PM: @peer_pm filled
561   * @STATION_INFO_NONPEER_PM: @nonpeer_pm filled
562 + * @STATION_INFO_CHAIN_SIGNAL: @chain_signal filled
563 + * @STATION_INFO_CHAIN_SIGNAL_AVG: @chain_signal_avg filled
564   */
565  enum station_info_flags {
566         STATION_INFO_INACTIVE_TIME      = 1<<0,
567 @@ -781,6 +783,8 @@ enum station_info_flags {
568         STATION_INFO_NONPEER_PM         = 1<<23,
569         STATION_INFO_RX_BYTES64         = 1<<24,
570         STATION_INFO_TX_BYTES64         = 1<<25,
571 +       STATION_INFO_CHAIN_SIGNAL       = 1<<26,
572 +       STATION_INFO_CHAIN_SIGNAL_AVG   = 1<<27,
573  };
574  
575  /**
576 @@ -857,6 +861,8 @@ struct sta_bss_parameters {
577         u16 beacon_interval;
578  };
579  
580 +#define IEEE80211_MAX_CHAINS   4
581 +
582  /**
583   * struct station_info - station information
584   *
585 @@ -874,6 +880,9 @@ struct sta_bss_parameters {
586   *     For CFG80211_SIGNAL_TYPE_MBM, value is expressed in _dBm_.
587   * @signal_avg: Average signal strength, type depends on the wiphy's signal_type.
588   *     For CFG80211_SIGNAL_TYPE_MBM, value is expressed in _dBm_.
589 + * @chains: bitmask for filled values in @chain_signal, @chain_signal_avg
590 + * @chain_signal: per-chain signal strength of last received packet in dBm
591 + * @chain_signal_avg: per-chain signal strength average in dBm
592   * @txrate: current unicast bitrate from this station
593   * @rxrate: current unicast bitrate to this station
594   * @rx_packets: packets received from this station
595 @@ -909,6 +918,11 @@ struct station_info {
596         u8 plink_state;
597         s8 signal;
598         s8 signal_avg;
599 +
600 +       u8 chains;
601 +       s8 chain_signal[IEEE80211_MAX_CHAINS];
602 +       s8 chain_signal_avg[IEEE80211_MAX_CHAINS];
603 +
604         struct rate_info txrate;
605         struct rate_info rxrate;
606         u32 rx_packets;
607 @@ -4027,6 +4041,17 @@ bool cfg80211_reg_can_beacon(struct wiph
608  void cfg80211_ch_switch_notify(struct net_device *dev,
609                                struct cfg80211_chan_def *chandef);
610  
611 +/**
612 + * ieee80211_operating_class_to_band - convert operating class to band
613 + *
614 + * @operating_class: the operating class to convert
615 + * @band: band pointer to fill
616 + *
617 + * Returns %true if the conversion was successful, %false otherwise.
618 + */
619 +bool ieee80211_operating_class_to_band(u8 operating_class,
620 +                                      enum ieee80211_band *band);
621 +
622  /*
623   * cfg80211_tdls_oper_request - request userspace to perform TDLS operation
624   * @dev: the device on which the operation is requested
625 --- a/include/net/mac80211.h
626 +++ b/include/net/mac80211.h
627 @@ -210,7 +210,7 @@ struct ieee80211_chanctx_conf {
628   * @BSS_CHANGED_QOS: QoS for this association was enabled/disabled. Note
629   *     that it is only ever disabled for station mode.
630   * @BSS_CHANGED_IDLE: Idle changed for this BSS/interface.
631 - * @BSS_CHANGED_SSID: SSID changed for this BSS (AP mode)
632 + * @BSS_CHANGED_SSID: SSID changed for this BSS (AP and IBSS mode)
633   * @BSS_CHANGED_AP_PROBE_RESP: Probe Response changed for this BSS (AP mode)
634   * @BSS_CHANGED_PS: PS changed for this BSS (STA mode)
635   * @BSS_CHANGED_TXPOWER: TX power setting changed for this interface
636 @@ -327,7 +327,7 @@ enum ieee80211_rssi_event {
637   *     your driver/device needs to do.
638   * @ps: power-save mode (STA only). This flag is NOT affected by
639   *     offchannel/dynamic_ps operations.
640 - * @ssid: The SSID of the current vif. Only valid in AP-mode.
641 + * @ssid: The SSID of the current vif. Valid in AP and IBSS mode.
642   * @ssid_len: Length of SSID given in @ssid.
643   * @hidden_ssid: The SSID of the current vif is hidden. Only valid in AP-mode.
644   * @txpower: TX power in dBm
645 @@ -562,6 +562,9 @@ enum mac80211_rate_control_flags {
646  /* maximum number of rate stages */
647  #define IEEE80211_TX_MAX_RATES 4
648  
649 +/* maximum number of rate table entries */
650 +#define IEEE80211_TX_RATE_TABLE_SIZE   4
651 +
652  /**
653   * struct ieee80211_tx_rate - rate selection/status
654   *
655 @@ -602,8 +605,8 @@ static inline void ieee80211_rate_set_vh
656                                           u8 mcs, u8 nss)
657  {
658         WARN_ON(mcs & ~0xF);
659 -       WARN_ON(nss & ~0x7);
660 -       rate->idx = (nss << 4) | mcs;
661 +       WARN_ON((nss - 1) & ~0x7);
662 +       rate->idx = ((nss - 1) << 4) | mcs;
663  }
664  
665  static inline u8
666 @@ -615,7 +618,7 @@ ieee80211_rate_get_vht_mcs(const struct 
667  static inline u8
668  ieee80211_rate_get_vht_nss(const struct ieee80211_tx_rate *rate)
669  {
670 -       return rate->idx >> 4;
671 +       return (rate->idx >> 4) + 1;
672  }
673  
674  /**
675 @@ -656,7 +659,11 @@ struct ieee80211_tx_info {
676                                         struct ieee80211_tx_rate rates[
677                                                 IEEE80211_TX_MAX_RATES];
678                                         s8 rts_cts_rate_idx;
679 -                                       /* 3 bytes free */
680 +                                       u8 use_rts:1;
681 +                                       u8 use_cts_prot:1;
682 +                                       u8 short_preamble:1;
683 +                                       u8 skip_table:1;
684 +                                       /* 2 bytes free */
685                                 };
686                                 /* only needed before rate control */
687                                 unsigned long jiffies;
688 @@ -677,6 +684,8 @@ struct ieee80211_tx_info {
689                 struct {
690                         struct ieee80211_tx_rate driver_rates[
691                                 IEEE80211_TX_MAX_RATES];
692 +                       u8 pad[4];
693 +
694                         void *rate_driver_data[
695                                 IEEE80211_TX_INFO_RATE_DRIVER_DATA_SIZE / sizeof(void *)];
696                 };
697 @@ -840,6 +849,9 @@ enum mac80211_rx_flags {
698   * @signal: signal strength when receiving this frame, either in dBm, in dB or
699   *     unspecified depending on the hardware capabilities flags
700   *     @IEEE80211_HW_SIGNAL_*
701 + * @chains: bitmask of receive chains for which separate signal strength
702 + *     values were filled.
703 + * @chain_signal: per-chain signal strength, same format as @signal
704   * @antenna: antenna used
705   * @rate_idx: index of data rate into band's supported rates or MCS index if
706   *     HT or VHT is used (%RX_FLAG_HT/%RX_FLAG_VHT)
707 @@ -871,6 +883,8 @@ struct ieee80211_rx_status {
708         u8 band;
709         u8 antenna;
710         s8 signal;
711 +       u8 chains;
712 +       s8 chain_signal[IEEE80211_MAX_CHAINS];
713         u8 ampdu_delimiter_crc;
714         u8 vendor_radiotap_align;
715         u8 vendor_radiotap_oui[3];
716 @@ -1018,13 +1032,13 @@ struct ieee80211_conf {
717   *     the driver passed into mac80211.
718   * @block_tx: Indicates whether transmission must be blocked before the
719   *     scheduled channel switch, as indicated by the AP.
720 - * @channel: the new channel to switch to
721 + * @chandef: the new channel to switch to
722   * @count: the number of TBTT's until the channel switch event
723   */
724  struct ieee80211_channel_switch {
725         u64 timestamp;
726         bool block_tx;
727 -       struct ieee80211_channel *channel;
728 +       struct cfg80211_chan_def chandef;
729         u8 count;
730  };
731  
732 @@ -1222,6 +1236,24 @@ enum ieee80211_sta_rx_bandwidth {
733  };
734  
735  /**
736 + * struct ieee80211_sta_rates - station rate selection table
737 + *
738 + * @rcu_head: RCU head used for freeing the table on update
739 + * @rates: transmit rates/flags to be used by default.
740 + *     Overriding entries per-packet is possible by using cb tx control.
741 + */
742 +struct ieee80211_sta_rates {
743 +       struct rcu_head rcu_head;
744 +       struct {
745 +               s8 idx;
746 +               u8 count;
747 +               u8 count_cts;
748 +               u8 count_rts;
749 +               u16 flags;
750 +       } rate[IEEE80211_TX_RATE_TABLE_SIZE];
751 +};
752 +
753 +/**
754   * struct ieee80211_sta - station table entry
755   *
756   * A station table entry represents a station we are possibly
757 @@ -1248,6 +1280,7 @@ enum ieee80211_sta_rx_bandwidth {
758   *     notifications and capabilities. The value is only valid after
759   *     the station moves to associated state.
760   * @smps_mode: current SMPS mode (off, static or dynamic)
761 + * @tx_rates: rate control selection table
762   */
763  struct ieee80211_sta {
764         u32 supp_rates[IEEE80211_NUM_BANDS];
765 @@ -1261,6 +1294,7 @@ struct ieee80211_sta {
766         u8 rx_nss;
767         enum ieee80211_sta_rx_bandwidth bandwidth;
768         enum ieee80211_smps_mode smps_mode;
769 +       struct ieee80211_sta_rates __rcu *rates;
770  
771         /* must be last */
772         u8 drv_priv[0] __aligned(sizeof(void *));
773 @@ -1416,6 +1450,9 @@ struct ieee80211_tx_control {
774   *     for different virtual interfaces. See the doc section on HW queue
775   *     control for more details.
776   *
777 + * @IEEE80211_HW_SUPPORTS_RC_TABLE: The driver supports using a rate
778 + *     selection table provided by the rate control algorithm.
779 + *
780   * @IEEE80211_HW_P2P_DEV_ADDR_FOR_INTF: Use the P2P Device address for any
781   *     P2P Interface. This will be honoured even if more than one interface
782   *     is supported.
783 @@ -1448,6 +1485,7 @@ enum ieee80211_hw_flags {
784         IEEE80211_HW_SUPPORTS_PER_STA_GTK               = 1<<21,
785         IEEE80211_HW_AP_LINK_PS                         = 1<<22,
786         IEEE80211_HW_TX_AMPDU_SETUP_IN_HW               = 1<<23,
787 +       IEEE80211_HW_SUPPORTS_RC_TABLE                  = 1<<24,
788         IEEE80211_HW_P2P_DEV_ADDR_FOR_INTF              = 1<<25,
789         IEEE80211_HW_TIMING_BEACON_ONLY                 = 1<<26,
790  };
791 @@ -3144,6 +3182,25 @@ void ieee80211_sta_set_buffered(struct i
792                                 u8 tid, bool buffered);
793  
794  /**
795 + * ieee80211_get_tx_rates - get the selected transmit rates for a packet
796 + *
797 + * Call this function in a driver with per-packet rate selection support
798 + * to combine the rate info in the packet tx info with the most recent
799 + * rate selection table for the station entry.
800 + *
801 + * @vif: &struct ieee80211_vif pointer from the add_interface callback.
802 + * @sta: the receiver station to which this packet is sent.
803 + * @skb: the frame to be transmitted.
804 + * @dest: buffer for extracted rate/retry information
805 + * @max_rates: maximum number of rates to fetch
806 + */
807 +void ieee80211_get_tx_rates(struct ieee80211_vif *vif,
808 +                           struct ieee80211_sta *sta,
809 +                           struct sk_buff *skb,
810 +                           struct ieee80211_tx_rate *dest,
811 +                           int max_rates);
812 +
813 +/**
814   * ieee80211_tx_status - transmit status callback
815   *
816   * Call this function for all transmitted frames after they have been
817 @@ -4118,7 +4175,7 @@ void ieee80211_send_bar(struct ieee80211
818   *     (deprecated; this will be removed once drivers get updated to use
819   *     rate_idx_mask)
820   * @rate_idx_mask: user-requested (legacy) rate mask
821 - * @rate_idx_mcs_mask: user-requested MCS rate mask
822 + * @rate_idx_mcs_mask: user-requested MCS rate mask (NULL if not in use)
823   * @bss: whether this frame is sent out in AP or IBSS mode
824   */
825  struct ieee80211_tx_rate_control {
826 @@ -4130,7 +4187,7 @@ struct ieee80211_tx_rate_control {
827         bool rts, short_preamble;
828         u8 max_rate_idx;
829         u32 rate_idx_mask;
830 -       u8 rate_idx_mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
831 +       u8 *rate_idx_mcs_mask;
832         bool bss;
833  };
834  
835 @@ -4219,6 +4276,22 @@ bool rate_usable_index_exists(struct iee
836         return false;
837  }
838  
839 +/**
840 + * rate_control_set_rates - pass the sta rate selection to mac80211/driver
841 + *
842 + * When not doing a rate control probe to test rates, rate control should pass
843 + * its rate selection to mac80211. If the driver supports receiving a station
844 + * rate table, it will use it to ensure that frames are always sent based on
845 + * the most recent rate control module decision.
846 + *
847 + * @hw: pointer as obtained from ieee80211_alloc_hw()
848 + * @pubsta: &struct ieee80211_sta pointer to the target destination.
849 + * @rates: new tx rate set to be used for this station.
850 + */
851 +int rate_control_set_rates(struct ieee80211_hw *hw,
852 +                          struct ieee80211_sta *pubsta,
853 +                          struct ieee80211_sta_rates *rates);
854 +
855  int ieee80211_rate_control_register(struct rate_control_ops *ops);
856  void ieee80211_rate_control_unregister(struct rate_control_ops *ops);
857  
858 --- a/net/mac80211/agg-rx.c
859 +++ b/net/mac80211/agg-rx.c
860 @@ -204,6 +204,8 @@ static void ieee80211_send_addba_resp(st
861                 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
862         else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
863                 memcpy(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN);
864 +       else if (sdata->vif.type == NL80211_IFTYPE_WDS)
865 +               memcpy(mgmt->bssid, da, ETH_ALEN);
866  
867         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
868                                           IEEE80211_STYPE_ACTION);
869 --- a/net/mac80211/agg-tx.c
870 +++ b/net/mac80211/agg-tx.c
871 @@ -81,7 +81,8 @@ static void ieee80211_send_addba_request
872         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
873         if (sdata->vif.type == NL80211_IFTYPE_AP ||
874             sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
875 -           sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
876 +           sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
877 +           sdata->vif.type == NL80211_IFTYPE_WDS)
878                 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
879         else if (sdata->vif.type == NL80211_IFTYPE_STATION)
880                 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
881 @@ -527,6 +528,7 @@ int ieee80211_start_tx_ba_session(struct
882             sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
883             sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
884             sdata->vif.type != NL80211_IFTYPE_AP &&
885 +           sdata->vif.type != NL80211_IFTYPE_WDS &&
886             sdata->vif.type != NL80211_IFTYPE_ADHOC)
887                 return -EINVAL;
888  
889 --- a/net/mac80211/cfg.c
890 +++ b/net/mac80211/cfg.c
891 @@ -444,7 +444,7 @@ static void sta_set_sinfo(struct sta_inf
892         struct ieee80211_local *local = sdata->local;
893         struct timespec uptime;
894         u64 packets = 0;
895 -       int ac;
896 +       int i, ac;
897  
898         sinfo->generation = sdata->local->sta_generation;
899  
900 @@ -488,6 +488,17 @@ static void sta_set_sinfo(struct sta_inf
901                         sinfo->signal = (s8)sta->last_signal;
902                 sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
903         }
904 +       if (sta->chains) {
905 +               sinfo->filled |= STATION_INFO_CHAIN_SIGNAL |
906 +                                STATION_INFO_CHAIN_SIGNAL_AVG;
907 +
908 +               sinfo->chains = sta->chains;
909 +               for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) {
910 +                       sinfo->chain_signal[i] = sta->chain_signal_last[i];
911 +                       sinfo->chain_signal_avg[i] =
912 +                               (s8) -ewma_read(&sta->chain_signal_avg[i]);
913 +               }
914 +       }
915  
916         sta_set_rate_info_tx(sta, &sta->last_tx_rate, &sinfo->txrate);
917         sta_set_rate_info_rx(sta, &sinfo->rxrate);
918 @@ -1052,6 +1063,7 @@ static int ieee80211_stop_ap(struct wiph
919         ieee80211_free_keys(sdata);
920  
921         sdata->vif.bss_conf.enable_beacon = false;
922 +       sdata->vif.bss_conf.ssid_len = 0;
923         clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
924         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
925  
926 @@ -2416,9 +2428,22 @@ static int ieee80211_set_bitrate_mask(st
927         }
928  
929         for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
930 +               struct ieee80211_supported_band *sband = wiphy->bands[i];
931 +               int j;
932 +
933                 sdata->rc_rateidx_mask[i] = mask->control[i].legacy;
934                 memcpy(sdata->rc_rateidx_mcs_mask[i], mask->control[i].mcs,
935                        sizeof(mask->control[i].mcs));
936 +
937 +               sdata->rc_has_mcs_mask[i] = false;
938 +               if (!sband)
939 +                       continue;
940 +
941 +               for (j = 0; j < IEEE80211_HT_MCS_MASK_LEN; j++)
942 +                       if (~sdata->rc_rateidx_mcs_mask[i][j]) {
943 +                               sdata->rc_has_mcs_mask[i] = true;
944 +                               break;
945 +                       }
946         }
947  
948         return 0;
949 --- a/net/mac80211/chan.c
950 +++ b/net/mac80211/chan.c
951 @@ -57,6 +57,22 @@ ieee80211_find_chanctx(struct ieee80211_
952         return NULL;
953  }
954  
955 +static bool ieee80211_is_radar_required(struct ieee80211_local *local)
956 +{
957 +       struct ieee80211_sub_if_data *sdata;
958 +
959 +       rcu_read_lock();
960 +       list_for_each_entry_rcu(sdata, &local->interfaces, list) {
961 +               if (sdata->radar_required) {
962 +                       rcu_read_unlock();
963 +                       return true;
964 +               }
965 +       }
966 +       rcu_read_unlock();
967 +
968 +       return false;
969 +}
970 +
971  static struct ieee80211_chanctx *
972  ieee80211_new_chanctx(struct ieee80211_local *local,
973                       const struct cfg80211_chan_def *chandef,
974 @@ -76,6 +92,9 @@ ieee80211_new_chanctx(struct ieee80211_l
975         ctx->conf.rx_chains_static = 1;
976         ctx->conf.rx_chains_dynamic = 1;
977         ctx->mode = mode;
978 +       ctx->conf.radar_enabled = ieee80211_is_radar_required(local);
979 +       if (!local->use_chanctx)
980 +               local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
981  
982         /* acquire mutex to prevent idle from changing */
983         mutex_lock(&local->mtx);
984 @@ -110,6 +129,7 @@ ieee80211_new_chanctx(struct ieee80211_l
985  static void ieee80211_free_chanctx(struct ieee80211_local *local,
986                                    struct ieee80211_chanctx *ctx)
987  {
988 +       bool check_single_channel = false;
989         lockdep_assert_held(&local->chanctx_mtx);
990  
991         WARN_ON_ONCE(ctx->refcount != 0);
992 @@ -119,6 +139,14 @@ static void ieee80211_free_chanctx(struc
993                 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
994                 chandef->center_freq1 = chandef->chan->center_freq;
995                 chandef->center_freq2 = 0;
996 +
997 +               /* NOTE: Disabling radar is only valid here for
998 +                * single channel context. To be sure, check it ...
999 +                */
1000 +               if (local->hw.conf.radar_enabled)
1001 +                       check_single_channel = true;
1002 +               local->hw.conf.radar_enabled = false;
1003 +
1004                 ieee80211_hw_config(local, 0);
1005         } else {
1006                 drv_remove_chanctx(local, ctx);
1007 @@ -127,6 +155,9 @@ static void ieee80211_free_chanctx(struc
1008         list_del_rcu(&ctx->list);
1009         kfree_rcu(ctx, rcu_head);
1010  
1011 +       /* throw a warning if this wasn't the only channel context. */
1012 +       WARN_ON(check_single_channel && !list_empty(&local->chanctx_list));
1013 +
1014         mutex_lock(&local->mtx);
1015         ieee80211_recalc_idle(local);
1016         mutex_unlock(&local->mtx);
1017 @@ -238,19 +269,11 @@ static void __ieee80211_vif_release_chan
1018  void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
1019                                     struct ieee80211_chanctx *chanctx)
1020  {
1021 -       struct ieee80211_sub_if_data *sdata;
1022 -       bool radar_enabled = false;
1023 +       bool radar_enabled;
1024  
1025         lockdep_assert_held(&local->chanctx_mtx);
1026  
1027 -       rcu_read_lock();
1028 -       list_for_each_entry_rcu(sdata, &local->interfaces, list) {
1029 -               if (sdata->radar_required) {
1030 -                       radar_enabled = true;
1031 -                       break;
1032 -               }
1033 -       }
1034 -       rcu_read_unlock();
1035 +       radar_enabled = ieee80211_is_radar_required(local);
1036  
1037         if (radar_enabled == chanctx->conf.radar_enabled)
1038                 return;
1039 --- a/net/mac80211/debugfs_sta.c
1040 +++ b/net/mac80211/debugfs_sta.c
1041 @@ -66,11 +66,11 @@ static ssize_t sta_flags_read(struct fil
1042         test_sta_flag(sta, WLAN_STA_##flg) ? #flg "\n" : ""
1043  
1044         int res = scnprintf(buf, sizeof(buf),
1045 -                           "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1046 +                           "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1047                             TEST(AUTH), TEST(ASSOC), TEST(PS_STA),
1048                             TEST(PS_DRIVER), TEST(AUTHORIZED),
1049                             TEST(SHORT_PREAMBLE),
1050 -                           TEST(WME), TEST(WDS), TEST(CLEAR_PS_FILT),
1051 +                           TEST(WME), TEST(CLEAR_PS_FILT),
1052                             TEST(MFP), TEST(BLOCK_BA), TEST(PSPOLL),
1053                             TEST(UAPSD), TEST(SP), TEST(TDLS_PEER),
1054                             TEST(TDLS_PEER_AUTH), TEST(4ADDR_EVENT),
1055 --- a/net/mac80211/ibss.c
1056 +++ b/net/mac80211/ibss.c
1057 @@ -209,6 +209,8 @@ static void __ieee80211_sta_join_ibss(st
1058         sdata->vif.bss_conf.enable_beacon = true;
1059         sdata->vif.bss_conf.beacon_int = beacon_int;
1060         sdata->vif.bss_conf.basic_rates = basic_rates;
1061 +       sdata->vif.bss_conf.ssid_len = ifibss->ssid_len;
1062 +       memcpy(sdata->vif.bss_conf.ssid, ifibss->ssid, ifibss->ssid_len);
1063         bss_change = BSS_CHANGED_BEACON_INT;
1064         bss_change |= ieee80211_reset_erp_info(sdata);
1065         bss_change |= BSS_CHANGED_BSSID;
1066 @@ -217,6 +219,7 @@ static void __ieee80211_sta_join_ibss(st
1067         bss_change |= BSS_CHANGED_BASIC_RATES;
1068         bss_change |= BSS_CHANGED_HT;
1069         bss_change |= BSS_CHANGED_IBSS;
1070 +       bss_change |= BSS_CHANGED_SSID;
1071  
1072         /*
1073          * In 5 GHz/802.11a, we can always use short slot time.
1074 @@ -911,7 +914,7 @@ void ieee80211_rx_mgmt_probe_beacon(stru
1075                 return;
1076  
1077         ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1078 -                               &elems);
1079 +                              false, &elems);
1080  
1081         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
1082  }
1083 @@ -1159,6 +1162,7 @@ int ieee80211_ibss_leave(struct ieee8021
1084         sdata->vif.bss_conf.ibss_joined = false;
1085         sdata->vif.bss_conf.ibss_creator = false;
1086         sdata->vif.bss_conf.enable_beacon = false;
1087 +       sdata->vif.bss_conf.ssid_len = 0;
1088         clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
1089         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
1090                                                 BSS_CHANGED_IBSS);
1091 --- a/net/mac80211/ieee80211_i.h
1092 +++ b/net/mac80211/ieee80211_i.h
1093 @@ -156,6 +156,7 @@ struct ieee80211_tx_data {
1094         struct ieee80211_sub_if_data *sdata;
1095         struct sta_info *sta;
1096         struct ieee80211_key *key;
1097 +       struct ieee80211_tx_rate rate;
1098  
1099         unsigned int flags;
1100  };
1101 @@ -740,6 +741,8 @@ struct ieee80211_sub_if_data {
1102  
1103         /* bitmap of allowed (non-MCS) rate indexes for rate control */
1104         u32 rc_rateidx_mask[IEEE80211_NUM_BANDS];
1105 +
1106 +       bool rc_has_mcs_mask[IEEE80211_NUM_BANDS];
1107         u8  rc_rateidx_mcs_mask[IEEE80211_NUM_BANDS][IEEE80211_HT_MCS_MASK_LEN];
1108  
1109         union {
1110 @@ -1025,7 +1028,7 @@ struct ieee80211_local {
1111         enum mac80211_scan_state next_scan_state;
1112         struct delayed_work scan_work;
1113         struct ieee80211_sub_if_data __rcu *scan_sdata;
1114 -       struct ieee80211_channel *csa_channel;
1115 +       struct cfg80211_chan_def csa_chandef;
1116         /* For backward compatibility only -- do not use */
1117         struct cfg80211_chan_def _oper_chandef;
1118  
1119 @@ -1184,10 +1187,13 @@ struct ieee802_11_elems {
1120         const u8 *perr;
1121         const struct ieee80211_rann_ie *rann;
1122         const struct ieee80211_channel_sw_ie *ch_switch_ie;
1123 +       const struct ieee80211_ext_chansw_ie *ext_chansw_ie;
1124 +       const struct ieee80211_wide_bw_chansw_ie *wide_bw_chansw_ie;
1125         const u8 *country_elem;
1126         const u8 *pwr_constr_elem;
1127         const struct ieee80211_timeout_interval_ie *timeout_int;
1128         const u8 *opmode_notif;
1129 +       const struct ieee80211_sec_chan_offs_ie *sec_chan_offs;
1130  
1131         /* length of them, respectively */
1132         u8 ssid_len;
1133 @@ -1258,10 +1264,6 @@ void ieee80211_recalc_ps_vif(struct ieee
1134  int ieee80211_max_network_latency(struct notifier_block *nb,
1135                                   unsigned long data, void *dummy);
1136  int ieee80211_set_arp_filter(struct ieee80211_sub_if_data *sdata);
1137 -void
1138 -ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1139 -                                const struct ieee80211_channel_sw_ie *sw_elem,
1140 -                                struct ieee80211_bss *bss, u64 timestamp);
1141  void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata);
1142  void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1143                                   struct sk_buff *skb);
1144 @@ -1499,13 +1501,13 @@ static inline void ieee80211_tx_skb(stru
1145         ieee80211_tx_skb_tid(sdata, skb, 7);
1146  }
1147  
1148 -u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
1149 +u32 ieee802_11_parse_elems_crc(u8 *start, size_t len, bool action,
1150                                struct ieee802_11_elems *elems,
1151                                u64 filter, u32 crc);
1152 -static inline void ieee802_11_parse_elems(u8 *start, size_t len,
1153 +static inline void ieee802_11_parse_elems(u8 *start, size_t len, bool action,
1154                                           struct ieee802_11_elems *elems)
1155  {
1156 -       ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
1157 +       ieee802_11_parse_elems_crc(start, len, action, elems, 0, 0);
1158  }
1159  
1160  u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
1161 --- a/net/mac80211/iface.c
1162 +++ b/net/mac80211/iface.c
1163 @@ -450,7 +450,6 @@ int ieee80211_do_open(struct wireless_de
1164         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
1165         struct net_device *dev = wdev->netdev;
1166         struct ieee80211_local *local = sdata->local;
1167 -       struct sta_info *sta;
1168         u32 changed = 0;
1169         int res;
1170         u32 hw_reconf_flags = 0;
1171 @@ -609,30 +608,8 @@ int ieee80211_do_open(struct wireless_de
1172  
1173         set_bit(SDATA_STATE_RUNNING, &sdata->state);
1174  
1175 -       if (sdata->vif.type == NL80211_IFTYPE_WDS) {
1176 -               /* Create STA entry for the WDS peer */
1177 -               sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
1178 -                                    GFP_KERNEL);
1179 -               if (!sta) {
1180 -                       res = -ENOMEM;
1181 -                       goto err_del_interface;
1182 -               }
1183 -
1184 -               sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
1185 -               sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
1186 -               sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
1187 -
1188 -               res = sta_info_insert(sta);
1189 -               if (res) {
1190 -                       /* STA has been freed */
1191 -                       goto err_del_interface;
1192 -               }
1193 -
1194 -               rate_control_rate_init(sta);
1195 -               netif_carrier_on(dev);
1196 -       } else if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
1197 +       if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE)
1198                 rcu_assign_pointer(local->p2p_sdata, sdata);
1199 -       }
1200  
1201         /*
1202          * set_multicast_list will be invoked by the networking core
1203 @@ -1092,6 +1069,74 @@ static void ieee80211_if_setup(struct ne
1204         dev->destructor = free_netdev;
1205  }
1206  
1207 +static void ieee80211_wds_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1208 +                                        struct sk_buff *skb)
1209 +{
1210 +       struct ieee80211_local *local = sdata->local;
1211 +       struct ieee80211_rx_status *rx_status;
1212 +       struct ieee802_11_elems elems;
1213 +       struct ieee80211_mgmt *mgmt;
1214 +       struct sta_info *sta;
1215 +       size_t baselen;
1216 +       u32 rates = 0;
1217 +       u16 stype;
1218 +       bool new = false;
1219 +       enum ieee80211_band band;
1220 +       struct ieee80211_supported_band *sband;
1221 +
1222 +       rx_status = IEEE80211_SKB_RXCB(skb);
1223 +       band = rx_status->band;
1224 +       sband = local->hw.wiphy->bands[band];
1225 +       mgmt = (struct ieee80211_mgmt *) skb->data;
1226 +       stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
1227 +
1228 +       if (stype != IEEE80211_STYPE_BEACON)
1229 +               return;
1230 +
1231 +       baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
1232 +       if (baselen > skb->len)
1233 +               return;
1234 +
1235 +       ieee802_11_parse_elems(mgmt->u.probe_resp.variable,
1236 +                              skb->len - baselen, false, &elems);
1237 +
1238 +       rates = ieee80211_sta_get_rates(local, &elems, band, NULL);
1239 +
1240 +       rcu_read_lock();
1241 +
1242 +       sta = sta_info_get(sdata, sdata->u.wds.remote_addr);
1243 +
1244 +       if (!sta) {
1245 +               rcu_read_unlock();
1246 +               sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
1247 +                                    GFP_KERNEL);
1248 +               if (!sta)
1249 +                       return;
1250 +
1251 +               new = true;
1252 +       }
1253 +
1254 +       sta->last_rx = jiffies;
1255 +       sta->sta.supp_rates[band] = rates;
1256 +
1257 +       if (elems.ht_cap_elem)
1258 +               ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
1259 +                               elems.ht_cap_elem, sta);
1260 +
1261 +       if (elems.wmm_param)
1262 +               set_sta_flag(sta, WLAN_STA_WME);
1263 +
1264 +       if (new) {
1265 +               sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
1266 +               sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
1267 +               sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
1268 +               rate_control_rate_init(sta);
1269 +               sta_info_insert_rcu(sta);
1270 +       }
1271 +
1272 +       rcu_read_unlock();
1273 +}
1274 +
1275  static void ieee80211_iface_work(struct work_struct *work)
1276  {
1277         struct ieee80211_sub_if_data *sdata =
1278 @@ -1196,6 +1241,9 @@ static void ieee80211_iface_work(struct 
1279                                 break;
1280                         ieee80211_mesh_rx_queued_mgmt(sdata, skb);
1281                         break;
1282 +               case NL80211_IFTYPE_WDS:
1283 +                       ieee80211_wds_rx_queued_mgmt(sdata, skb);
1284 +                       break;
1285                 default:
1286                         WARN(1, "frame for unexpected interface type");
1287                         break;
1288 --- a/net/mac80211/main.c
1289 +++ b/net/mac80211/main.c
1290 @@ -674,6 +674,7 @@ int ieee80211_register_hw(struct ieee802
1291         int channels, max_bitrates;
1292         bool supp_ht, supp_vht;
1293         netdev_features_t feature_whitelist;
1294 +       struct cfg80211_chan_def dflt_chandef = {};
1295         static const u32 cipher_suites[] = {
1296                 /* keep WEP first, it may be removed below */
1297                 WLAN_CIPHER_SUITE_WEP40,
1298 @@ -751,19 +752,19 @@ int ieee80211_register_hw(struct ieee802
1299                 sband = local->hw.wiphy->bands[band];
1300                 if (!sband)
1301                         continue;
1302 -               if (!local->use_chanctx && !local->_oper_chandef.chan) {
1303 +
1304 +               if (!dflt_chandef.chan) {
1305 +                       cfg80211_chandef_create(&dflt_chandef,
1306 +                                               &sband->channels[0],
1307 +                                               NL80211_CHAN_NO_HT);
1308                         /* init channel we're on */
1309 -                       struct cfg80211_chan_def chandef = {
1310 -                               .chan = &sband->channels[0],
1311 -                               .width = NL80211_CHAN_NO_HT,
1312 -                               .center_freq1 = sband->channels[0].center_freq,
1313 -                               .center_freq2 = 0
1314 -                       };
1315 -                       local->hw.conf.chandef = local->_oper_chandef = chandef;
1316 +                       if (!local->use_chanctx && !local->_oper_chandef.chan) {
1317 +                               local->hw.conf.chandef = dflt_chandef;
1318 +                               local->_oper_chandef = dflt_chandef;
1319 +                       }
1320 +                       local->monitor_chandef = dflt_chandef;
1321                 }
1322 -               cfg80211_chandef_create(&local->monitor_chandef,
1323 -                                       &sband->channels[0],
1324 -                                       NL80211_CHAN_NO_HT);
1325 +
1326                 channels += sband->n_channels;
1327  
1328                 if (max_bitrates < sband->n_bitrates)
1329 --- a/net/mac80211/mesh.c
1330 +++ b/net/mac80211/mesh.c
1331 @@ -838,7 +838,7 @@ ieee80211_mesh_rx_probe_req(struct ieee8
1332         if (baselen > len)
1333                 return;
1334  
1335 -       ieee802_11_parse_elems(pos, len - baselen, &elems);
1336 +       ieee802_11_parse_elems(pos, len - baselen, false, &elems);
1337  
1338         /* 802.11-2012 10.1.4.3.2 */
1339         if ((!ether_addr_equal(mgmt->da, sdata->vif.addr) &&
1340 @@ -899,7 +899,7 @@ static void ieee80211_mesh_rx_bcn_presp(
1341                 return;
1342  
1343         ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1344 -                              &elems);
1345 +                              false, &elems);
1346  
1347         /* ignore non-mesh or secure / unsecure mismatch */
1348         if ((!elems.mesh_id || !elems.mesh_config) ||
1349 --- a/net/mac80211/mesh_hwmp.c
1350 +++ b/net/mac80211/mesh_hwmp.c
1351 @@ -880,7 +880,7 @@ void mesh_rx_path_sel_frame(struct ieee8
1352  
1353         baselen = (u8 *) mgmt->u.action.u.mesh_action.variable - (u8 *) mgmt;
1354         ieee802_11_parse_elems(mgmt->u.action.u.mesh_action.variable,
1355 -                       len - baselen, &elems);
1356 +                              len - baselen, false, &elems);
1357  
1358         if (elems.preq) {
1359                 if (elems.preq_len != 37)
1360 --- a/net/mac80211/mesh_plink.c
1361 +++ b/net/mac80211/mesh_plink.c
1362 @@ -544,8 +544,8 @@ static void mesh_plink_timer(unsigned lo
1363                 return;
1364         }
1365         mpl_dbg(sta->sdata,
1366 -               "Mesh plink timer for %pM fired on state %d\n",
1367 -               sta->sta.addr, sta->plink_state);
1368 +               "Mesh plink timer for %pM fired on state %s\n",
1369 +               sta->sta.addr, mplstates[sta->plink_state]);
1370         reason = 0;
1371         llid = sta->llid;
1372         plid = sta->plid;
1373 @@ -687,7 +687,7 @@ void mesh_rx_plink_frame(struct ieee8021
1374                 baseaddr += 4;
1375                 baselen += 4;
1376         }
1377 -       ieee802_11_parse_elems(baseaddr, len - baselen, &elems);
1378 +       ieee802_11_parse_elems(baseaddr, len - baselen, true, &elems);
1379  
1380         if (!elems.peering) {
1381                 mpl_dbg(sdata,
1382 --- a/net/mac80211/mlme.c
1383 +++ b/net/mac80211/mlme.c
1384 @@ -289,6 +289,8 @@ ieee80211_determine_chantype(struct ieee
1385         } else {
1386                 /* 40 MHz (and 80 MHz) must be supported for VHT */
1387                 ret = IEEE80211_STA_DISABLE_VHT;
1388 +               /* also mark 40 MHz disabled */
1389 +               ret |= IEEE80211_STA_DISABLE_40MHZ;
1390                 goto out;
1391         }
1392  
1393 @@ -303,12 +305,6 @@ ieee80211_determine_chantype(struct ieee
1394                                                channel->band);
1395         vht_chandef.center_freq2 = 0;
1396  
1397 -       if (vht_oper->center_freq_seg2_idx)
1398 -               vht_chandef.center_freq2 =
1399 -                       ieee80211_channel_to_frequency(
1400 -                               vht_oper->center_freq_seg2_idx,
1401 -                               channel->band);
1402 -
1403         switch (vht_oper->chan_width) {
1404         case IEEE80211_VHT_CHANWIDTH_USE_HT:
1405                 vht_chandef.width = chandef->width;
1406 @@ -321,6 +317,10 @@ ieee80211_determine_chantype(struct ieee
1407                 break;
1408         case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
1409                 vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
1410 +               vht_chandef.center_freq2 =
1411 +                       ieee80211_channel_to_frequency(
1412 +                               vht_oper->center_freq_seg2_idx,
1413 +                               channel->band);
1414                 break;
1415         default:
1416                 if (verbose)
1417 @@ -604,7 +604,6 @@ static void ieee80211_add_vht_ie(struct 
1418         u8 *pos;
1419         u32 cap;
1420         struct ieee80211_sta_vht_cap vht_cap;
1421 -       int i;
1422  
1423         BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
1424  
1425 @@ -632,37 +631,6 @@ static void ieee80211_add_vht_ie(struct 
1426                         cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
1427                 cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
1428  
1429 -       if (!(ap_vht_cap->vht_cap_info &
1430 -                       cpu_to_le32(IEEE80211_VHT_CAP_TXSTBC)))
1431 -               cap &= ~(IEEE80211_VHT_CAP_RXSTBC_1 |
1432 -                        IEEE80211_VHT_CAP_RXSTBC_3 |
1433 -                        IEEE80211_VHT_CAP_RXSTBC_4);
1434 -
1435 -       for (i = 0; i < 8; i++) {
1436 -               int shift = i * 2;
1437 -               u16 mask = IEEE80211_VHT_MCS_NOT_SUPPORTED << shift;
1438 -               u16 ap_mcs, our_mcs;
1439 -
1440 -               ap_mcs = (le16_to_cpu(ap_vht_cap->supp_mcs.tx_mcs_map) &
1441 -                                                               mask) >> shift;
1442 -               our_mcs = (le16_to_cpu(vht_cap.vht_mcs.rx_mcs_map) &
1443 -                                                               mask) >> shift;
1444 -
1445 -               if (our_mcs == IEEE80211_VHT_MCS_NOT_SUPPORTED)
1446 -                       continue;
1447 -
1448 -               switch (ap_mcs) {
1449 -               default:
1450 -                       if (our_mcs <= ap_mcs)
1451 -                               break;
1452 -                       /* fall through */
1453 -               case IEEE80211_VHT_MCS_NOT_SUPPORTED:
1454 -                       vht_cap.vht_mcs.rx_mcs_map &= cpu_to_le16(~mask);
1455 -                       vht_cap.vht_mcs.rx_mcs_map |=
1456 -                               cpu_to_le16(ap_mcs << shift);
1457 -               }
1458 -       }
1459 -
1460         /* reserve and fill IE */
1461         pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
1462         ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
1463 @@ -998,16 +966,7 @@ static void ieee80211_chswitch_work(stru
1464         if (!ifmgd->associated)
1465                 goto out;
1466  
1467 -       /*
1468 -        * FIXME: Here we are downgrading to NL80211_CHAN_WIDTH_20_NOHT
1469 -        * and don't adjust our ht/vht settings
1470 -        * This is wrong - we should behave according to the CSA params
1471 -        */
1472 -       local->_oper_chandef.chan = local->csa_channel;
1473 -       local->_oper_chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
1474 -       local->_oper_chandef.center_freq1 =
1475 -               local->_oper_chandef.chan->center_freq;
1476 -       local->_oper_chandef.center_freq2 = 0;
1477 +       local->_oper_chandef = local->csa_chandef;
1478  
1479         if (!local->ops->channel_switch) {
1480                 /* call "hw_config" only if doing sw channel switch */
1481 @@ -1054,56 +1013,208 @@ static void ieee80211_chswitch_timer(uns
1482         ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
1483  }
1484  
1485 -void
1486 +static void
1487  ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1488 -                                const struct ieee80211_channel_sw_ie *sw_elem,
1489 -                                struct ieee80211_bss *bss, u64 timestamp)
1490 +                                u64 timestamp, struct ieee802_11_elems *elems,
1491 +                                bool beacon)
1492  {
1493 -       struct cfg80211_bss *cbss =
1494 -               container_of((void *)bss, struct cfg80211_bss, priv);
1495 -       struct ieee80211_channel *new_ch;
1496 +       struct ieee80211_local *local = sdata->local;
1497         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1498 -       int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
1499 -                                                     cbss->channel->band);
1500 +       struct cfg80211_bss *cbss = ifmgd->associated;
1501 +       struct ieee80211_bss *bss;
1502         struct ieee80211_chanctx *chanctx;
1503 +       enum ieee80211_band new_band;
1504 +       int new_freq;
1505 +       u8 new_chan_no;
1506 +       u8 count;
1507 +       u8 mode;
1508 +       struct ieee80211_channel *new_chan;
1509 +       struct cfg80211_chan_def new_chandef = {};
1510 +       struct cfg80211_chan_def new_vht_chandef = {};
1511 +       const struct ieee80211_sec_chan_offs_ie *sec_chan_offs;
1512 +       const struct ieee80211_wide_bw_chansw_ie *wide_bw_chansw_ie;
1513 +       const struct ieee80211_ht_operation *ht_oper;
1514 +       int secondary_channel_offset = -1;
1515  
1516         ASSERT_MGD_MTX(ifmgd);
1517  
1518 -       if (!ifmgd->associated)
1519 +       if (!cbss)
1520 +               return;
1521 +
1522 +       if (local->scanning)
1523                 return;
1524  
1525 -       if (sdata->local->scanning)
1526 +       /* disregard subsequent announcements if we are already processing */
1527 +       if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
1528                 return;
1529  
1530 -       /* Disregard subsequent beacons if we are already running a timer
1531 -          processing a CSA */
1532 +       sec_chan_offs = elems->sec_chan_offs;
1533 +       wide_bw_chansw_ie = elems->wide_bw_chansw_ie;
1534 +       ht_oper = elems->ht_operation;
1535 +
1536 +       if (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
1537 +                           IEEE80211_STA_DISABLE_40MHZ)) {
1538 +               sec_chan_offs = NULL;
1539 +               wide_bw_chansw_ie = NULL;
1540 +               /* only used for bandwidth here */
1541 +               ht_oper = NULL;
1542 +       }
1543  
1544 -       if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
1545 +       if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
1546 +               wide_bw_chansw_ie = NULL;
1547 +
1548 +       if (elems->ext_chansw_ie) {
1549 +               if (!ieee80211_operating_class_to_band(
1550 +                               elems->ext_chansw_ie->new_operating_class,
1551 +                               &new_band)) {
1552 +                       sdata_info(sdata,
1553 +                                  "cannot understand ECSA IE operating class %d, disconnecting\n",
1554 +                                  elems->ext_chansw_ie->new_operating_class);
1555 +                       ieee80211_queue_work(&local->hw,
1556 +                                            &ifmgd->csa_connection_drop_work);
1557 +               }
1558 +               new_chan_no = elems->ext_chansw_ie->new_ch_num;
1559 +               count = elems->ext_chansw_ie->count;
1560 +               mode = elems->ext_chansw_ie->mode;
1561 +       } else if (elems->ch_switch_ie) {
1562 +               new_band = cbss->channel->band;
1563 +               new_chan_no = elems->ch_switch_ie->new_ch_num;
1564 +               count = elems->ch_switch_ie->count;
1565 +               mode = elems->ch_switch_ie->mode;
1566 +       } else {
1567 +               /* nothing here we understand */
1568                 return;
1569 +       }
1570 +
1571 +       bss = (void *)cbss->priv;
1572  
1573 -       new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
1574 -       if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
1575 +       new_freq = ieee80211_channel_to_frequency(new_chan_no, new_band);
1576 +       new_chan = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
1577 +       if (!new_chan || new_chan->flags & IEEE80211_CHAN_DISABLED) {
1578                 sdata_info(sdata,
1579                            "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
1580                            ifmgd->associated->bssid, new_freq);
1581 -               ieee80211_queue_work(&sdata->local->hw,
1582 +               ieee80211_queue_work(&local->hw,
1583 +                                    &ifmgd->csa_connection_drop_work);
1584 +               return;
1585 +       }
1586 +
1587 +       if (!beacon && sec_chan_offs) {
1588 +               secondary_channel_offset = sec_chan_offs->sec_chan_offs;
1589 +       } else if (beacon && ht_oper) {
1590 +               secondary_channel_offset =
1591 +                       ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET;
1592 +       } else if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
1593 +               /*
1594 +                * If it's not a beacon, HT is enabled and the IE not present,
1595 +                * it's 20 MHz, 802.11-2012 8.5.2.6:
1596 +                *      This element [the Secondary Channel Offset Element] is
1597 +                *      present when switching to a 40 MHz channel. It may be
1598 +                *      present when switching to a 20 MHz channel (in which
1599 +                *      case the secondary channel offset is set to SCN).
1600 +                */
1601 +               secondary_channel_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
1602 +       }
1603 +
1604 +       switch (secondary_channel_offset) {
1605 +       default:
1606 +               /* secondary_channel_offset was present but is invalid */
1607 +       case IEEE80211_HT_PARAM_CHA_SEC_NONE:
1608 +               cfg80211_chandef_create(&new_chandef, new_chan,
1609 +                                       NL80211_CHAN_HT20);
1610 +               break;
1611 +       case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
1612 +               cfg80211_chandef_create(&new_chandef, new_chan,
1613 +                                       NL80211_CHAN_HT40PLUS);
1614 +               break;
1615 +       case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
1616 +               cfg80211_chandef_create(&new_chandef, new_chan,
1617 +                                       NL80211_CHAN_HT40MINUS);
1618 +               break;
1619 +       case -1:
1620 +               cfg80211_chandef_create(&new_chandef, new_chan,
1621 +                                       NL80211_CHAN_NO_HT);
1622 +               break;
1623 +       }
1624 +
1625 +       if (wide_bw_chansw_ie) {
1626 +               new_vht_chandef.chan = new_chan;
1627 +               new_vht_chandef.center_freq1 =
1628 +                       ieee80211_channel_to_frequency(
1629 +                               wide_bw_chansw_ie->new_center_freq_seg0,
1630 +                               new_band);
1631 +
1632 +               switch (wide_bw_chansw_ie->new_channel_width) {
1633 +               default:
1634 +                       /* hmmm, ignore VHT and use HT if present */
1635 +               case IEEE80211_VHT_CHANWIDTH_USE_HT:
1636 +                       new_vht_chandef.chan = NULL;
1637 +                       break;
1638 +               case IEEE80211_VHT_CHANWIDTH_80MHZ:
1639 +                       new_vht_chandef.width = NL80211_CHAN_WIDTH_80;
1640 +                       break;
1641 +               case IEEE80211_VHT_CHANWIDTH_160MHZ:
1642 +                       new_vht_chandef.width = NL80211_CHAN_WIDTH_160;
1643 +                       break;
1644 +               case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
1645 +                       /* field is otherwise reserved */
1646 +                       new_vht_chandef.center_freq2 =
1647 +                               ieee80211_channel_to_frequency(
1648 +                                       wide_bw_chansw_ie->new_center_freq_seg1,
1649 +                                       new_band);
1650 +                       new_vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
1651 +                       break;
1652 +               }
1653 +               if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
1654 +                   new_vht_chandef.width == NL80211_CHAN_WIDTH_80P80)
1655 +                       chandef_downgrade(&new_vht_chandef);
1656 +               if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
1657 +                   new_vht_chandef.width == NL80211_CHAN_WIDTH_160)
1658 +                       chandef_downgrade(&new_vht_chandef);
1659 +               if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
1660 +                   new_vht_chandef.width > NL80211_CHAN_WIDTH_20)
1661 +                       chandef_downgrade(&new_vht_chandef);
1662 +       }
1663 +
1664 +       /* if VHT data is there validate & use it */
1665 +       if (new_vht_chandef.chan) {
1666 +               if (!cfg80211_chandef_compatible(&new_vht_chandef,
1667 +                                                &new_chandef)) {
1668 +                       sdata_info(sdata,
1669 +                                  "AP %pM CSA has inconsistent channel data, disconnecting\n",
1670 +                                  ifmgd->associated->bssid);
1671 +                       ieee80211_queue_work(&local->hw,
1672 +                                            &ifmgd->csa_connection_drop_work);
1673 +                       return;
1674 +               }
1675 +               new_chandef = new_vht_chandef;
1676 +       }
1677 +
1678 +       if (!cfg80211_chandef_usable(local->hw.wiphy, &new_chandef,
1679 +                                    IEEE80211_CHAN_DISABLED)) {
1680 +               sdata_info(sdata,
1681 +                          "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
1682 +                          ifmgd->associated->bssid, new_freq,
1683 +                          new_chandef.width, new_chandef.center_freq1,
1684 +                          new_chandef.center_freq2);
1685 +               ieee80211_queue_work(&local->hw,
1686                                      &ifmgd->csa_connection_drop_work);
1687                 return;
1688         }
1689  
1690         ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
1691  
1692 -       if (sdata->local->use_chanctx) {
1693 +       if (local->use_chanctx) {
1694                 sdata_info(sdata,
1695                            "not handling channel switch with channel contexts\n");
1696 -               ieee80211_queue_work(&sdata->local->hw,
1697 +               ieee80211_queue_work(&local->hw,
1698                                      &ifmgd->csa_connection_drop_work);
1699                 return;
1700         }
1701  
1702 -       mutex_lock(&sdata->local->chanctx_mtx);
1703 +       mutex_lock(&local->chanctx_mtx);
1704         if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
1705 -               mutex_unlock(&sdata->local->chanctx_mtx);
1706 +               mutex_unlock(&local->chanctx_mtx);
1707                 return;
1708         }
1709         chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
1710 @@ -1111,40 +1222,39 @@ ieee80211_sta_process_chanswitch(struct 
1711         if (chanctx->refcount > 1) {
1712                 sdata_info(sdata,
1713                            "channel switch with multiple interfaces on the same channel, disconnecting\n");
1714 -               ieee80211_queue_work(&sdata->local->hw,
1715 +               ieee80211_queue_work(&local->hw,
1716                                      &ifmgd->csa_connection_drop_work);
1717 -               mutex_unlock(&sdata->local->chanctx_mtx);
1718 +               mutex_unlock(&local->chanctx_mtx);
1719                 return;
1720         }
1721 -       mutex_unlock(&sdata->local->chanctx_mtx);
1722 +       mutex_unlock(&local->chanctx_mtx);
1723  
1724 -       sdata->local->csa_channel = new_ch;
1725 +       local->csa_chandef = new_chandef;
1726  
1727 -       if (sw_elem->mode)
1728 -               ieee80211_stop_queues_by_reason(&sdata->local->hw,
1729 +       if (mode)
1730 +               ieee80211_stop_queues_by_reason(&local->hw,
1731                                 IEEE80211_MAX_QUEUE_MAP,
1732                                 IEEE80211_QUEUE_STOP_REASON_CSA);
1733  
1734 -       if (sdata->local->ops->channel_switch) {
1735 +       if (local->ops->channel_switch) {
1736                 /* use driver's channel switch callback */
1737                 struct ieee80211_channel_switch ch_switch = {
1738                         .timestamp = timestamp,
1739 -                       .block_tx = sw_elem->mode,
1740 -                       .channel = new_ch,
1741 -                       .count = sw_elem->count,
1742 +                       .block_tx = mode,
1743 +                       .chandef = new_chandef,
1744 +                       .count = count,
1745                 };
1746  
1747 -               drv_channel_switch(sdata->local, &ch_switch);
1748 +               drv_channel_switch(local, &ch_switch);
1749                 return;
1750         }
1751  
1752         /* channel switch handled in software */
1753 -       if (sw_elem->count <= 1)
1754 -               ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
1755 +       if (count <= 1)
1756 +               ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
1757         else
1758                 mod_timer(&ifmgd->chswitch_timer,
1759 -                         TU_TO_EXP_TIME(sw_elem->count *
1760 -                                        cbss->beacon_interval));
1761 +                         TU_TO_EXP_TIME(count * cbss->beacon_interval));
1762  }
1763  
1764  static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1765 @@ -2120,7 +2230,6 @@ void ieee80211_beacon_loss(struct ieee80
1766  
1767         trace_api_beacon_loss(sdata);
1768  
1769 -       WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
1770         sdata->u.mgd.connection_loss = false;
1771         ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
1772  }
1773 @@ -2170,7 +2279,7 @@ static void ieee80211_auth_challenge(str
1774         u32 tx_flags = 0;
1775  
1776         pos = mgmt->u.auth.variable;
1777 -       ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1778 +       ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
1779         if (!elems.challenge)
1780                 return;
1781         auth_data->expected_transaction = 4;
1782 @@ -2435,7 +2544,7 @@ static bool ieee80211_assoc_success(stru
1783         }
1784  
1785         pos = mgmt->u.assoc_resp.variable;
1786 -       ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1787 +       ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
1788  
1789         if (!elems.supp_rates) {
1790                 sdata_info(sdata, "no SuppRates element in AssocResp\n");
1791 @@ -2604,7 +2713,7 @@ ieee80211_rx_mgmt_assoc_resp(struct ieee
1792                    capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
1793  
1794         pos = mgmt->u.assoc_resp.variable;
1795 -       ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
1796 +       ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
1797  
1798         if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
1799             elems.timeout_int &&
1800 @@ -2659,6 +2768,8 @@ static void ieee80211_rx_bss_info(struct
1801         struct ieee80211_channel *channel;
1802         bool need_ps = false;
1803  
1804 +       lockdep_assert_held(&sdata->u.mgd.mtx);
1805 +
1806         if ((sdata->u.mgd.associated &&
1807              ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) ||
1808             (sdata->u.mgd.assoc_data &&
1809 @@ -2689,7 +2800,8 @@ static void ieee80211_rx_bss_info(struct
1810         if (bss)
1811                 ieee80211_rx_bss_put(local, bss);
1812  
1813 -       if (!sdata->u.mgd.associated)
1814 +       if (!sdata->u.mgd.associated ||
1815 +           !ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid))
1816                 return;
1817  
1818         if (need_ps) {
1819 @@ -2698,10 +2810,9 @@ static void ieee80211_rx_bss_info(struct
1820                 mutex_unlock(&local->iflist_mtx);
1821         }
1822  
1823 -       if (elems->ch_switch_ie &&
1824 -           memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
1825 -               ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
1826 -                                                bss, rx_status->mactime);
1827 +       ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
1828 +                                        elems, true);
1829 +
1830  }
1831  
1832  
1833 @@ -2726,7 +2837,7 @@ static void ieee80211_rx_mgmt_probe_resp
1834                 return;
1835  
1836         ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
1837 -                               &elems);
1838 +                              false, &elems);
1839  
1840         ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
1841  
1842 @@ -2809,7 +2920,7 @@ ieee80211_rx_mgmt_beacon(struct ieee8021
1843         if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
1844             ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
1845                 ieee802_11_parse_elems(mgmt->u.beacon.variable,
1846 -                                      len - baselen, &elems);
1847 +                                      len - baselen, false, &elems);
1848  
1849                 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
1850                 ifmgd->assoc_data->have_beacon = true;
1851 @@ -2919,7 +3030,7 @@ ieee80211_rx_mgmt_beacon(struct ieee8021
1852  
1853         ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
1854         ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
1855 -                                         len - baselen, &elems,
1856 +                                         len - baselen, false, &elems,
1857                                           care_about_ies, ncrc);
1858  
1859         if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
1860 @@ -3066,6 +3177,8 @@ void ieee80211_sta_rx_queued_mgmt(struct
1861         enum rx_mgmt_action rma = RX_MGMT_NONE;
1862         u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
1863         u16 fc;
1864 +       struct ieee802_11_elems elems;
1865 +       int ies_len;
1866  
1867         rx_status = (struct ieee80211_rx_status *) skb->cb;
1868         mgmt = (struct ieee80211_mgmt *) skb->data;
1869 @@ -3095,14 +3208,48 @@ void ieee80211_sta_rx_queued_mgmt(struct
1870                 rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
1871                 break;
1872         case IEEE80211_STYPE_ACTION:
1873 -               switch (mgmt->u.action.category) {
1874 -               case WLAN_CATEGORY_SPECTRUM_MGMT:
1875 +               if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
1876 +                       ies_len = skb->len -
1877 +                                 offsetof(struct ieee80211_mgmt,
1878 +                                          u.action.u.chan_switch.variable);
1879 +
1880 +                       if (ies_len < 0)
1881 +                               break;
1882 +
1883 +                       ieee802_11_parse_elems(
1884 +                               mgmt->u.action.u.chan_switch.variable,
1885 +                               ies_len, true, &elems);
1886 +
1887 +                       if (elems.parse_error)
1888 +                               break;
1889 +
1890                         ieee80211_sta_process_chanswitch(sdata,
1891 -                                       &mgmt->u.action.u.chan_switch.sw_elem,
1892 -                                       (void *)ifmgd->associated->priv,
1893 -                                       rx_status->mactime);
1894 -                       break;
1895 +                                                        rx_status->mactime,
1896 +                                                        &elems, false);
1897 +               } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
1898 +                       ies_len = skb->len -
1899 +                                 offsetof(struct ieee80211_mgmt,
1900 +                                          u.action.u.ext_chan_switch.variable);
1901 +
1902 +                       if (ies_len < 0)
1903 +                               break;
1904 +
1905 +                       ieee802_11_parse_elems(
1906 +                               mgmt->u.action.u.ext_chan_switch.variable,
1907 +                               ies_len, true, &elems);
1908 +
1909 +                       if (elems.parse_error)
1910 +                               break;
1911 +
1912 +                       /* for the handling code pretend this was also an IE */
1913 +                       elems.ext_chansw_ie =
1914 +                               &mgmt->u.action.u.ext_chan_switch.data;
1915 +
1916 +                       ieee80211_sta_process_chanswitch(sdata,
1917 +                                                        rx_status->mactime,
1918 +                                                        &elems, false);
1919                 }
1920 +               break;
1921         }
1922         mutex_unlock(&ifmgd->mtx);
1923  
1924 @@ -4197,7 +4344,7 @@ int ieee80211_mgd_deauth(struct ieee8021
1925         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1926         u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
1927         bool tx = !req->local_state_change;
1928 -       bool sent_frame = false;
1929 +       bool report_frame = false;
1930  
1931         mutex_lock(&ifmgd->mtx);
1932  
1933 @@ -4214,7 +4361,7 @@ int ieee80211_mgd_deauth(struct ieee8021
1934                 ieee80211_destroy_auth_data(sdata, false);
1935                 mutex_unlock(&ifmgd->mtx);
1936  
1937 -               sent_frame = tx;
1938 +               report_frame = true;
1939                 goto out;
1940         }
1941  
1942 @@ -4222,12 +4369,12 @@ int ieee80211_mgd_deauth(struct ieee8021
1943             ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
1944                 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
1945                                        req->reason_code, tx, frame_buf);
1946 -               sent_frame = tx;
1947 +               report_frame = true;
1948         }
1949         mutex_unlock(&ifmgd->mtx);
1950  
1951   out:
1952 -       if (sent_frame)
1953 +       if (report_frame)
1954                 __cfg80211_send_deauth(sdata->dev, frame_buf,
1955                                        IEEE80211_DEAUTH_FRAME_LEN);
1956  
1957 --- a/net/mac80211/pm.c
1958 +++ b/net/mac80211/pm.c
1959 @@ -38,8 +38,8 @@ int __ieee80211_suspend(struct ieee80211
1960                                         IEEE80211_MAX_QUEUE_MAP,
1961                                         IEEE80211_QUEUE_STOP_REASON_SUSPEND);
1962  
1963 -       /* flush out all packets */
1964 -       synchronize_net();
1965 +       /* flush out all packets and station cleanup call_rcu()s */
1966 +       rcu_barrier();
1967  
1968         ieee80211_flush_queues(local, NULL);
1969  
1970 --- a/net/mac80211/rate.c
1971 +++ b/net/mac80211/rate.c
1972 @@ -252,6 +252,25 @@ rate_lowest_non_cck_index(struct ieee802
1973         return 0;
1974  }
1975  
1976 +static void __rate_control_send_low(struct ieee80211_hw *hw,
1977 +                                   struct ieee80211_supported_band *sband,
1978 +                                   struct ieee80211_sta *sta,
1979 +                                   struct ieee80211_tx_info *info)
1980 +{
1981 +       if ((sband->band != IEEE80211_BAND_2GHZ) ||
1982 +           !(info->flags & IEEE80211_TX_CTL_NO_CCK_RATE))
1983 +               info->control.rates[0].idx = rate_lowest_index(sband, sta);
1984 +       else
1985 +               info->control.rates[0].idx =
1986 +                       rate_lowest_non_cck_index(sband, sta);
1987 +
1988 +       info->control.rates[0].count =
1989 +               (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
1990 +               1 : hw->max_rate_tries;
1991 +
1992 +       info->control.skip_table = 1;
1993 +}
1994 +
1995  
1996  bool rate_control_send_low(struct ieee80211_sta *sta,
1997                            void *priv_sta,
1998 @@ -262,16 +281,8 @@ bool rate_control_send_low(struct ieee80
1999         int mcast_rate;
2000  
2001         if (!sta || !priv_sta || rc_no_data_or_no_ack_use_min(txrc)) {
2002 -               if ((sband->band != IEEE80211_BAND_2GHZ) ||
2003 -                   !(info->flags & IEEE80211_TX_CTL_NO_CCK_RATE))
2004 -                       info->control.rates[0].idx =
2005 -                               rate_lowest_index(txrc->sband, sta);
2006 -               else
2007 -                       info->control.rates[0].idx =
2008 -                               rate_lowest_non_cck_index(txrc->sband, sta);
2009 -               info->control.rates[0].count =
2010 -                       (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
2011 -                       1 : txrc->hw->max_rate_tries;
2012 +               __rate_control_send_low(txrc->hw, sband, sta, info);
2013 +
2014                 if (!sta && txrc->bss) {
2015                         mcast_rate = txrc->bss_conf->mcast_rate[sband->band];
2016                         if (mcast_rate > 0) {
2017 @@ -355,7 +366,8 @@ static bool rate_idx_match_mcs_mask(stru
2018  
2019  
2020  static void rate_idx_match_mask(struct ieee80211_tx_rate *rate,
2021 -                               struct ieee80211_tx_rate_control *txrc,
2022 +                               struct ieee80211_supported_band *sband,
2023 +                               enum nl80211_chan_width chan_width,
2024                                 u32 mask,
2025                                 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
2026  {
2027 @@ -375,27 +387,17 @@ static void rate_idx_match_mask(struct i
2028                                   IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
2029                 alt_rate.count = rate->count;
2030                 if (rate_idx_match_legacy_mask(&alt_rate,
2031 -                                              txrc->sband->n_bitrates,
2032 -                                              mask)) {
2033 +                                              sband->n_bitrates, mask)) {
2034                         *rate = alt_rate;
2035                         return;
2036                 }
2037         } else {
2038 -               struct sk_buff *skb = txrc->skb;
2039 -               struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2040 -               __le16 fc;
2041 -
2042                 /* handle legacy rates */
2043 -               if (rate_idx_match_legacy_mask(rate, txrc->sband->n_bitrates,
2044 -                                              mask))
2045 +               if (rate_idx_match_legacy_mask(rate, sband->n_bitrates, mask))
2046                         return;
2047  
2048                 /* if HT BSS, and we handle a data frame, also try HT rates */
2049 -               if (txrc->bss_conf->chandef.width == NL80211_CHAN_WIDTH_20_NOHT)
2050 -                       return;
2051 -
2052 -               fc = hdr->frame_control;
2053 -               if (!ieee80211_is_data(fc))
2054 +               if (chan_width == NL80211_CHAN_WIDTH_20_NOHT)
2055                         return;
2056  
2057                 alt_rate.idx = 0;
2058 @@ -408,7 +410,7 @@ static void rate_idx_match_mask(struct i
2059  
2060                 alt_rate.flags |= IEEE80211_TX_RC_MCS;
2061  
2062 -               if (txrc->bss_conf->chandef.width == NL80211_CHAN_WIDTH_40)
2063 +               if (chan_width == NL80211_CHAN_WIDTH_40)
2064                         alt_rate.flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2065  
2066                 if (rate_idx_match_mcs_mask(&alt_rate, mcs_mask)) {
2067 @@ -426,6 +428,228 @@ static void rate_idx_match_mask(struct i
2068          */
2069  }
2070  
2071 +static void rate_fixup_ratelist(struct ieee80211_vif *vif,
2072 +                               struct ieee80211_supported_band *sband,
2073 +                               struct ieee80211_tx_info *info,
2074 +                               struct ieee80211_tx_rate *rates,
2075 +                               int max_rates)
2076 +{
2077 +       struct ieee80211_rate *rate;
2078 +       bool inval = false;
2079 +       int i;
2080 +
2081 +       /*
2082 +        * Set up the RTS/CTS rate as the fastest basic rate
2083 +        * that is not faster than the data rate unless there
2084 +        * is no basic rate slower than the data rate, in which
2085 +        * case we pick the slowest basic rate
2086 +        *
2087 +        * XXX: Should this check all retry rates?
2088 +        */
2089 +       if (!(rates[0].flags & IEEE80211_TX_RC_MCS)) {
2090 +               u32 basic_rates = vif->bss_conf.basic_rates;
2091 +               s8 baserate = basic_rates ? ffs(basic_rates - 1) : 0;
2092 +
2093 +               rate = &sband->bitrates[rates[0].idx];
2094 +
2095 +               for (i = 0; i < sband->n_bitrates; i++) {
2096 +                       /* must be a basic rate */
2097 +                       if (!(basic_rates & BIT(i)))
2098 +                               continue;
2099 +                       /* must not be faster than the data rate */
2100 +                       if (sband->bitrates[i].bitrate > rate->bitrate)
2101 +                               continue;
2102 +                       /* maximum */
2103 +                       if (sband->bitrates[baserate].bitrate <
2104 +                            sband->bitrates[i].bitrate)
2105 +                               baserate = i;
2106 +               }
2107 +
2108 +               info->control.rts_cts_rate_idx = baserate;
2109 +       }
2110 +
2111 +       for (i = 0; i < max_rates; i++) {
2112 +               /*
2113 +                * make sure there's no valid rate following
2114 +                * an invalid one, just in case drivers don't
2115 +                * take the API seriously to stop at -1.
2116 +                */
2117 +               if (inval) {
2118 +                       rates[i].idx = -1;
2119 +                       continue;
2120 +               }
2121 +               if (rates[i].idx < 0) {
2122 +                       inval = true;
2123 +                       continue;
2124 +               }
2125 +
2126 +               /*
2127 +                * For now assume MCS is already set up correctly, this
2128 +                * needs to be fixed.
2129 +                */
2130 +               if (rates[i].flags & IEEE80211_TX_RC_MCS) {
2131 +                       WARN_ON(rates[i].idx > 76);
2132 +
2133 +                       if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
2134 +                           info->control.use_cts_prot)
2135 +                               rates[i].flags |=
2136 +                                       IEEE80211_TX_RC_USE_CTS_PROTECT;
2137 +                       continue;
2138 +               }
2139 +
2140 +               if (rates[i].flags & IEEE80211_TX_RC_VHT_MCS) {
2141 +                       WARN_ON(ieee80211_rate_get_vht_mcs(&rates[i]) > 9);
2142 +                       continue;
2143 +               }
2144 +
2145 +               /* set up RTS protection if desired */
2146 +               if (info->control.use_rts) {
2147 +                       rates[i].flags |= IEEE80211_TX_RC_USE_RTS_CTS;
2148 +                       info->control.use_cts_prot = false;
2149 +               }
2150 +
2151 +               /* RC is busted */
2152 +               if (WARN_ON_ONCE(rates[i].idx >= sband->n_bitrates)) {
2153 +                       rates[i].idx = -1;
2154 +                       continue;
2155 +               }
2156 +
2157 +               rate = &sband->bitrates[rates[i].idx];
2158 +
2159 +               /* set up short preamble */
2160 +               if (info->control.short_preamble &&
2161 +                   rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
2162 +                       rates[i].flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
2163 +
2164 +               /* set up G protection */
2165 +               if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
2166 +                   info->control.use_cts_prot &&
2167 +                   rate->flags & IEEE80211_RATE_ERP_G)
2168 +                       rates[i].flags |= IEEE80211_TX_RC_USE_CTS_PROTECT;
2169 +       }
2170 +}
2171 +
2172 +
2173 +static void rate_control_fill_sta_table(struct ieee80211_sta *sta,
2174 +                                       struct ieee80211_tx_info *info,
2175 +                                       struct ieee80211_tx_rate *rates,
2176 +                                       int max_rates)
2177 +{
2178 +       struct ieee80211_sta_rates *ratetbl = NULL;
2179 +       int i;
2180 +
2181 +       if (sta && !info->control.skip_table)
2182 +               ratetbl = rcu_dereference(sta->rates);
2183 +
2184 +       /* Fill remaining rate slots with data from the sta rate table. */
2185 +       max_rates = min_t(int, max_rates, IEEE80211_TX_RATE_TABLE_SIZE);
2186 +       for (i = 0; i < max_rates; i++) {
2187 +               if (i < ARRAY_SIZE(info->control.rates) &&
2188 +                   info->control.rates[i].idx >= 0 &&
2189 +                   info->control.rates[i].count) {
2190 +                       if (rates != info->control.rates)
2191 +                               rates[i] = info->control.rates[i];
2192 +               } else if (ratetbl) {
2193 +                       rates[i].idx = ratetbl->rate[i].idx;
2194 +                       rates[i].flags = ratetbl->rate[i].flags;
2195 +                       if (info->control.use_rts)
2196 +                               rates[i].count = ratetbl->rate[i].count_rts;
2197 +                       else if (info->control.use_cts_prot)
2198 +                               rates[i].count = ratetbl->rate[i].count_cts;
2199 +                       else
2200 +                               rates[i].count = ratetbl->rate[i].count;
2201 +               } else {
2202 +                       rates[i].idx = -1;
2203 +                       rates[i].count = 0;
2204 +               }
2205 +
2206 +               if (rates[i].idx < 0 || !rates[i].count)
2207 +                       break;
2208 +       }
2209 +}
2210 +
2211 +static void rate_control_apply_mask(struct ieee80211_sub_if_data *sdata,
2212 +                                   struct ieee80211_sta *sta,
2213 +                                   struct ieee80211_supported_band *sband,
2214 +                                   struct ieee80211_tx_info *info,
2215 +                                   struct ieee80211_tx_rate *rates,
2216 +                                   int max_rates)
2217 +{
2218 +       enum nl80211_chan_width chan_width;
2219 +       u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
2220 +       bool has_mcs_mask;
2221 +       u32 mask;
2222 +       int i;
2223 +
2224 +       /*
2225 +        * Try to enforce the rateidx mask the user wanted. skip this if the
2226 +        * default mask (allow all rates) is used to save some processing for
2227 +        * the common case.
2228 +        */
2229 +       mask = sdata->rc_rateidx_mask[info->band];
2230 +       has_mcs_mask = sdata->rc_has_mcs_mask[info->band];
2231 +       if (mask == (1 << sband->n_bitrates) - 1 && !has_mcs_mask)
2232 +               return;
2233 +
2234 +       if (has_mcs_mask)
2235 +               memcpy(mcs_mask, sdata->rc_rateidx_mcs_mask[info->band],
2236 +                      sizeof(mcs_mask));
2237 +       else
2238 +               memset(mcs_mask, 0xff, sizeof(mcs_mask));
2239 +
2240 +       if (sta) {
2241 +               /* Filter out rates that the STA does not support */
2242 +               mask &= sta->supp_rates[info->band];
2243 +               for (i = 0; i < sizeof(mcs_mask); i++)
2244 +                       mcs_mask[i] &= sta->ht_cap.mcs.rx_mask[i];
2245 +       }
2246 +
2247 +       /*
2248 +        * Make sure the rate index selected for each TX rate is
2249 +        * included in the configured mask and change the rate indexes
2250 +        * if needed.
2251 +        */
2252 +       chan_width = sdata->vif.bss_conf.chandef.width;
2253 +       for (i = 0; i < max_rates; i++) {
2254 +               /* Skip invalid rates */
2255 +               if (rates[i].idx < 0)
2256 +                       break;
2257 +
2258 +               rate_idx_match_mask(&rates[i], sband, mask, chan_width,
2259 +                                   mcs_mask);
2260 +       }
2261 +}
2262 +
2263 +void ieee80211_get_tx_rates(struct ieee80211_vif *vif,
2264 +                           struct ieee80211_sta *sta,
2265 +                           struct sk_buff *skb,
2266 +                           struct ieee80211_tx_rate *dest,
2267 +                           int max_rates)
2268 +{
2269 +       struct ieee80211_sub_if_data *sdata;
2270 +       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2271 +       struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2272 +       struct ieee80211_supported_band *sband;
2273 +
2274 +       rate_control_fill_sta_table(sta, info, dest, max_rates);
2275 +
2276 +       if (!vif)
2277 +               return;
2278 +
2279 +       sdata = vif_to_sdata(vif);
2280 +       sband = sdata->local->hw.wiphy->bands[info->band];
2281 +
2282 +       if (ieee80211_is_data(hdr->frame_control))
2283 +               rate_control_apply_mask(sdata, sta, sband, info, dest, max_rates);
2284 +
2285 +       if (dest[0].idx < 0)
2286 +               __rate_control_send_low(&sdata->local->hw, sband, sta, info);
2287 +
2288 +       if (sta)
2289 +               rate_fixup_ratelist(vif, sband, info, dest, max_rates);
2290 +}
2291 +EXPORT_SYMBOL(ieee80211_get_tx_rates);
2292 +
2293  void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
2294                            struct sta_info *sta,
2295                            struct ieee80211_tx_rate_control *txrc)
2296 @@ -435,8 +659,6 @@ void rate_control_get_rate(struct ieee80
2297         struct ieee80211_sta *ista = NULL;
2298         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
2299         int i;
2300 -       u32 mask;
2301 -       u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
2302  
2303         if (sta && test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) {
2304                 ista = &sta->sta;
2305 @@ -454,37 +676,27 @@ void rate_control_get_rate(struct ieee80
2306  
2307         ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
2308  
2309 -       /*
2310 -        * Try to enforce the rateidx mask the user wanted. skip this if the
2311 -        * default mask (allow all rates) is used to save some processing for
2312 -        * the common case.
2313 -        */
2314 -       mask = sdata->rc_rateidx_mask[info->band];
2315 -       memcpy(mcs_mask, sdata->rc_rateidx_mcs_mask[info->band],
2316 -              sizeof(mcs_mask));
2317 -       if (mask != (1 << txrc->sband->n_bitrates) - 1) {
2318 -               if (sta) {
2319 -                       /* Filter out rates that the STA does not support */
2320 -                       mask &= sta->sta.supp_rates[info->band];
2321 -                       for (i = 0; i < sizeof(mcs_mask); i++)
2322 -                               mcs_mask[i] &= sta->sta.ht_cap.mcs.rx_mask[i];
2323 -               }
2324 -               /*
2325 -                * Make sure the rate index selected for each TX rate is
2326 -                * included in the configured mask and change the rate indexes
2327 -                * if needed.
2328 -                */
2329 -               for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2330 -                       /* Skip invalid rates */
2331 -                       if (info->control.rates[i].idx < 0)
2332 -                               break;
2333 -                       rate_idx_match_mask(&info->control.rates[i], txrc,
2334 -                                           mask, mcs_mask);
2335 -               }
2336 -       }
2337 +       if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_RC_TABLE)
2338 +               return;
2339  
2340 -       BUG_ON(info->control.rates[0].idx < 0);
2341 +       ieee80211_get_tx_rates(&sdata->vif, ista, txrc->skb,
2342 +                              info->control.rates,
2343 +                              ARRAY_SIZE(info->control.rates));
2344 +}
2345 +
2346 +int rate_control_set_rates(struct ieee80211_hw *hw,
2347 +                          struct ieee80211_sta *pubsta,
2348 +                          struct ieee80211_sta_rates *rates)
2349 +{
2350 +       struct ieee80211_sta_rates *old = rcu_dereference(pubsta->rates);
2351 +
2352 +       rcu_assign_pointer(pubsta->rates, rates);
2353 +       if (old)
2354 +               kfree_rcu(old, rcu_head);
2355 +
2356 +       return 0;
2357  }
2358 +EXPORT_SYMBOL(rate_control_set_rates);
2359  
2360  int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
2361                                  const char *name)
2362 --- a/net/mac80211/rc80211_minstrel.c
2363 +++ b/net/mac80211/rc80211_minstrel.c
2364 @@ -84,6 +84,50 @@ minstrel_sort_best_tp_rates(struct minst
2365  }
2366  
2367  static void
2368 +minstrel_set_rate(struct minstrel_sta_info *mi, struct ieee80211_sta_rates *ratetbl,
2369 +                 int offset, int idx)
2370 +{
2371 +       struct minstrel_rate *r = &mi->r[idx];
2372 +
2373 +       ratetbl->rate[offset].idx = r->rix;
2374 +       ratetbl->rate[offset].count = r->adjusted_retry_count;
2375 +       ratetbl->rate[offset].count_cts = r->retry_count_cts;
2376 +       ratetbl->rate[offset].count_rts = r->retry_count_rtscts;
2377 +}
2378 +
2379 +static void
2380 +minstrel_update_rates(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
2381 +{
2382 +       struct ieee80211_sta_rates *ratetbl;
2383 +       int i = 0;
2384 +
2385 +       ratetbl = kzalloc(sizeof(*ratetbl), GFP_ATOMIC);
2386 +       if (!ratetbl)
2387 +               return;
2388 +
2389 +       /* Start with max_tp_rate */
2390 +       minstrel_set_rate(mi, ratetbl, i++, mi->max_tp_rate[0]);
2391 +
2392 +       if (mp->hw->max_rates >= 3) {
2393 +               /* At least 3 tx rates supported, use max_tp_rate2 next */
2394 +               minstrel_set_rate(mi, ratetbl, i++, mi->max_tp_rate[1]);
2395 +       }
2396 +
2397 +       if (mp->hw->max_rates >= 2) {
2398 +               /* At least 2 tx rates supported, use max_prob_rate next */
2399 +               minstrel_set_rate(mi, ratetbl, i++, mi->max_prob_rate);
2400 +       }
2401 +
2402 +       /* Use lowest rate last */
2403 +       ratetbl->rate[i].idx = mi->lowest_rix;
2404 +       ratetbl->rate[i].count = mp->max_retry;
2405 +       ratetbl->rate[i].count_cts = mp->max_retry;
2406 +       ratetbl->rate[i].count_rts = mp->max_retry;
2407 +
2408 +       rate_control_set_rates(mp->hw, mi->sta, ratetbl);
2409 +}
2410 +
2411 +static void
2412  minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
2413  {
2414         u8 tmp_tp_rate[MAX_THR_RATES];
2415 @@ -161,6 +205,8 @@ minstrel_update_stats(struct minstrel_pr
2416  
2417         /* Reset update timer */
2418         mi->stats_update = jiffies;
2419 +
2420 +       minstrel_update_rates(mp, mi);
2421  }
2422  
2423  static void
2424 @@ -209,9 +255,9 @@ minstrel_get_retry_count(struct minstrel
2425  {
2426         unsigned int retry = mr->adjusted_retry_count;
2427  
2428 -       if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
2429 +       if (info->control.use_rts)
2430                 retry = max(2U, min(mr->retry_count_rtscts, retry));
2431 -       else if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
2432 +       else if (info->control.use_cts_prot)
2433                 retry = max(2U, min(mr->retry_count_cts, retry));
2434         return retry;
2435  }
2436 @@ -240,13 +286,12 @@ minstrel_get_rate(void *priv, struct iee
2437         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2438         struct minstrel_sta_info *mi = priv_sta;
2439         struct minstrel_priv *mp = priv;
2440 -       struct ieee80211_tx_rate *ar = info->control.rates;
2441 -       unsigned int ndx, sample_ndx = 0;
2442 +       struct ieee80211_tx_rate *rate = &info->control.rates[0];
2443 +       struct minstrel_rate *msr, *mr;
2444 +       unsigned int ndx;
2445         bool mrr_capable;
2446 -       bool indirect_rate_sampling = false;
2447 -       bool rate_sampling = false;
2448 -       int i, delta;
2449 -       int mrr_ndx[3];
2450 +       bool prev_sample = mi->prev_sample;
2451 +       int delta;
2452         int sampling_ratio;
2453  
2454         /* management/no-ack frames do not use rate control */
2455 @@ -262,107 +307,75 @@ minstrel_get_rate(void *priv, struct iee
2456         else
2457                 sampling_ratio = mp->lookaround_rate;
2458  
2459 -       /* init rateindex [ndx] with max throughput rate */
2460 -       ndx = mi->max_tp_rate[0];
2461 -
2462         /* increase sum packet counter */
2463         mi->packet_count++;
2464  
2465         delta = (mi->packet_count * sampling_ratio / 100) -
2466                         (mi->sample_count + mi->sample_deferred / 2);
2467  
2468 -       /* delta > 0: sampling required */
2469 -       if ((delta > 0) && (mrr_capable || !mi->prev_sample)) {
2470 -               struct minstrel_rate *msr;
2471 -               if (mi->packet_count >= 10000) {
2472 -                       mi->sample_deferred = 0;
2473 -                       mi->sample_count = 0;
2474 -                       mi->packet_count = 0;
2475 -               } else if (delta > mi->n_rates * 2) {
2476 -                       /* With multi-rate retry, not every planned sample
2477 -                        * attempt actually gets used, due to the way the retry
2478 -                        * chain is set up - [max_tp,sample,prob,lowest] for
2479 -                        * sample_rate < max_tp.
2480 -                        *
2481 -                        * If there's too much sampling backlog and the link
2482 -                        * starts getting worse, minstrel would start bursting
2483 -                        * out lots of sampling frames, which would result
2484 -                        * in a large throughput loss. */
2485 -                       mi->sample_count += (delta - mi->n_rates * 2);
2486 -               }
2487 +       /* delta < 0: no sampling required */
2488 +       mi->prev_sample = false;
2489 +       if (delta < 0 || (!mrr_capable && prev_sample))
2490 +               return;
2491  
2492 -               /* get next random rate sample */
2493 -               sample_ndx = minstrel_get_next_sample(mi);
2494 -               msr = &mi->r[sample_ndx];
2495 -               rate_sampling = true;
2496 -
2497 -               /* Decide if direct ( 1st mrr stage) or indirect (2nd mrr stage)
2498 -                * rate sampling method should be used.
2499 -                * Respect such rates that are not sampled for 20 interations.
2500 -                */
2501 -               if (mrr_capable &&
2502 -                   msr->perfect_tx_time > mi->r[ndx].perfect_tx_time &&
2503 -                   msr->sample_skipped < 20)
2504 -                               indirect_rate_sampling = true;
2505 -
2506 -               if (!indirect_rate_sampling) {
2507 -                       if (msr->sample_limit != 0) {
2508 -                               ndx = sample_ndx;
2509 -                               mi->sample_count++;
2510 -                               if (msr->sample_limit > 0)
2511 -                                       msr->sample_limit--;
2512 -                       } else
2513 -                               rate_sampling = false;
2514 -               } else {
2515 -                       /* Only use IEEE80211_TX_CTL_RATE_CTRL_PROBE to mark
2516 -                        * packets that have the sampling rate deferred to the
2517 -                        * second MRR stage. Increase the sample counter only
2518 -                        * if the deferred sample rate was actually used.
2519 -                        * Use the sample_deferred counter to make sure that
2520 -                        * the sampling is not done in large bursts */
2521 -                       info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
2522 -                       mi->sample_deferred++;
2523 -               }
2524 +       if (mi->packet_count >= 10000) {
2525 +               mi->sample_deferred = 0;
2526 +               mi->sample_count = 0;
2527 +               mi->packet_count = 0;
2528 +       } else if (delta > mi->n_rates * 2) {
2529 +               /* With multi-rate retry, not every planned sample
2530 +                * attempt actually gets used, due to the way the retry
2531 +                * chain is set up - [max_tp,sample,prob,lowest] for
2532 +                * sample_rate < max_tp.
2533 +                *
2534 +                * If there's too much sampling backlog and the link
2535 +                * starts getting worse, minstrel would start bursting
2536 +                * out lots of sampling frames, which would result
2537 +                * in a large throughput loss. */
2538 +               mi->sample_count += (delta - mi->n_rates * 2);
2539 +       }
2540 +
2541 +       /* get next random rate sample */
2542 +       ndx = minstrel_get_next_sample(mi);
2543 +       msr = &mi->r[ndx];
2544 +       mr = &mi->r[mi->max_tp_rate[0]];
2545 +
2546 +       /* Decide if direct ( 1st mrr stage) or indirect (2nd mrr stage)
2547 +        * rate sampling method should be used.
2548 +        * Respect such rates that are not sampled for 20 interations.
2549 +        */
2550 +       if (mrr_capable &&
2551 +           msr->perfect_tx_time > mr->perfect_tx_time &&
2552 +           msr->sample_skipped < 20) {
2553 +               /* Only use IEEE80211_TX_CTL_RATE_CTRL_PROBE to mark
2554 +                * packets that have the sampling rate deferred to the
2555 +                * second MRR stage. Increase the sample counter only
2556 +                * if the deferred sample rate was actually used.
2557 +                * Use the sample_deferred counter to make sure that
2558 +                * the sampling is not done in large bursts */
2559 +               info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
2560 +               rate++;
2561 +               mi->sample_deferred++;
2562 +       } else {
2563 +               if (!msr->sample_limit != 0)
2564 +                       return;
2565 +
2566 +               mi->sample_count++;
2567 +               if (msr->sample_limit > 0)
2568 +                       msr->sample_limit--;
2569         }
2570 -       mi->prev_sample = rate_sampling;
2571  
2572         /* If we're not using MRR and the sampling rate already
2573          * has a probability of >95%, we shouldn't be attempting
2574          * to use it, as this only wastes precious airtime */
2575 -       if (!mrr_capable && rate_sampling &&
2576 +       if (!mrr_capable &&
2577            (mi->r[ndx].probability > MINSTREL_FRAC(95, 100)))
2578 -               ndx = mi->max_tp_rate[0];
2579 -
2580 -       /* mrr setup for 1st stage */
2581 -       ar[0].idx = mi->r[ndx].rix;
2582 -       ar[0].count = minstrel_get_retry_count(&mi->r[ndx], info);
2583 -
2584 -       /* non mrr setup for 2nd stage */
2585 -       if (!mrr_capable) {
2586 -               if (!rate_sampling)
2587 -                       ar[0].count = mp->max_retry;
2588 -               ar[1].idx = mi->lowest_rix;
2589 -               ar[1].count = mp->max_retry;
2590                 return;
2591 -       }
2592  
2593 -       /* mrr setup for 2nd stage */
2594 -       if (rate_sampling) {
2595 -               if (indirect_rate_sampling)
2596 -                       mrr_ndx[0] = sample_ndx;
2597 -               else
2598 -                       mrr_ndx[0] = mi->max_tp_rate[0];
2599 -       } else {
2600 -               mrr_ndx[0] = mi->max_tp_rate[1];
2601 -       }
2602 +       mi->prev_sample = true;
2603  
2604 -       /* mrr setup for 3rd & 4th stage */
2605 -       mrr_ndx[1] = mi->max_prob_rate;
2606 -       mrr_ndx[2] = 0;
2607 -       for (i = 1; i < 4; i++) {
2608 -               ar[i].idx = mi->r[mrr_ndx[i - 1]].rix;
2609 -               ar[i].count = mi->r[mrr_ndx[i - 1]].adjusted_retry_count;
2610 -       }
2611 +       rate->idx = mi->r[ndx].rix;
2612 +       rate->count = minstrel_get_retry_count(&mi->r[ndx], info);
2613  }
2614  
2615  
2616 @@ -412,12 +425,16 @@ minstrel_rate_init(void *priv, struct ie
2617         unsigned int i, n = 0;
2618         unsigned int t_slot = 9; /* FIXME: get real slot time */
2619  
2620 +       mi->sta = sta;
2621         mi->lowest_rix = rate_lowest_index(sband, sta);
2622         ctl_rate = &sband->bitrates[mi->lowest_rix];
2623         mi->sp_ack_dur = ieee80211_frame_duration(sband->band, 10,
2624                                 ctl_rate->bitrate,
2625                                 !!(ctl_rate->flags & IEEE80211_RATE_ERP_G), 1);
2626  
2627 +       memset(mi->max_tp_rate, 0, sizeof(mi->max_tp_rate));
2628 +       mi->max_prob_rate = 0;
2629 +
2630         for (i = 0; i < sband->n_bitrates; i++) {
2631                 struct minstrel_rate *mr = &mi->r[n];
2632                 unsigned int tx_time = 0, tx_time_cts = 0, tx_time_rtscts = 0;
2633 @@ -460,6 +477,8 @@ minstrel_rate_init(void *priv, struct ie
2634                 } while ((tx_time < mp->segment_size) &&
2635                                 (++mr->retry_count < mp->max_retry));
2636                 mr->adjusted_retry_count = mr->retry_count;
2637 +               if (!(sband->bitrates[i].flags & IEEE80211_RATE_ERP_G))
2638 +                       mr->retry_count_cts = mr->retry_count;
2639         }
2640  
2641         for (i = n; i < sband->n_bitrates; i++) {
2642 @@ -471,6 +490,7 @@ minstrel_rate_init(void *priv, struct ie
2643         mi->stats_update = jiffies;
2644  
2645         init_sample_table(mi);
2646 +       minstrel_update_rates(mp, mi);
2647  }
2648  
2649  static void *
2650 --- a/net/mac80211/rc80211_minstrel.h
2651 +++ b/net/mac80211/rc80211_minstrel.h
2652 @@ -9,7 +9,8 @@
2653  #ifndef __RC_MINSTREL_H
2654  #define __RC_MINSTREL_H
2655  
2656 -#define EWMA_LEVEL     75      /* ewma weighting factor [%] */
2657 +#define EWMA_LEVEL     96      /* ewma weighting factor [/EWMA_DIV] */
2658 +#define EWMA_DIV       128
2659  #define SAMPLE_COLUMNS 10      /* number of columns in sample table */
2660  
2661  
2662 @@ -27,7 +28,7 @@
2663  static inline int
2664  minstrel_ewma(int old, int new, int weight)
2665  {
2666 -       return (new * (100 - weight) + old * weight) / 100;
2667 +       return (new * (EWMA_DIV - weight) + old * weight) / EWMA_DIV;
2668  }
2669  
2670  
2671 @@ -62,6 +63,8 @@ struct minstrel_rate {
2672  };
2673  
2674  struct minstrel_sta_info {
2675 +       struct ieee80211_sta *sta;
2676 +
2677         unsigned long stats_update;
2678         unsigned int sp_ack_dur;
2679         unsigned int rate_avg;
2680 --- a/net/mac80211/rc80211_minstrel_debugfs.c
2681 +++ b/net/mac80211/rc80211_minstrel_debugfs.c
2682 @@ -68,7 +68,7 @@ minstrel_stats_open(struct inode *inode,
2683  
2684         file->private_data = ms;
2685         p = ms->buf;
2686 -       p += sprintf(p, "rate     throughput  ewma prob   this prob  "
2687 +       p += sprintf(p, "rate      throughput  ewma prob  this prob  "
2688                         "this succ/attempt   success    attempts\n");
2689         for (i = 0; i < mi->n_rates; i++) {
2690                 struct minstrel_rate *mr = &mi->r[i];
2691 @@ -86,7 +86,7 @@ minstrel_stats_open(struct inode *inode,
2692                 eprob = MINSTREL_TRUNC(mr->probability * 1000);
2693  
2694                 p += sprintf(p, "  %6u.%1u   %6u.%1u   %6u.%1u        "
2695 -                               "%3u(%3u)   %8llu    %8llu\n",
2696 +                               "   %3u(%3u)  %8llu    %8llu\n",
2697                                 tp / 10, tp % 10,
2698                                 eprob / 10, eprob % 10,
2699                                 prob / 10, prob % 10,
2700 --- a/net/mac80211/rc80211_minstrel_ht.c
2701 +++ b/net/mac80211/rc80211_minstrel_ht.c
2702 @@ -126,6 +126,9 @@ const struct mcs_group minstrel_mcs_grou
2703  
2704  static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES];
2705  
2706 +static void
2707 +minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi);
2708 +
2709  /*
2710   * Look up an MCS group index based on mac80211 rate information
2711   */
2712 @@ -244,6 +247,7 @@ minstrel_ht_update_stats(struct minstrel
2713         struct minstrel_rate_stats *mr;
2714         int cur_prob, cur_prob_tp, cur_tp, cur_tp2;
2715         int group, i, index;
2716 +       bool mi_rates_valid = false;
2717  
2718         if (mi->ampdu_packets > 0) {
2719                 mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
2720 @@ -254,11 +258,10 @@ minstrel_ht_update_stats(struct minstrel
2721  
2722         mi->sample_slow = 0;
2723         mi->sample_count = 0;
2724 -       mi->max_tp_rate = 0;
2725 -       mi->max_tp_rate2 = 0;
2726 -       mi->max_prob_rate = 0;
2727  
2728         for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
2729 +               bool mg_rates_valid = false;
2730 +
2731                 cur_prob = 0;
2732                 cur_prob_tp = 0;
2733                 cur_tp = 0;
2734 @@ -268,15 +271,24 @@ minstrel_ht_update_stats(struct minstrel
2735                 if (!mg->supported)
2736                         continue;
2737  
2738 -               mg->max_tp_rate = 0;
2739 -               mg->max_tp_rate2 = 0;
2740 -               mg->max_prob_rate = 0;
2741                 mi->sample_count++;
2742  
2743                 for (i = 0; i < MCS_GROUP_RATES; i++) {
2744                         if (!(mg->supported & BIT(i)))
2745                                 continue;
2746  
2747 +                       /* initialize rates selections starting indexes */
2748 +                       if (!mg_rates_valid) {
2749 +                               mg->max_tp_rate = mg->max_tp_rate2 =
2750 +                                       mg->max_prob_rate = i;
2751 +                               if (!mi_rates_valid) {
2752 +                                       mi->max_tp_rate = mi->max_tp_rate2 =
2753 +                                               mi->max_prob_rate = i;
2754 +                                       mi_rates_valid = true;
2755 +                               }
2756 +                               mg_rates_valid = true;
2757 +                       }
2758 +
2759                         mr = &mg->rates[i];
2760                         mr->retry_updated = false;
2761                         index = MCS_GROUP_RATES * group + i;
2762 @@ -456,7 +468,7 @@ minstrel_ht_tx_status(void *priv, struct
2763         struct ieee80211_tx_rate *ar = info->status.rates;
2764         struct minstrel_rate_stats *rate, *rate2;
2765         struct minstrel_priv *mp = priv;
2766 -       bool last;
2767 +       bool last, update = false;
2768         int i;
2769  
2770         if (!msp->is_ht)
2771 @@ -505,21 +517,29 @@ minstrel_ht_tx_status(void *priv, struct
2772         rate = minstrel_get_ratestats(mi, mi->max_tp_rate);
2773         if (rate->attempts > 30 &&
2774             MINSTREL_FRAC(rate->success, rate->attempts) <
2775 -           MINSTREL_FRAC(20, 100))
2776 +           MINSTREL_FRAC(20, 100)) {
2777                 minstrel_downgrade_rate(mi, &mi->max_tp_rate, true);
2778 +               update = true;
2779 +       }
2780  
2781         rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate2);
2782         if (rate2->attempts > 30 &&
2783             MINSTREL_FRAC(rate2->success, rate2->attempts) <
2784 -           MINSTREL_FRAC(20, 100))
2785 +           MINSTREL_FRAC(20, 100)) {
2786                 minstrel_downgrade_rate(mi, &mi->max_tp_rate2, false);
2787 +               update = true;
2788 +       }
2789  
2790         if (time_after(jiffies, mi->stats_update + (mp->update_interval / 2 * HZ) / 1000)) {
2791 +               update = true;
2792                 minstrel_ht_update_stats(mp, mi);
2793                 if (!(info->flags & IEEE80211_TX_CTL_AMPDU) &&
2794                     mi->max_prob_rate / MCS_GROUP_RATES != MINSTREL_CCK_GROUP)
2795                         minstrel_aggr_check(sta, skb);
2796         }
2797 +
2798 +       if (update)
2799 +               minstrel_ht_update_rates(mp, mi);
2800  }
2801  
2802  static void
2803 @@ -580,39 +600,73 @@ minstrel_calc_retransmit(struct minstrel
2804                  (++mr->retry_count < mp->max_retry));
2805  }
2806  
2807 -
2808  static void
2809  minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
2810 -                     struct ieee80211_tx_rate *rate, int index,
2811 -                     bool sample, bool rtscts)
2812 +                     struct ieee80211_sta_rates *ratetbl, int offset, int index)
2813  {
2814         const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
2815         struct minstrel_rate_stats *mr;
2816 +       u8 idx;
2817 +       u16 flags;
2818  
2819         mr = minstrel_get_ratestats(mi, index);
2820         if (!mr->retry_updated)
2821                 minstrel_calc_retransmit(mp, mi, index);
2822  
2823 -       if (sample)
2824 -               rate->count = 1;
2825 -       else if (mr->probability < MINSTREL_FRAC(20, 100))
2826 -               rate->count = 2;
2827 -       else if (rtscts)
2828 -               rate->count = mr->retry_count_rtscts;
2829 -       else
2830 -               rate->count = mr->retry_count;
2831 -
2832 -       rate->flags = 0;
2833 -       if (rtscts)
2834 -               rate->flags |= IEEE80211_TX_RC_USE_RTS_CTS;
2835 +       if (mr->probability < MINSTREL_FRAC(20, 100) || !mr->retry_count) {
2836 +               ratetbl->rate[offset].count = 2;
2837 +               ratetbl->rate[offset].count_rts = 2;
2838 +               ratetbl->rate[offset].count_cts = 2;
2839 +       } else {
2840 +               ratetbl->rate[offset].count = mr->retry_count;
2841 +               ratetbl->rate[offset].count_cts = mr->retry_count;
2842 +               ratetbl->rate[offset].count_rts = mr->retry_count_rtscts;
2843 +       }
2844  
2845         if (index / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) {
2846 -               rate->idx = mp->cck_rates[index % ARRAY_SIZE(mp->cck_rates)];
2847 +               idx = mp->cck_rates[index % ARRAY_SIZE(mp->cck_rates)];
2848 +               flags = 0;
2849 +       } else {
2850 +               idx = index % MCS_GROUP_RATES +
2851 +                     (group->streams - 1) * MCS_GROUP_RATES;
2852 +               flags = IEEE80211_TX_RC_MCS | group->flags;
2853 +       }
2854 +
2855 +       if (offset > 0) {
2856 +               ratetbl->rate[offset].count = ratetbl->rate[offset].count_rts;
2857 +               flags |= IEEE80211_TX_RC_USE_RTS_CTS;
2858 +       }
2859 +
2860 +       ratetbl->rate[offset].idx = idx;
2861 +       ratetbl->rate[offset].flags = flags;
2862 +}
2863 +
2864 +static void
2865 +minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
2866 +{
2867 +       struct ieee80211_sta_rates *rates;
2868 +       int i = 0;
2869 +
2870 +       rates = kzalloc(sizeof(*rates), GFP_ATOMIC);
2871 +       if (!rates)
2872                 return;
2873 +
2874 +       /* Start with max_tp_rate */
2875 +       minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate);
2876 +
2877 +       if (mp->hw->max_rates >= 3) {
2878 +               /* At least 3 tx rates supported, use max_tp_rate2 next */
2879 +               minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate2);
2880 +       }
2881 +
2882 +       if (mp->hw->max_rates >= 2) {
2883 +               /*
2884 +                * At least 2 tx rates supported, use max_prob_rate next */
2885 +               minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_prob_rate);
2886         }
2887  
2888 -       rate->flags |= IEEE80211_TX_RC_MCS | group->flags;
2889 -       rate->idx = index % MCS_GROUP_RATES + (group->streams - 1) * MCS_GROUP_RATES;
2890 +       rates->rate[i].idx = -1;
2891 +       rate_control_set_rates(mp->hw, mi->sta, rates);
2892  }
2893  
2894  static inline int
2895 @@ -702,13 +756,13 @@ static void
2896  minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
2897                       struct ieee80211_tx_rate_control *txrc)
2898  {
2899 +       const struct mcs_group *sample_group;
2900         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
2901 -       struct ieee80211_tx_rate *ar = info->status.rates;
2902 +       struct ieee80211_tx_rate *rate = &info->status.rates[0];
2903         struct minstrel_ht_sta_priv *msp = priv_sta;
2904         struct minstrel_ht_sta *mi = &msp->ht;
2905         struct minstrel_priv *mp = priv;
2906         int sample_idx;
2907 -       bool sample = false;
2908  
2909         if (rate_control_send_low(sta, priv_sta, txrc))
2910                 return;
2911 @@ -736,51 +790,6 @@ minstrel_ht_get_rate(void *priv, struct 
2912         }
2913  #endif
2914  
2915 -       if (sample_idx >= 0) {
2916 -               sample = true;
2917 -               minstrel_ht_set_rate(mp, mi, &ar[0], sample_idx,
2918 -                       true, false);
2919 -               info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
2920 -       } else {
2921 -               minstrel_ht_set_rate(mp, mi, &ar[0], mi->max_tp_rate,
2922 -                       false, false);
2923 -       }
2924 -
2925 -       if (mp->hw->max_rates >= 3) {
2926 -               /*
2927 -                * At least 3 tx rates supported, use
2928 -                * sample_rate -> max_tp_rate -> max_prob_rate for sampling and
2929 -                * max_tp_rate -> max_tp_rate2 -> max_prob_rate by default.
2930 -                */
2931 -               if (sample_idx >= 0)
2932 -                       minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_tp_rate,
2933 -                               false, false);
2934 -               else
2935 -                       minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_tp_rate2,
2936 -                               false, true);
2937 -
2938 -               minstrel_ht_set_rate(mp, mi, &ar[2], mi->max_prob_rate,
2939 -                                    false, !sample);
2940 -
2941 -               ar[3].count = 0;
2942 -               ar[3].idx = -1;
2943 -       } else if (mp->hw->max_rates == 2) {
2944 -               /*
2945 -                * Only 2 tx rates supported, use
2946 -                * sample_rate -> max_prob_rate for sampling and
2947 -                * max_tp_rate -> max_prob_rate by default.
2948 -                */
2949 -               minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_prob_rate,
2950 -                                    false, !sample);
2951 -
2952 -               ar[2].count = 0;
2953 -               ar[2].idx = -1;
2954 -       } else {
2955 -               /* Not using MRR, only use the first rate */
2956 -               ar[1].count = 0;
2957 -               ar[1].idx = -1;
2958 -       }
2959 -
2960         mi->total_packets++;
2961  
2962         /* wraparound */
2963 @@ -788,6 +797,16 @@ minstrel_ht_get_rate(void *priv, struct 
2964                 mi->total_packets = 0;
2965                 mi->sample_packets = 0;
2966         }
2967 +
2968 +       if (sample_idx < 0)
2969 +               return;
2970 +
2971 +       sample_group = &minstrel_mcs_groups[sample_idx / MCS_GROUP_RATES];
2972 +       info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
2973 +       rate->idx = sample_idx % MCS_GROUP_RATES +
2974 +                   (sample_group->streams - 1) * MCS_GROUP_RATES;
2975 +       rate->flags = IEEE80211_TX_RC_MCS | sample_group->flags;
2976 +       rate->count = 1;
2977  }
2978  
2979  static void
2980 @@ -837,6 +856,8 @@ minstrel_ht_update_caps(void *priv, stru
2981  
2982         msp->is_ht = true;
2983         memset(mi, 0, sizeof(*mi));
2984 +
2985 +       mi->sta = sta;
2986         mi->stats_update = jiffies;
2987  
2988         ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1);
2989 @@ -898,6 +919,9 @@ minstrel_ht_update_caps(void *priv, stru
2990         if (!n_supported)
2991                 goto use_legacy;
2992  
2993 +       minstrel_ht_update_stats(mp, mi);
2994 +       minstrel_ht_update_rates(mp, mi);
2995 +
2996         return;
2997  
2998  use_legacy:
2999 --- a/net/mac80211/rc80211_minstrel_ht.h
3000 +++ b/net/mac80211/rc80211_minstrel_ht.h
3001 @@ -65,6 +65,8 @@ struct minstrel_mcs_group_data {
3002  };
3003  
3004  struct minstrel_ht_sta {
3005 +       struct ieee80211_sta *sta;
3006 +
3007         /* ampdu length (average, per sampling interval) */
3008         unsigned int ampdu_len;
3009         unsigned int ampdu_packets;
3010 --- a/net/mac80211/rx.c
3011 +++ b/net/mac80211/rx.c
3012 @@ -1372,6 +1372,7 @@ ieee80211_rx_h_sta_process(struct ieee80
3013         struct sk_buff *skb = rx->skb;
3014         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
3015         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3016 +       int i;
3017  
3018         if (!sta)
3019                 return RX_CONTINUE;
3020 @@ -1422,6 +1423,19 @@ ieee80211_rx_h_sta_process(struct ieee80
3021                 ewma_add(&sta->avg_signal, -status->signal);
3022         }
3023  
3024 +       if (status->chains) {
3025 +               sta->chains = status->chains;
3026 +               for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
3027 +                       int signal = status->chain_signal[i];
3028 +
3029 +                       if (!(status->chains & BIT(i)))
3030 +                               continue;
3031 +
3032 +                       sta->chain_signal_last[i] = signal;
3033 +                       ewma_add(&sta->chain_signal_avg[i], -signal);
3034 +               }
3035 +       }
3036 +
3037         /*
3038          * Change STA power saving mode only at the end of a frame
3039          * exchange sequence.
3040 @@ -2085,6 +2099,7 @@ ieee80211_rx_h_mesh_fwding(struct ieee80
3041         }
3042  
3043         fwd_hdr =  (struct ieee80211_hdr *) fwd_skb->data;
3044 +       fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
3045         info = IEEE80211_SKB_CB(fwd_skb);
3046         memset(info, 0, sizeof(*info));
3047         info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
3048 @@ -2356,6 +2371,7 @@ ieee80211_rx_h_action(struct ieee80211_r
3049                     sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3050                     sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3051                     sdata->vif.type != NL80211_IFTYPE_AP &&
3052 +                   sdata->vif.type != NL80211_IFTYPE_WDS &&
3053                     sdata->vif.type != NL80211_IFTYPE_ADHOC)
3054                         break;
3055  
3056 @@ -2423,6 +2439,22 @@ ieee80211_rx_h_action(struct ieee80211_r
3057                 }
3058  
3059                 break;
3060 +       case WLAN_CATEGORY_PUBLIC:
3061 +               if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3062 +                       goto invalid;
3063 +               if (sdata->vif.type != NL80211_IFTYPE_STATION)
3064 +                       break;
3065 +               if (!rx->sta)
3066 +                       break;
3067 +               if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
3068 +                       break;
3069 +               if (mgmt->u.action.u.ext_chan_switch.action_code !=
3070 +                               WLAN_PUB_ACTION_EXT_CHANSW_ANN)
3071 +                       break;
3072 +               if (len < offsetof(struct ieee80211_mgmt,
3073 +                                  u.action.u.ext_chan_switch.variable))
3074 +                       goto invalid;
3075 +               goto queue;
3076         case WLAN_CATEGORY_VHT:
3077                 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3078                     sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3079 @@ -2506,10 +2538,6 @@ ieee80211_rx_h_action(struct ieee80211_r
3080                         ieee80211_process_measurement_req(sdata, mgmt, len);
3081                         goto handled;
3082                 case WLAN_ACTION_SPCT_CHL_SWITCH:
3083 -                       if (len < (IEEE80211_MIN_ACTION_SIZE +
3084 -                                  sizeof(mgmt->u.action.u.chan_switch)))
3085 -                               break;
3086 -
3087                         if (sdata->vif.type != NL80211_IFTYPE_STATION)
3088                                 break;
3089  
3090 @@ -2695,14 +2723,15 @@ ieee80211_rx_h_mgmt(struct ieee80211_rx_
3091  
3092         if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3093             sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3094 -           sdata->vif.type != NL80211_IFTYPE_STATION)
3095 +           sdata->vif.type != NL80211_IFTYPE_STATION &&
3096 +           sdata->vif.type != NL80211_IFTYPE_WDS)
3097                 return RX_DROP_MONITOR;
3098  
3099         switch (stype) {
3100         case cpu_to_le16(IEEE80211_STYPE_AUTH):
3101         case cpu_to_le16(IEEE80211_STYPE_BEACON):
3102         case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3103 -               /* process for all: mesh, mlme, ibss */
3104 +               /* process for all: mesh, mlme, ibss, wds */
3105                 break;
3106         case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3107         case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
3108 @@ -3023,6 +3052,9 @@ static int prepare_for_handlers(struct i
3109                          * and location updates. Note that mac80211
3110                          * itself never looks at these frames.
3111                          */
3112 +                       if (!multicast &&
3113 +                           !ether_addr_equal(sdata->vif.addr, hdr->addr1))
3114 +                               return 0;
3115                         if (ieee80211_is_public_action(hdr, skb->len))
3116                                 return 1;
3117                         if (!ieee80211_is_beacon(hdr->frame_control))
3118 @@ -3031,10 +3063,16 @@ static int prepare_for_handlers(struct i
3119                 }
3120                 break;
3121         case NL80211_IFTYPE_WDS:
3122 -               if (bssid || !ieee80211_is_data(hdr->frame_control))
3123 -                       return 0;
3124                 if (!ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2))
3125                         return 0;
3126 +
3127 +               if (ieee80211_is_data(hdr->frame_control) ||
3128 +                   ieee80211_is_action(hdr->frame_control)) {
3129 +                       if (compare_ether_addr(sdata->vif.addr, hdr->addr1))
3130 +                               return 0;
3131 +               } else if (!ieee80211_is_beacon(hdr->frame_control))
3132 +                       return 0;
3133 +
3134                 break;
3135         case NL80211_IFTYPE_P2P_DEVICE:
3136                 if (!ieee80211_is_public_action(hdr, skb->len) &&
3137 --- a/net/mac80211/scan.c
3138 +++ b/net/mac80211/scan.c
3139 @@ -181,7 +181,7 @@ void ieee80211_scan_rx(struct ieee80211_
3140         if (baselen > skb->len)
3141                 return;
3142  
3143 -       ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
3144 +       ieee802_11_parse_elems(elements, skb->len - baselen, false, &elems);
3145  
3146         channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
3147  
3148 --- a/net/mac80211/sta_info.h
3149 +++ b/net/mac80211/sta_info.h
3150 @@ -32,7 +32,6 @@
3151   * @WLAN_STA_SHORT_PREAMBLE: Station is capable of receiving short-preamble
3152   *     frames.
3153   * @WLAN_STA_WME: Station is a QoS-STA.
3154 - * @WLAN_STA_WDS: Station is one of our WDS peers.
3155   * @WLAN_STA_CLEAR_PS_FILT: Clear PS filter in hardware (using the
3156   *     IEEE80211_TX_CTL_CLEAR_PS_FILT control flag) when the next
3157   *     frame to this station is transmitted.
3158 @@ -66,7 +65,6 @@ enum ieee80211_sta_info_flags {
3159         WLAN_STA_AUTHORIZED,
3160         WLAN_STA_SHORT_PREAMBLE,
3161         WLAN_STA_WME,
3162 -       WLAN_STA_WDS,
3163         WLAN_STA_CLEAR_PS_FILT,
3164         WLAN_STA_MFP,
3165         WLAN_STA_BLOCK_BA,
3166 @@ -344,6 +342,11 @@ struct sta_info {
3167         int last_signal;
3168         struct ewma avg_signal;
3169         int last_ack_signal;
3170 +
3171 +       u8 chains;
3172 +       s8 chain_signal_last[IEEE80211_MAX_CHAINS];
3173 +       struct ewma chain_signal_avg[IEEE80211_MAX_CHAINS];
3174 +
3175         /* Plus 1 for non-QoS frames */
3176         __le16 last_seq_ctrl[IEEE80211_NUM_TIDS + 1];
3177  
3178 --- a/net/mac80211/trace.h
3179 +++ b/net/mac80211/trace.h
3180 @@ -990,23 +990,23 @@ TRACE_EVENT(drv_channel_switch,
3181  
3182         TP_STRUCT__entry(
3183                 LOCAL_ENTRY
3184 +               CHANDEF_ENTRY
3185                 __field(u64, timestamp)
3186                 __field(bool, block_tx)
3187 -               __field(u16, freq)
3188                 __field(u8, count)
3189         ),
3190  
3191         TP_fast_assign(
3192                 LOCAL_ASSIGN;
3193 +               CHANDEF_ASSIGN(&ch_switch->chandef)
3194                 __entry->timestamp = ch_switch->timestamp;
3195                 __entry->block_tx = ch_switch->block_tx;
3196 -               __entry->freq = ch_switch->channel->center_freq;
3197                 __entry->count = ch_switch->count;
3198         ),
3199  
3200         TP_printk(
3201 -               LOCAL_PR_FMT " new freq:%u count:%d",
3202 -               LOCAL_PR_ARG, __entry->freq, __entry->count
3203 +               LOCAL_PR_FMT " new " CHANDEF_PR_FMT " count:%d",
3204 +               LOCAL_PR_ARG, CHANDEF_PR_ARG, __entry->count
3205         )
3206  );
3207  
3208 --- a/net/mac80211/tx.c
3209 +++ b/net/mac80211/tx.c
3210 @@ -48,15 +48,15 @@ static __le16 ieee80211_duration(struct 
3211         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3212  
3213         /* assume HW handles this */
3214 -       if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
3215 +       if (tx->rate.flags & IEEE80211_TX_RC_MCS)
3216                 return 0;
3217  
3218         /* uh huh? */
3219 -       if (WARN_ON_ONCE(info->control.rates[0].idx < 0))
3220 +       if (WARN_ON_ONCE(tx->rate.idx < 0))
3221                 return 0;
3222  
3223         sband = local->hw.wiphy->bands[info->band];
3224 -       txrate = &sband->bitrates[info->control.rates[0].idx];
3225 +       txrate = &sband->bitrates[tx->rate.idx];
3226  
3227         erp = txrate->flags & IEEE80211_RATE_ERP_G;
3228  
3229 @@ -617,11 +617,9 @@ ieee80211_tx_h_rate_ctrl(struct ieee8021
3230         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
3231         struct ieee80211_hdr *hdr = (void *)tx->skb->data;
3232         struct ieee80211_supported_band *sband;
3233 -       struct ieee80211_rate *rate;
3234 -       int i;
3235         u32 len;
3236 -       bool inval = false, rts = false, short_preamble = false;
3237         struct ieee80211_tx_rate_control txrc;
3238 +       struct ieee80211_sta_rates *ratetbl = NULL;
3239         bool assoc = false;
3240  
3241         memset(&txrc, 0, sizeof(txrc));
3242 @@ -642,18 +640,23 @@ ieee80211_tx_h_rate_ctrl(struct ieee8021
3243                 txrc.max_rate_idx = -1;
3244         else
3245                 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
3246 -       memcpy(txrc.rate_idx_mcs_mask,
3247 -              tx->sdata->rc_rateidx_mcs_mask[info->band],
3248 -              sizeof(txrc.rate_idx_mcs_mask));
3249 +
3250 +       if (tx->sdata->rc_has_mcs_mask[info->band])
3251 +               txrc.rate_idx_mcs_mask =
3252 +                       tx->sdata->rc_rateidx_mcs_mask[info->band];
3253 +
3254         txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
3255                     tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
3256                     tx->sdata->vif.type == NL80211_IFTYPE_ADHOC);
3257  
3258         /* set up RTS protection if desired */
3259         if (len > tx->local->hw.wiphy->rts_threshold) {
3260 -               txrc.rts = rts = true;
3261 +               txrc.rts = true;
3262         }
3263  
3264 +       info->control.use_rts = txrc.rts;
3265 +       info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
3266 +
3267         /*
3268          * Use short preamble if the BSS can handle it, but not for
3269          * management frames unless we know the receiver can handle
3270 @@ -663,7 +666,9 @@ ieee80211_tx_h_rate_ctrl(struct ieee8021
3271         if (tx->sdata->vif.bss_conf.use_short_preamble &&
3272             (ieee80211_is_data(hdr->frame_control) ||
3273              (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
3274 -               txrc.short_preamble = short_preamble = true;
3275 +               txrc.short_preamble = true;
3276 +
3277 +       info->control.short_preamble = txrc.short_preamble;
3278  
3279         if (tx->sta)
3280                 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
3281 @@ -687,16 +692,38 @@ ieee80211_tx_h_rate_ctrl(struct ieee8021
3282          */
3283         rate_control_get_rate(tx->sdata, tx->sta, &txrc);
3284  
3285 -       if (unlikely(info->control.rates[0].idx < 0))
3286 -               return TX_DROP;
3287 +       if (tx->sta && !info->control.skip_table)
3288 +               ratetbl = rcu_dereference(tx->sta->sta.rates);
3289 +
3290 +       if (unlikely(info->control.rates[0].idx < 0)) {
3291 +               if (ratetbl) {
3292 +                       struct ieee80211_tx_rate rate = {
3293 +                               .idx = ratetbl->rate[0].idx,
3294 +                               .flags = ratetbl->rate[0].flags,
3295 +                               .count = ratetbl->rate[0].count
3296 +                       };
3297 +
3298 +                       if (ratetbl->rate[0].idx < 0)
3299 +                               return TX_DROP;
3300 +
3301 +                       tx->rate = rate;
3302 +               } else {
3303 +                       return TX_DROP;
3304 +               }
3305 +       } else {
3306 +               tx->rate = info->control.rates[0];
3307 +       }
3308  
3309         if (txrc.reported_rate.idx < 0) {
3310 -               txrc.reported_rate = info->control.rates[0];
3311 +               txrc.reported_rate = tx->rate;
3312                 if (tx->sta && ieee80211_is_data(hdr->frame_control))
3313                         tx->sta->last_tx_rate = txrc.reported_rate;
3314         } else if (tx->sta)
3315                 tx->sta->last_tx_rate = txrc.reported_rate;
3316  
3317 +       if (ratetbl)
3318 +               return TX_CONTINUE;
3319 +
3320         if (unlikely(!info->control.rates[0].count))
3321                 info->control.rates[0].count = 1;
3322  
3323 @@ -704,91 +731,6 @@ ieee80211_tx_h_rate_ctrl(struct ieee8021
3324                          (info->flags & IEEE80211_TX_CTL_NO_ACK)))
3325                 info->control.rates[0].count = 1;
3326  
3327 -       if (is_multicast_ether_addr(hdr->addr1)) {
3328 -               /*
3329 -                * XXX: verify the rate is in the basic rateset
3330 -                */
3331 -               return TX_CONTINUE;
3332 -       }
3333 -
3334 -       /*
3335 -        * set up the RTS/CTS rate as the fastest basic rate
3336 -        * that is not faster than the data rate
3337 -        *
3338 -        * XXX: Should this check all retry rates?
3339 -        */
3340 -       if (!(info->control.rates[0].flags & IEEE80211_TX_RC_MCS)) {
3341 -               s8 baserate = 0;
3342 -
3343 -               rate = &sband->bitrates[info->control.rates[0].idx];
3344 -
3345 -               for (i = 0; i < sband->n_bitrates; i++) {
3346 -                       /* must be a basic rate */
3347 -                       if (!(tx->sdata->vif.bss_conf.basic_rates & BIT(i)))
3348 -                               continue;
3349 -                       /* must not be faster than the data rate */
3350 -                       if (sband->bitrates[i].bitrate > rate->bitrate)
3351 -                               continue;
3352 -                       /* maximum */
3353 -                       if (sband->bitrates[baserate].bitrate <
3354 -                            sband->bitrates[i].bitrate)
3355 -                               baserate = i;
3356 -               }
3357 -
3358 -               info->control.rts_cts_rate_idx = baserate;
3359 -       }
3360 -
3361 -       for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
3362 -               /*
3363 -                * make sure there's no valid rate following
3364 -                * an invalid one, just in case drivers don't
3365 -                * take the API seriously to stop at -1.
3366 -                */
3367 -               if (inval) {
3368 -                       info->control.rates[i].idx = -1;
3369 -                       continue;
3370 -               }
3371 -               if (info->control.rates[i].idx < 0) {
3372 -                       inval = true;
3373 -                       continue;
3374 -               }
3375 -
3376 -               /*
3377 -                * For now assume MCS is already set up correctly, this
3378 -                * needs to be fixed.
3379 -                */
3380 -               if (info->control.rates[i].flags & IEEE80211_TX_RC_MCS) {
3381 -                       WARN_ON(info->control.rates[i].idx > 76);
3382 -                       continue;
3383 -               }
3384 -
3385 -               /* set up RTS protection if desired */
3386 -               if (rts)
3387 -                       info->control.rates[i].flags |=
3388 -                               IEEE80211_TX_RC_USE_RTS_CTS;
3389 -
3390 -               /* RC is busted */
3391 -               if (WARN_ON_ONCE(info->control.rates[i].idx >=
3392 -                                sband->n_bitrates)) {
3393 -                       info->control.rates[i].idx = -1;
3394 -                       continue;
3395 -               }
3396 -
3397 -               rate = &sband->bitrates[info->control.rates[i].idx];
3398 -
3399 -               /* set up short preamble */
3400 -               if (short_preamble &&
3401 -                   rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
3402 -                       info->control.rates[i].flags |=
3403 -                               IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
3404 -
3405 -               /* set up G protection */
3406 -               if (!rts && tx->sdata->vif.bss_conf.use_cts_prot &&
3407 -                   rate->flags & IEEE80211_RATE_ERP_G)
3408 -                       info->control.rates[i].flags |=
3409 -                               IEEE80211_TX_RC_USE_CTS_PROTECT;
3410 -       }
3411 -
3412         return TX_CONTINUE;
3413  }
3414  
3415 @@ -2508,8 +2450,6 @@ struct sk_buff *ieee80211_beacon_get_tim
3416                 txrc.max_rate_idx = -1;
3417         else
3418                 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
3419 -       memcpy(txrc.rate_idx_mcs_mask, sdata->rc_rateidx_mcs_mask[band],
3420 -              sizeof(txrc.rate_idx_mcs_mask));
3421         txrc.bss = true;
3422         rate_control_get_rate(sdata, NULL, &txrc);
3423  
3424 --- a/net/mac80211/util.c
3425 +++ b/net/mac80211/util.c
3426 @@ -485,7 +485,8 @@ int ieee80211_queue_stopped(struct ieee8
3427                 return true;
3428  
3429         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
3430 -       ret = !!local->queue_stop_reasons[queue];
3431 +       ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
3432 +                      &local->queue_stop_reasons[queue]);
3433         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
3434         return ret;
3435  }
3436 @@ -660,7 +661,7 @@ void ieee80211_queue_delayed_work(struct
3437  }
3438  EXPORT_SYMBOL(ieee80211_queue_delayed_work);
3439  
3440 -u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
3441 +u32 ieee802_11_parse_elems_crc(u8 *start, size_t len, bool action,
3442                                struct ieee802_11_elems *elems,
3443                                u64 filter, u32 crc)
3444  {
3445 @@ -668,6 +669,7 @@ u32 ieee802_11_parse_elems_crc(u8 *start
3446         u8 *pos = start;
3447         bool calc_crc = filter != 0;
3448         DECLARE_BITMAP(seen_elems, 256);
3449 +       const u8 *ie;
3450  
3451         bitmap_zero(seen_elems, 256);
3452         memset(elems, 0, sizeof(*elems));
3453 @@ -715,6 +717,12 @@ u32 ieee802_11_parse_elems_crc(u8 *start
3454                 case WLAN_EID_COUNTRY:
3455                 case WLAN_EID_PWR_CONSTRAINT:
3456                 case WLAN_EID_TIMEOUT_INTERVAL:
3457 +               case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
3458 +               case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
3459 +               /*
3460 +                * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
3461 +                * that if the content gets bigger it might be needed more than once
3462 +                */
3463                         if (test_bit(id, seen_elems)) {
3464                                 elems->parse_error = true;
3465                                 left -= elen;
3466 @@ -862,6 +870,48 @@ u32 ieee802_11_parse_elems_crc(u8 *start
3467                         }
3468                         elems->ch_switch_ie = (void *)pos;
3469                         break;
3470 +               case WLAN_EID_EXT_CHANSWITCH_ANN:
3471 +                       if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
3472 +                               elem_parse_failed = true;
3473 +                               break;
3474 +                       }
3475 +                       elems->ext_chansw_ie = (void *)pos;
3476 +                       break;
3477 +               case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
3478 +                       if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
3479 +                               elem_parse_failed = true;
3480 +                               break;
3481 +                       }
3482 +                       elems->sec_chan_offs = (void *)pos;
3483 +                       break;
3484 +               case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
3485 +                       if (!action ||
3486 +                           elen != sizeof(*elems->wide_bw_chansw_ie)) {
3487 +                               elem_parse_failed = true;
3488 +                               break;
3489 +                       }
3490 +                       elems->wide_bw_chansw_ie = (void *)pos;
3491 +                       break;
3492 +               case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
3493 +                       if (action) {
3494 +                               elem_parse_failed = true;
3495 +                               break;
3496 +                       }
3497 +                       /*
3498 +                        * This is a bit tricky, but as we only care about
3499 +                        * the wide bandwidth channel switch element, so
3500 +                        * just parse it out manually.
3501 +                        */
3502 +                       ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
3503 +                                             pos, elen);
3504 +                       if (ie) {
3505 +                               if (ie[1] == sizeof(*elems->wide_bw_chansw_ie))
3506 +                                       elems->wide_bw_chansw_ie =
3507 +                                               (void *)(ie + 2);
3508 +                               else
3509 +                                       elem_parse_failed = true;
3510 +                       }
3511 +                       break;
3512                 case WLAN_EID_COUNTRY:
3513                         elems->country_elem = pos;
3514                         elems->country_elem_len = elen;
3515 --- a/net/wireless/reg.c
3516 +++ b/net/wireless/reg.c
3517 @@ -857,7 +857,7 @@ static void handle_channel(struct wiphy 
3518                         return;
3519  
3520                 REG_DBG_PRINT("Disabling freq %d MHz\n", chan->center_freq);
3521 -               chan->flags = IEEE80211_CHAN_DISABLED;
3522 +               chan->flags |= IEEE80211_CHAN_DISABLED;
3523                 return;
3524         }
3525  
3526 --- a/net/wireless/util.c
3527 +++ b/net/wireless/util.c
3528 @@ -1156,6 +1156,26 @@ int cfg80211_get_p2p_attr(const u8 *ies,
3529  }
3530  EXPORT_SYMBOL(cfg80211_get_p2p_attr);
3531  
3532 +bool ieee80211_operating_class_to_band(u8 operating_class,
3533 +                                      enum ieee80211_band *band)
3534 +{
3535 +       switch (operating_class) {
3536 +       case 112:
3537 +       case 115 ... 127:
3538 +               *band = IEEE80211_BAND_5GHZ;
3539 +               return true;
3540 +       case 81:
3541 +       case 82:
3542 +       case 83:
3543 +       case 84:
3544 +               *band = IEEE80211_BAND_2GHZ;
3545 +               return true;
3546 +       }
3547 +
3548 +       return false;
3549 +}
3550 +EXPORT_SYMBOL(ieee80211_operating_class_to_band);
3551 +
3552  int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
3553                                  u32 beacon_int)
3554  {
3555 --- a/include/uapi/linux/nl80211.h
3556 +++ b/include/uapi/linux/nl80211.h
3557 @@ -1973,6 +1973,10 @@ enum nl80211_sta_bss_param {
3558   * @NL80211_STA_INFO_PEER_PM: peer mesh STA link-specific power mode
3559   * @NL80211_STA_INFO_NONPEER_PM: neighbor mesh STA power save mode towards
3560   *     non-peer STA
3561 + * @NL80211_STA_INFO_CHAIN_SIGNAL: per-chain signal strength of last PPDU
3562 + *     Contains a nested array of signal strength attributes (u8, dBm)
3563 + * @NL80211_STA_INFO_CHAIN_SIGNAL_AVG: per-chain signal strength average
3564 + *     Same format as NL80211_STA_INFO_CHAIN_SIGNAL.
3565   * @__NL80211_STA_INFO_AFTER_LAST: internal
3566   * @NL80211_STA_INFO_MAX: highest possible station info attribute
3567   */
3568 @@ -2002,6 +2006,8 @@ enum nl80211_sta_info {
3569         NL80211_STA_INFO_NONPEER_PM,
3570         NL80211_STA_INFO_RX_BYTES64,
3571         NL80211_STA_INFO_TX_BYTES64,
3572 +       NL80211_STA_INFO_CHAIN_SIGNAL,
3573 +       NL80211_STA_INFO_CHAIN_SIGNAL_AVG,
3574  
3575         /* keep last */
3576         __NL80211_STA_INFO_AFTER_LAST,
3577 --- a/net/mac80211/sta_info.c
3578 +++ b/net/mac80211/sta_info.c
3579 @@ -358,6 +358,8 @@ struct sta_info *sta_info_alloc(struct i
3580         do_posix_clock_monotonic_gettime(&uptime);
3581         sta->last_connected = uptime.tv_sec;
3582         ewma_init(&sta->avg_signal, 1024, 8);
3583 +       for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
3584 +               ewma_init(&sta->chain_signal_avg[i], 1024, 8);
3585  
3586         if (sta_prepare_rate_control(local, sta, gfp)) {
3587                 kfree(sta);
3588 --- a/net/wireless/nl80211.c
3589 +++ b/net/wireless/nl80211.c
3590 @@ -3367,6 +3367,32 @@ static bool nl80211_put_sta_rate(struct 
3591         return true;
3592  }
3593  
3594 +static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3595 +                              int id)
3596 +{
3597 +       void *attr;
3598 +       int i = 0;
3599 +
3600 +       if (!mask)
3601 +               return true;
3602 +
3603 +       attr = nla_nest_start(msg, id);
3604 +       if (!attr)
3605 +               return false;
3606 +
3607 +       for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3608 +               if (!(mask & BIT(i)))
3609 +                       continue;
3610 +
3611 +               if (nla_put_u8(msg, i, signal[i]))
3612 +                       return false;
3613 +       }
3614 +
3615 +       nla_nest_end(msg, attr);
3616 +
3617 +       return true;
3618 +}
3619 +
3620  static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
3621                                 int flags,
3622                                 struct cfg80211_registered_device *rdev,
3623 @@ -3438,6 +3464,18 @@ static int nl80211_send_station(struct s
3624         default:
3625                 break;
3626         }
3627 +       if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3628 +               if (!nl80211_put_signal(msg, sinfo->chains,
3629 +                                       sinfo->chain_signal,
3630 +                                       NL80211_STA_INFO_CHAIN_SIGNAL))
3631 +                       goto nla_put_failure;
3632 +       }
3633 +       if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3634 +               if (!nl80211_put_signal(msg, sinfo->chains,
3635 +                                       sinfo->chain_signal_avg,
3636 +                                       NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3637 +                       goto nla_put_failure;
3638 +       }
3639         if (sinfo->filled & STATION_INFO_TX_BITRATE) {
3640                 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3641                                           NL80211_STA_INFO_TX_BITRATE))
3642 --- a/drivers/net/wireless/ath/ath9k/init.c
3643 +++ b/drivers/net/wireless/ath/ath9k/init.c
3644 @@ -768,7 +768,8 @@ void ath9k_set_hw_capab(struct ath_softc
3645                 IEEE80211_HW_SUPPORTS_PS |
3646                 IEEE80211_HW_PS_NULLFUNC_STACK |
3647                 IEEE80211_HW_SPECTRUM_MGMT |
3648 -               IEEE80211_HW_REPORTS_TX_ACK_STATUS;
3649 +               IEEE80211_HW_REPORTS_TX_ACK_STATUS |
3650 +               IEEE80211_HW_SUPPORTS_RC_TABLE;
3651  
3652         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_HT)
3653                  hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
3654 --- a/drivers/net/wireless/ath/ath9k/xmit.c
3655 +++ b/drivers/net/wireless/ath/ath9k/xmit.c
3656 @@ -157,6 +157,13 @@ static void ath_send_bar(struct ath_atx_
3657                            seqno << IEEE80211_SEQ_SEQ_SHIFT);
3658  }
3659  
3660 +static void ath_set_rates(struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3661 +                         struct ath_buf *bf)
3662 +{
3663 +       ieee80211_get_tx_rates(vif, sta, bf->bf_mpdu, bf->rates,
3664 +                              ARRAY_SIZE(bf->rates));
3665 +}
3666 +
3667  static void ath_tx_flush_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
3668  {
3669         struct ath_txq *txq = tid->ac->txq;
3670 @@ -189,6 +196,7 @@ static void ath_tx_flush_tid(struct ath_
3671                         ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, 0);
3672                         sendbar = true;
3673                 } else {
3674 +                       ath_set_rates(tid->an->vif, tid->an->sta, bf);
3675                         ath_tx_send_normal(sc, txq, NULL, skb);
3676                 }
3677         }
3678 @@ -407,7 +415,7 @@ static void ath_tx_complete_aggr(struct 
3679  
3680         tx_info = IEEE80211_SKB_CB(skb);
3681  
3682 -       memcpy(rates, tx_info->control.rates, sizeof(rates));
3683 +       memcpy(rates, bf->rates, sizeof(rates));
3684  
3685         retries = ts->ts_longretry + 1;
3686         for (i = 0; i < ts->ts_rateindex; i++)
3687 @@ -736,8 +744,6 @@ static int ath_compute_num_delims(struct
3688                                   bool first_subfrm)
3689  {
3690  #define FIRST_DESC_NDELIMS 60
3691 -       struct sk_buff *skb = bf->bf_mpdu;
3692 -       struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
3693         u32 nsymbits, nsymbols;
3694         u16 minlen;
3695         u8 flags, rix;
3696 @@ -778,8 +784,8 @@ static int ath_compute_num_delims(struct
3697         if (tid->an->mpdudensity == 0)
3698                 return ndelim;
3699  
3700 -       rix = tx_info->control.rates[0].idx;
3701 -       flags = tx_info->control.rates[0].flags;
3702 +       rix = bf->rates[0].idx;
3703 +       flags = bf->rates[0].flags;
3704         width = (flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ? 1 : 0;
3705         half_gi = (flags & IEEE80211_TX_RC_SHORT_GI) ? 1 : 0;
3706  
3707 @@ -858,6 +864,7 @@ static enum ATH_AGGR_STATUS ath_tx_form_
3708                         bf_first = bf;
3709  
3710                 if (!rl) {
3711 +                       ath_set_rates(tid->an->vif, tid->an->sta, bf);
3712                         aggr_limit = ath_lookup_rate(sc, bf, tid);
3713                         rl = 1;
3714                 }
3715 @@ -998,14 +1005,14 @@ static void ath_buf_set_rate(struct ath_
3716  
3717         skb = bf->bf_mpdu;
3718         tx_info = IEEE80211_SKB_CB(skb);
3719 -       rates = tx_info->control.rates;
3720 +       rates = bf->rates;
3721         hdr = (struct ieee80211_hdr *)skb->data;
3722  
3723         /* set dur_update_en for l-sig computation except for PS-Poll frames */
3724         info->dur_update = !ieee80211_is_pspoll(hdr->frame_control);
3725         info->rtscts_rate = fi->rtscts_rate;
3726  
3727 -       for (i = 0; i < 4; i++) {
3728 +       for (i = 0; i < ARRAY_SIZE(bf->rates); i++) {
3729                 bool is_40, is_sgi, is_sp;
3730                 int phy;
3731  
3732 @@ -1743,6 +1750,7 @@ static void ath_tx_send_ampdu(struct ath
3733                 return;
3734         }
3735  
3736 +       ath_set_rates(tid->an->vif, tid->an->sta, bf);
3737         bf->bf_state.bf_type = BUF_AMPDU;
3738         INIT_LIST_HEAD(&bf_head);
3739         list_add(&bf->list, &bf_head);
3740 @@ -1892,49 +1900,6 @@ static struct ath_buf *ath_tx_setup_buff
3741         return bf;
3742  }
3743  
3744 -/* FIXME: tx power */
3745 -static void ath_tx_start_dma(struct ath_softc *sc, struct sk_buff *skb,
3746 -                            struct ath_tx_control *txctl)
3747 -{
3748 -       struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
3749 -       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3750 -       struct ath_atx_tid *tid = NULL;
3751 -       struct ath_buf *bf;
3752 -       u8 tidno;
3753 -
3754 -       if (txctl->an && ieee80211_is_data_qos(hdr->frame_control)) {
3755 -               tidno = ieee80211_get_qos_ctl(hdr)[0] &
3756 -                       IEEE80211_QOS_CTL_TID_MASK;
3757 -               tid = ATH_AN_2_TID(txctl->an, tidno);
3758 -
3759 -               WARN_ON(tid->ac->txq != txctl->txq);
3760 -       }
3761 -
3762 -       if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) && tid) {
3763 -               /*
3764 -                * Try aggregation if it's a unicast data frame
3765 -                * and the destination is HT capable.
3766 -                */
3767 -               ath_tx_send_ampdu(sc, tid, skb, txctl);
3768 -       } else {
3769 -               bf = ath_tx_setup_buffer(sc, txctl->txq, tid, skb);
3770 -               if (!bf) {
3771 -                       if (txctl->paprd)
3772 -                               dev_kfree_skb_any(skb);
3773 -                       else
3774 -                               ieee80211_free_txskb(sc->hw, skb);
3775 -                       return;
3776 -               }
3777 -
3778 -               bf->bf_state.bfs_paprd = txctl->paprd;
3779 -
3780 -               if (txctl->paprd)
3781 -                       bf->bf_state.bfs_paprd_timestamp = jiffies;
3782 -
3783 -               ath_tx_send_normal(sc, txctl->txq, tid, skb);
3784 -       }
3785 -}
3786 -
3787  /* Upon failure caller should free skb */
3788  int ath_tx_start(struct ieee80211_hw *hw, struct sk_buff *skb,
3789                  struct ath_tx_control *txctl)
3790 @@ -1945,8 +1910,11 @@ int ath_tx_start(struct ieee80211_hw *hw
3791         struct ieee80211_vif *vif = info->control.vif;
3792         struct ath_softc *sc = hw->priv;
3793         struct ath_txq *txq = txctl->txq;
3794 +       struct ath_atx_tid *tid = NULL;
3795 +       struct ath_buf *bf;
3796         int padpos, padsize;
3797         int frmlen = skb->len + FCS_LEN;
3798 +       u8 tidno;
3799         int q;
3800  
3801         /* NOTE:  sta can be NULL according to net/mac80211.h */
3802 @@ -2002,8 +1970,41 @@ int ath_tx_start(struct ieee80211_hw *hw
3803                 txq->stopped = true;
3804         }
3805  
3806 -       ath_tx_start_dma(sc, skb, txctl);
3807 +       if (txctl->an && ieee80211_is_data_qos(hdr->frame_control)) {
3808 +               tidno = ieee80211_get_qos_ctl(hdr)[0] &
3809 +                       IEEE80211_QOS_CTL_TID_MASK;
3810 +               tid = ATH_AN_2_TID(txctl->an, tidno);
3811 +
3812 +               WARN_ON(tid->ac->txq != txctl->txq);
3813 +       }
3814 +
3815 +       if ((info->flags & IEEE80211_TX_CTL_AMPDU) && tid) {
3816 +               /*
3817 +                * Try aggregation if it's a unicast data frame
3818 +                * and the destination is HT capable.
3819 +                */
3820 +               ath_tx_send_ampdu(sc, tid, skb, txctl);
3821 +               goto out;
3822 +       }
3823 +
3824 +       bf = ath_tx_setup_buffer(sc, txctl->txq, tid, skb);
3825 +       if (!bf) {
3826 +               if (txctl->paprd)
3827 +                       dev_kfree_skb_any(skb);
3828 +               else
3829 +                       ieee80211_free_txskb(sc->hw, skb);
3830 +               goto out;
3831 +       }
3832 +
3833 +       bf->bf_state.bfs_paprd = txctl->paprd;
3834 +
3835 +       if (txctl->paprd)
3836 +               bf->bf_state.bfs_paprd_timestamp = jiffies;
3837 +
3838 +       ath_set_rates(vif, sta, bf);
3839 +       ath_tx_send_normal(sc, txctl->txq, tid, skb);
3840  
3841 +out:
3842         ath_txq_unlock(sc, txq);
3843  
3844         return 0;
3845 --- a/drivers/net/wireless/ath/ath9k/recv.c
3846 +++ b/drivers/net/wireless/ath/ath9k/recv.c
3847 @@ -124,7 +124,7 @@ static bool ath_rx_edma_buf_link(struct 
3848  
3849         SKB_CB_ATHBUF(skb) = bf;
3850         ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
3851 -       skb_queue_tail(&rx_edma->rx_fifo, skb);
3852 +       __skb_queue_tail(&rx_edma->rx_fifo, skb);
3853  
3854         return true;
3855  }
3856 @@ -155,7 +155,7 @@ static void ath_rx_remove_buffer(struct 
3857  
3858         rx_edma = &sc->rx.rx_edma[qtype];
3859  
3860 -       while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
3861 +       while ((skb = __skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
3862                 bf = SKB_CB_ATHBUF(skb);
3863                 BUG_ON(!bf);
3864                 list_add_tail(&bf->list, &sc->rx.rxbuf);
3865 @@ -1287,13 +1287,13 @@ int ath_rx_tasklet(struct ath_softc *sc,
3866                         goto requeue_drop_frag;
3867                 }
3868  
3869 -               bf->bf_mpdu = requeue_skb;
3870 -               bf->bf_buf_addr = new_buf_addr;
3871 -
3872                 /* Unmap the frame */
3873                 dma_unmap_single(sc->dev, bf->bf_buf_addr,
3874                                  common->rx_bufsize, dma_type);
3875  
3876 +               bf->bf_mpdu = requeue_skb;
3877 +               bf->bf_buf_addr = new_buf_addr;
3878 +
3879                 skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
3880                 if (ah->caps.rx_status_len)
3881                         skb_pull(skb, ah->caps.rx_status_len);
3882 --- a/net/wireless/core.c
3883 +++ b/net/wireless/core.c
3884 @@ -885,7 +885,6 @@ void cfg80211_leave(struct cfg80211_regi
3885  #endif
3886                 __cfg80211_disconnect(rdev, dev,
3887                                       WLAN_REASON_DEAUTH_LEAVING, true);
3888 -               cfg80211_mlme_down(rdev, dev);
3889                 wdev_unlock(wdev);
3890                 break;
3891         case NL80211_IFTYPE_MESH_POINT:
3892 --- a/net/wireless/sme.c
3893 +++ b/net/wireless/sme.c
3894 @@ -961,7 +961,7 @@ int __cfg80211_disconnect(struct cfg8021
3895                 /* was it connected by userspace SME? */
3896                 if (!wdev->conn) {
3897                         cfg80211_mlme_down(rdev, dev);
3898 -                       return 0;
3899 +                       goto disconnect;
3900                 }
3901  
3902                 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
3903 @@ -987,6 +987,7 @@ int __cfg80211_disconnect(struct cfg8021
3904                         return err;
3905         }
3906  
3907 + disconnect:
3908         if (wdev->sme_state == CFG80211_SME_CONNECTED)
3909                 __cfg80211_disconnected(dev, NULL, 0, 0, false);
3910         else if (wdev->sme_state == CFG80211_SME_CONNECTING)