mac80211: merge a client mode channel switch handling fix
[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 --- a/net/mac80211/pm.c
1925 +++ b/net/mac80211/pm.c
1926 @@ -38,8 +38,8 @@ int __ieee80211_suspend(struct ieee80211
1927                                         IEEE80211_MAX_QUEUE_MAP,
1928                                         IEEE80211_QUEUE_STOP_REASON_SUSPEND);
1929  
1930 -       /* flush out all packets */
1931 -       synchronize_net();
1932 +       /* flush out all packets and station cleanup call_rcu()s */
1933 +       rcu_barrier();
1934  
1935         ieee80211_flush_queues(local, NULL);
1936  
1937 --- a/net/mac80211/rate.c
1938 +++ b/net/mac80211/rate.c
1939 @@ -252,6 +252,25 @@ rate_lowest_non_cck_index(struct ieee802
1940         return 0;
1941  }
1942  
1943 +static void __rate_control_send_low(struct ieee80211_hw *hw,
1944 +                                   struct ieee80211_supported_band *sband,
1945 +                                   struct ieee80211_sta *sta,
1946 +                                   struct ieee80211_tx_info *info)
1947 +{
1948 +       if ((sband->band != IEEE80211_BAND_2GHZ) ||
1949 +           !(info->flags & IEEE80211_TX_CTL_NO_CCK_RATE))
1950 +               info->control.rates[0].idx = rate_lowest_index(sband, sta);
1951 +       else
1952 +               info->control.rates[0].idx =
1953 +                       rate_lowest_non_cck_index(sband, sta);
1954 +
1955 +       info->control.rates[0].count =
1956 +               (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
1957 +               1 : hw->max_rate_tries;
1958 +
1959 +       info->control.skip_table = 1;
1960 +}
1961 +
1962  
1963  bool rate_control_send_low(struct ieee80211_sta *sta,
1964                            void *priv_sta,
1965 @@ -262,16 +281,8 @@ bool rate_control_send_low(struct ieee80
1966         int mcast_rate;
1967  
1968         if (!sta || !priv_sta || rc_no_data_or_no_ack_use_min(txrc)) {
1969 -               if ((sband->band != IEEE80211_BAND_2GHZ) ||
1970 -                   !(info->flags & IEEE80211_TX_CTL_NO_CCK_RATE))
1971 -                       info->control.rates[0].idx =
1972 -                               rate_lowest_index(txrc->sband, sta);
1973 -               else
1974 -                       info->control.rates[0].idx =
1975 -                               rate_lowest_non_cck_index(txrc->sband, sta);
1976 -               info->control.rates[0].count =
1977 -                       (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
1978 -                       1 : txrc->hw->max_rate_tries;
1979 +               __rate_control_send_low(txrc->hw, sband, sta, info);
1980 +
1981                 if (!sta && txrc->bss) {
1982                         mcast_rate = txrc->bss_conf->mcast_rate[sband->band];
1983                         if (mcast_rate > 0) {
1984 @@ -355,7 +366,8 @@ static bool rate_idx_match_mcs_mask(stru
1985  
1986  
1987  static void rate_idx_match_mask(struct ieee80211_tx_rate *rate,
1988 -                               struct ieee80211_tx_rate_control *txrc,
1989 +                               struct ieee80211_supported_band *sband,
1990 +                               enum nl80211_chan_width chan_width,
1991                                 u32 mask,
1992                                 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
1993  {
1994 @@ -375,27 +387,17 @@ static void rate_idx_match_mask(struct i
1995                                   IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
1996                 alt_rate.count = rate->count;
1997                 if (rate_idx_match_legacy_mask(&alt_rate,
1998 -                                              txrc->sband->n_bitrates,
1999 -                                              mask)) {
2000 +                                              sband->n_bitrates, mask)) {
2001                         *rate = alt_rate;
2002                         return;
2003                 }
2004         } else {
2005 -               struct sk_buff *skb = txrc->skb;
2006 -               struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2007 -               __le16 fc;
2008 -
2009                 /* handle legacy rates */
2010 -               if (rate_idx_match_legacy_mask(rate, txrc->sband->n_bitrates,
2011 -                                              mask))
2012 +               if (rate_idx_match_legacy_mask(rate, sband->n_bitrates, mask))
2013                         return;
2014  
2015                 /* if HT BSS, and we handle a data frame, also try HT rates */
2016 -               if (txrc->bss_conf->chandef.width == NL80211_CHAN_WIDTH_20_NOHT)
2017 -                       return;
2018 -
2019 -               fc = hdr->frame_control;
2020 -               if (!ieee80211_is_data(fc))
2021 +               if (chan_width == NL80211_CHAN_WIDTH_20_NOHT)
2022                         return;
2023  
2024                 alt_rate.idx = 0;
2025 @@ -408,7 +410,7 @@ static void rate_idx_match_mask(struct i
2026  
2027                 alt_rate.flags |= IEEE80211_TX_RC_MCS;
2028  
2029 -               if (txrc->bss_conf->chandef.width == NL80211_CHAN_WIDTH_40)
2030 +               if (chan_width == NL80211_CHAN_WIDTH_40)
2031                         alt_rate.flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2032  
2033                 if (rate_idx_match_mcs_mask(&alt_rate, mcs_mask)) {
2034 @@ -426,6 +428,228 @@ static void rate_idx_match_mask(struct i
2035          */
2036  }
2037  
2038 +static void rate_fixup_ratelist(struct ieee80211_vif *vif,
2039 +                               struct ieee80211_supported_band *sband,
2040 +                               struct ieee80211_tx_info *info,
2041 +                               struct ieee80211_tx_rate *rates,
2042 +                               int max_rates)
2043 +{
2044 +       struct ieee80211_rate *rate;
2045 +       bool inval = false;
2046 +       int i;
2047 +
2048 +       /*
2049 +        * Set up the RTS/CTS rate as the fastest basic rate
2050 +        * that is not faster than the data rate unless there
2051 +        * is no basic rate slower than the data rate, in which
2052 +        * case we pick the slowest basic rate
2053 +        *
2054 +        * XXX: Should this check all retry rates?
2055 +        */
2056 +       if (!(rates[0].flags & IEEE80211_TX_RC_MCS)) {
2057 +               u32 basic_rates = vif->bss_conf.basic_rates;
2058 +               s8 baserate = basic_rates ? ffs(basic_rates - 1) : 0;
2059 +
2060 +               rate = &sband->bitrates[rates[0].idx];
2061 +
2062 +               for (i = 0; i < sband->n_bitrates; i++) {
2063 +                       /* must be a basic rate */
2064 +                       if (!(basic_rates & BIT(i)))
2065 +                               continue;
2066 +                       /* must not be faster than the data rate */
2067 +                       if (sband->bitrates[i].bitrate > rate->bitrate)
2068 +                               continue;
2069 +                       /* maximum */
2070 +                       if (sband->bitrates[baserate].bitrate <
2071 +                            sband->bitrates[i].bitrate)
2072 +                               baserate = i;
2073 +               }
2074 +
2075 +               info->control.rts_cts_rate_idx = baserate;
2076 +       }
2077 +
2078 +       for (i = 0; i < max_rates; i++) {
2079 +               /*
2080 +                * make sure there's no valid rate following
2081 +                * an invalid one, just in case drivers don't
2082 +                * take the API seriously to stop at -1.
2083 +                */
2084 +               if (inval) {
2085 +                       rates[i].idx = -1;
2086 +                       continue;
2087 +               }
2088 +               if (rates[i].idx < 0) {
2089 +                       inval = true;
2090 +                       continue;
2091 +               }
2092 +
2093 +               /*
2094 +                * For now assume MCS is already set up correctly, this
2095 +                * needs to be fixed.
2096 +                */
2097 +               if (rates[i].flags & IEEE80211_TX_RC_MCS) {
2098 +                       WARN_ON(rates[i].idx > 76);
2099 +
2100 +                       if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
2101 +                           info->control.use_cts_prot)
2102 +                               rates[i].flags |=
2103 +                                       IEEE80211_TX_RC_USE_CTS_PROTECT;
2104 +                       continue;
2105 +               }
2106 +
2107 +               if (rates[i].flags & IEEE80211_TX_RC_VHT_MCS) {
2108 +                       WARN_ON(ieee80211_rate_get_vht_mcs(&rates[i]) > 9);
2109 +                       continue;
2110 +               }
2111 +
2112 +               /* set up RTS protection if desired */
2113 +               if (info->control.use_rts) {
2114 +                       rates[i].flags |= IEEE80211_TX_RC_USE_RTS_CTS;
2115 +                       info->control.use_cts_prot = false;
2116 +               }
2117 +
2118 +               /* RC is busted */
2119 +               if (WARN_ON_ONCE(rates[i].idx >= sband->n_bitrates)) {
2120 +                       rates[i].idx = -1;
2121 +                       continue;
2122 +               }
2123 +
2124 +               rate = &sband->bitrates[rates[i].idx];
2125 +
2126 +               /* set up short preamble */
2127 +               if (info->control.short_preamble &&
2128 +                   rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
2129 +                       rates[i].flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
2130 +
2131 +               /* set up G protection */
2132 +               if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
2133 +                   info->control.use_cts_prot &&
2134 +                   rate->flags & IEEE80211_RATE_ERP_G)
2135 +                       rates[i].flags |= IEEE80211_TX_RC_USE_CTS_PROTECT;
2136 +       }
2137 +}
2138 +
2139 +
2140 +static void rate_control_fill_sta_table(struct ieee80211_sta *sta,
2141 +                                       struct ieee80211_tx_info *info,
2142 +                                       struct ieee80211_tx_rate *rates,
2143 +                                       int max_rates)
2144 +{
2145 +       struct ieee80211_sta_rates *ratetbl = NULL;
2146 +       int i;
2147 +
2148 +       if (sta && !info->control.skip_table)
2149 +               ratetbl = rcu_dereference(sta->rates);
2150 +
2151 +       /* Fill remaining rate slots with data from the sta rate table. */
2152 +       max_rates = min_t(int, max_rates, IEEE80211_TX_RATE_TABLE_SIZE);
2153 +       for (i = 0; i < max_rates; i++) {
2154 +               if (i < ARRAY_SIZE(info->control.rates) &&
2155 +                   info->control.rates[i].idx >= 0 &&
2156 +                   info->control.rates[i].count) {
2157 +                       if (rates != info->control.rates)
2158 +                               rates[i] = info->control.rates[i];
2159 +               } else if (ratetbl) {
2160 +                       rates[i].idx = ratetbl->rate[i].idx;
2161 +                       rates[i].flags = ratetbl->rate[i].flags;
2162 +                       if (info->control.use_rts)
2163 +                               rates[i].count = ratetbl->rate[i].count_rts;
2164 +                       else if (info->control.use_cts_prot)
2165 +                               rates[i].count = ratetbl->rate[i].count_cts;
2166 +                       else
2167 +                               rates[i].count = ratetbl->rate[i].count;
2168 +               } else {
2169 +                       rates[i].idx = -1;
2170 +                       rates[i].count = 0;
2171 +               }
2172 +
2173 +               if (rates[i].idx < 0 || !rates[i].count)
2174 +                       break;
2175 +       }
2176 +}
2177 +
2178 +static void rate_control_apply_mask(struct ieee80211_sub_if_data *sdata,
2179 +                                   struct ieee80211_sta *sta,
2180 +                                   struct ieee80211_supported_band *sband,
2181 +                                   struct ieee80211_tx_info *info,
2182 +                                   struct ieee80211_tx_rate *rates,
2183 +                                   int max_rates)
2184 +{
2185 +       enum nl80211_chan_width chan_width;
2186 +       u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
2187 +       bool has_mcs_mask;
2188 +       u32 mask;
2189 +       int i;
2190 +
2191 +       /*
2192 +        * Try to enforce the rateidx mask the user wanted. skip this if the
2193 +        * default mask (allow all rates) is used to save some processing for
2194 +        * the common case.
2195 +        */
2196 +       mask = sdata->rc_rateidx_mask[info->band];
2197 +       has_mcs_mask = sdata->rc_has_mcs_mask[info->band];
2198 +       if (mask == (1 << sband->n_bitrates) - 1 && !has_mcs_mask)
2199 +               return;
2200 +
2201 +       if (has_mcs_mask)
2202 +               memcpy(mcs_mask, sdata->rc_rateidx_mcs_mask[info->band],
2203 +                      sizeof(mcs_mask));
2204 +       else
2205 +               memset(mcs_mask, 0xff, sizeof(mcs_mask));
2206 +
2207 +       if (sta) {
2208 +               /* Filter out rates that the STA does not support */
2209 +               mask &= sta->supp_rates[info->band];
2210 +               for (i = 0; i < sizeof(mcs_mask); i++)
2211 +                       mcs_mask[i] &= sta->ht_cap.mcs.rx_mask[i];
2212 +       }
2213 +
2214 +       /*
2215 +        * Make sure the rate index selected for each TX rate is
2216 +        * included in the configured mask and change the rate indexes
2217 +        * if needed.
2218 +        */
2219 +       chan_width = sdata->vif.bss_conf.chandef.width;
2220 +       for (i = 0; i < max_rates; i++) {
2221 +               /* Skip invalid rates */
2222 +               if (rates[i].idx < 0)
2223 +                       break;
2224 +
2225 +               rate_idx_match_mask(&rates[i], sband, mask, chan_width,
2226 +                                   mcs_mask);
2227 +       }
2228 +}
2229 +
2230 +void ieee80211_get_tx_rates(struct ieee80211_vif *vif,
2231 +                           struct ieee80211_sta *sta,
2232 +                           struct sk_buff *skb,
2233 +                           struct ieee80211_tx_rate *dest,
2234 +                           int max_rates)
2235 +{
2236 +       struct ieee80211_sub_if_data *sdata;
2237 +       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2238 +       struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2239 +       struct ieee80211_supported_band *sband;
2240 +
2241 +       rate_control_fill_sta_table(sta, info, dest, max_rates);
2242 +
2243 +       if (!vif)
2244 +               return;
2245 +
2246 +       sdata = vif_to_sdata(vif);
2247 +       sband = sdata->local->hw.wiphy->bands[info->band];
2248 +
2249 +       if (ieee80211_is_data(hdr->frame_control))
2250 +               rate_control_apply_mask(sdata, sta, sband, info, dest, max_rates);
2251 +
2252 +       if (dest[0].idx < 0)
2253 +               __rate_control_send_low(&sdata->local->hw, sband, sta, info);
2254 +
2255 +       if (sta)
2256 +               rate_fixup_ratelist(vif, sband, info, dest, max_rates);
2257 +}
2258 +EXPORT_SYMBOL(ieee80211_get_tx_rates);
2259 +
2260  void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
2261                            struct sta_info *sta,
2262                            struct ieee80211_tx_rate_control *txrc)
2263 @@ -435,8 +659,6 @@ void rate_control_get_rate(struct ieee80
2264         struct ieee80211_sta *ista = NULL;
2265         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
2266         int i;
2267 -       u32 mask;
2268 -       u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
2269  
2270         if (sta && test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) {
2271                 ista = &sta->sta;
2272 @@ -454,37 +676,27 @@ void rate_control_get_rate(struct ieee80
2273  
2274         ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
2275  
2276 -       /*
2277 -        * Try to enforce the rateidx mask the user wanted. skip this if the
2278 -        * default mask (allow all rates) is used to save some processing for
2279 -        * the common case.
2280 -        */
2281 -       mask = sdata->rc_rateidx_mask[info->band];
2282 -       memcpy(mcs_mask, sdata->rc_rateidx_mcs_mask[info->band],
2283 -              sizeof(mcs_mask));
2284 -       if (mask != (1 << txrc->sband->n_bitrates) - 1) {
2285 -               if (sta) {
2286 -                       /* Filter out rates that the STA does not support */
2287 -                       mask &= sta->sta.supp_rates[info->band];
2288 -                       for (i = 0; i < sizeof(mcs_mask); i++)
2289 -                               mcs_mask[i] &= sta->sta.ht_cap.mcs.rx_mask[i];
2290 -               }
2291 -               /*
2292 -                * Make sure the rate index selected for each TX rate is
2293 -                * included in the configured mask and change the rate indexes
2294 -                * if needed.
2295 -                */
2296 -               for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2297 -                       /* Skip invalid rates */
2298 -                       if (info->control.rates[i].idx < 0)
2299 -                               break;
2300 -                       rate_idx_match_mask(&info->control.rates[i], txrc,
2301 -                                           mask, mcs_mask);
2302 -               }
2303 -       }
2304 +       if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_RC_TABLE)
2305 +               return;
2306  
2307 -       BUG_ON(info->control.rates[0].idx < 0);
2308 +       ieee80211_get_tx_rates(&sdata->vif, ista, txrc->skb,
2309 +                              info->control.rates,
2310 +                              ARRAY_SIZE(info->control.rates));
2311 +}
2312 +
2313 +int rate_control_set_rates(struct ieee80211_hw *hw,
2314 +                          struct ieee80211_sta *pubsta,
2315 +                          struct ieee80211_sta_rates *rates)
2316 +{
2317 +       struct ieee80211_sta_rates *old = rcu_dereference(pubsta->rates);
2318 +
2319 +       rcu_assign_pointer(pubsta->rates, rates);
2320 +       if (old)
2321 +               kfree_rcu(old, rcu_head);
2322 +
2323 +       return 0;
2324  }
2325 +EXPORT_SYMBOL(rate_control_set_rates);
2326  
2327  int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
2328                                  const char *name)
2329 --- a/net/mac80211/rc80211_minstrel.c
2330 +++ b/net/mac80211/rc80211_minstrel.c
2331 @@ -84,6 +84,50 @@ minstrel_sort_best_tp_rates(struct minst
2332  }
2333  
2334  static void
2335 +minstrel_set_rate(struct minstrel_sta_info *mi, struct ieee80211_sta_rates *ratetbl,
2336 +                 int offset, int idx)
2337 +{
2338 +       struct minstrel_rate *r = &mi->r[idx];
2339 +
2340 +       ratetbl->rate[offset].idx = r->rix;
2341 +       ratetbl->rate[offset].count = r->adjusted_retry_count;
2342 +       ratetbl->rate[offset].count_cts = r->retry_count_cts;
2343 +       ratetbl->rate[offset].count_rts = r->retry_count_rtscts;
2344 +}
2345 +
2346 +static void
2347 +minstrel_update_rates(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
2348 +{
2349 +       struct ieee80211_sta_rates *ratetbl;
2350 +       int i = 0;
2351 +
2352 +       ratetbl = kzalloc(sizeof(*ratetbl), GFP_ATOMIC);
2353 +       if (!ratetbl)
2354 +               return;
2355 +
2356 +       /* Start with max_tp_rate */
2357 +       minstrel_set_rate(mi, ratetbl, i++, mi->max_tp_rate[0]);
2358 +
2359 +       if (mp->hw->max_rates >= 3) {
2360 +               /* At least 3 tx rates supported, use max_tp_rate2 next */
2361 +               minstrel_set_rate(mi, ratetbl, i++, mi->max_tp_rate[1]);
2362 +       }
2363 +
2364 +       if (mp->hw->max_rates >= 2) {
2365 +               /* At least 2 tx rates supported, use max_prob_rate next */
2366 +               minstrel_set_rate(mi, ratetbl, i++, mi->max_prob_rate);
2367 +       }
2368 +
2369 +       /* Use lowest rate last */
2370 +       ratetbl->rate[i].idx = mi->lowest_rix;
2371 +       ratetbl->rate[i].count = mp->max_retry;
2372 +       ratetbl->rate[i].count_cts = mp->max_retry;
2373 +       ratetbl->rate[i].count_rts = mp->max_retry;
2374 +
2375 +       rate_control_set_rates(mp->hw, mi->sta, ratetbl);
2376 +}
2377 +
2378 +static void
2379  minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
2380  {
2381         u8 tmp_tp_rate[MAX_THR_RATES];
2382 @@ -161,6 +205,8 @@ minstrel_update_stats(struct minstrel_pr
2383  
2384         /* Reset update timer */
2385         mi->stats_update = jiffies;
2386 +
2387 +       minstrel_update_rates(mp, mi);
2388  }
2389  
2390  static void
2391 @@ -209,9 +255,9 @@ minstrel_get_retry_count(struct minstrel
2392  {
2393         unsigned int retry = mr->adjusted_retry_count;
2394  
2395 -       if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
2396 +       if (info->control.use_rts)
2397                 retry = max(2U, min(mr->retry_count_rtscts, retry));
2398 -       else if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
2399 +       else if (info->control.use_cts_prot)
2400                 retry = max(2U, min(mr->retry_count_cts, retry));
2401         return retry;
2402  }
2403 @@ -240,13 +286,12 @@ minstrel_get_rate(void *priv, struct iee
2404         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2405         struct minstrel_sta_info *mi = priv_sta;
2406         struct minstrel_priv *mp = priv;
2407 -       struct ieee80211_tx_rate *ar = info->control.rates;
2408 -       unsigned int ndx, sample_ndx = 0;
2409 +       struct ieee80211_tx_rate *rate = &info->control.rates[0];
2410 +       struct minstrel_rate *msr, *mr;
2411 +       unsigned int ndx;
2412         bool mrr_capable;
2413 -       bool indirect_rate_sampling = false;
2414 -       bool rate_sampling = false;
2415 -       int i, delta;
2416 -       int mrr_ndx[3];
2417 +       bool prev_sample = mi->prev_sample;
2418 +       int delta;
2419         int sampling_ratio;
2420  
2421         /* management/no-ack frames do not use rate control */
2422 @@ -262,107 +307,75 @@ minstrel_get_rate(void *priv, struct iee
2423         else
2424                 sampling_ratio = mp->lookaround_rate;
2425  
2426 -       /* init rateindex [ndx] with max throughput rate */
2427 -       ndx = mi->max_tp_rate[0];
2428 -
2429         /* increase sum packet counter */
2430         mi->packet_count++;
2431  
2432         delta = (mi->packet_count * sampling_ratio / 100) -
2433                         (mi->sample_count + mi->sample_deferred / 2);
2434  
2435 -       /* delta > 0: sampling required */
2436 -       if ((delta > 0) && (mrr_capable || !mi->prev_sample)) {
2437 -               struct minstrel_rate *msr;
2438 -               if (mi->packet_count >= 10000) {
2439 -                       mi->sample_deferred = 0;
2440 -                       mi->sample_count = 0;
2441 -                       mi->packet_count = 0;
2442 -               } else if (delta > mi->n_rates * 2) {
2443 -                       /* With multi-rate retry, not every planned sample
2444 -                        * attempt actually gets used, due to the way the retry
2445 -                        * chain is set up - [max_tp,sample,prob,lowest] for
2446 -                        * sample_rate < max_tp.
2447 -                        *
2448 -                        * If there's too much sampling backlog and the link
2449 -                        * starts getting worse, minstrel would start bursting
2450 -                        * out lots of sampling frames, which would result
2451 -                        * in a large throughput loss. */
2452 -                       mi->sample_count += (delta - mi->n_rates * 2);
2453 -               }
2454 +       /* delta < 0: no sampling required */
2455 +       mi->prev_sample = false;
2456 +       if (delta < 0 || (!mrr_capable && prev_sample))
2457 +               return;
2458  
2459 -               /* get next random rate sample */
2460 -               sample_ndx = minstrel_get_next_sample(mi);
2461 -               msr = &mi->r[sample_ndx];
2462 -               rate_sampling = true;
2463 -
2464 -               /* Decide if direct ( 1st mrr stage) or indirect (2nd mrr stage)
2465 -                * rate sampling method should be used.
2466 -                * Respect such rates that are not sampled for 20 interations.
2467 -                */
2468 -               if (mrr_capable &&
2469 -                   msr->perfect_tx_time > mi->r[ndx].perfect_tx_time &&
2470 -                   msr->sample_skipped < 20)
2471 -                               indirect_rate_sampling = true;
2472 -
2473 -               if (!indirect_rate_sampling) {
2474 -                       if (msr->sample_limit != 0) {
2475 -                               ndx = sample_ndx;
2476 -                               mi->sample_count++;
2477 -                               if (msr->sample_limit > 0)
2478 -                                       msr->sample_limit--;
2479 -                       } else
2480 -                               rate_sampling = false;
2481 -               } else {
2482 -                       /* Only use IEEE80211_TX_CTL_RATE_CTRL_PROBE to mark
2483 -                        * packets that have the sampling rate deferred to the
2484 -                        * second MRR stage. Increase the sample counter only
2485 -                        * if the deferred sample rate was actually used.
2486 -                        * Use the sample_deferred counter to make sure that
2487 -                        * the sampling is not done in large bursts */
2488 -                       info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
2489 -                       mi->sample_deferred++;
2490 -               }
2491 +       if (mi->packet_count >= 10000) {
2492 +               mi->sample_deferred = 0;
2493 +               mi->sample_count = 0;
2494 +               mi->packet_count = 0;
2495 +       } else if (delta > mi->n_rates * 2) {
2496 +               /* With multi-rate retry, not every planned sample
2497 +                * attempt actually gets used, due to the way the retry
2498 +                * chain is set up - [max_tp,sample,prob,lowest] for
2499 +                * sample_rate < max_tp.
2500 +                *
2501 +                * If there's too much sampling backlog and the link
2502 +                * starts getting worse, minstrel would start bursting
2503 +                * out lots of sampling frames, which would result
2504 +                * in a large throughput loss. */
2505 +               mi->sample_count += (delta - mi->n_rates * 2);
2506 +       }
2507 +
2508 +       /* get next random rate sample */
2509 +       ndx = minstrel_get_next_sample(mi);
2510 +       msr = &mi->r[ndx];
2511 +       mr = &mi->r[mi->max_tp_rate[0]];
2512 +
2513 +       /* Decide if direct ( 1st mrr stage) or indirect (2nd mrr stage)
2514 +        * rate sampling method should be used.
2515 +        * Respect such rates that are not sampled for 20 interations.
2516 +        */
2517 +       if (mrr_capable &&
2518 +           msr->perfect_tx_time > mr->perfect_tx_time &&
2519 +           msr->sample_skipped < 20) {
2520 +               /* Only use IEEE80211_TX_CTL_RATE_CTRL_PROBE to mark
2521 +                * packets that have the sampling rate deferred to the
2522 +                * second MRR stage. Increase the sample counter only
2523 +                * if the deferred sample rate was actually used.
2524 +                * Use the sample_deferred counter to make sure that
2525 +                * the sampling is not done in large bursts */
2526 +               info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
2527 +               rate++;
2528 +               mi->sample_deferred++;
2529 +       } else {
2530 +               if (!msr->sample_limit != 0)
2531 +                       return;
2532 +
2533 +               mi->sample_count++;
2534 +               if (msr->sample_limit > 0)
2535 +                       msr->sample_limit--;
2536         }
2537 -       mi->prev_sample = rate_sampling;
2538  
2539         /* If we're not using MRR and the sampling rate already
2540          * has a probability of >95%, we shouldn't be attempting
2541          * to use it, as this only wastes precious airtime */
2542 -       if (!mrr_capable && rate_sampling &&
2543 +       if (!mrr_capable &&
2544            (mi->r[ndx].probability > MINSTREL_FRAC(95, 100)))
2545 -               ndx = mi->max_tp_rate[0];
2546 -
2547 -       /* mrr setup for 1st stage */
2548 -       ar[0].idx = mi->r[ndx].rix;
2549 -       ar[0].count = minstrel_get_retry_count(&mi->r[ndx], info);
2550 -
2551 -       /* non mrr setup for 2nd stage */
2552 -       if (!mrr_capable) {
2553 -               if (!rate_sampling)
2554 -                       ar[0].count = mp->max_retry;
2555 -               ar[1].idx = mi->lowest_rix;
2556 -               ar[1].count = mp->max_retry;
2557                 return;
2558 -       }
2559  
2560 -       /* mrr setup for 2nd stage */
2561 -       if (rate_sampling) {
2562 -               if (indirect_rate_sampling)
2563 -                       mrr_ndx[0] = sample_ndx;
2564 -               else
2565 -                       mrr_ndx[0] = mi->max_tp_rate[0];
2566 -       } else {
2567 -               mrr_ndx[0] = mi->max_tp_rate[1];
2568 -       }
2569 +       mi->prev_sample = true;
2570  
2571 -       /* mrr setup for 3rd & 4th stage */
2572 -       mrr_ndx[1] = mi->max_prob_rate;
2573 -       mrr_ndx[2] = 0;
2574 -       for (i = 1; i < 4; i++) {
2575 -               ar[i].idx = mi->r[mrr_ndx[i - 1]].rix;
2576 -               ar[i].count = mi->r[mrr_ndx[i - 1]].adjusted_retry_count;
2577 -       }
2578 +       rate->idx = mi->r[ndx].rix;
2579 +       rate->count = minstrel_get_retry_count(&mi->r[ndx], info);
2580  }
2581  
2582  
2583 @@ -412,12 +425,16 @@ minstrel_rate_init(void *priv, struct ie
2584         unsigned int i, n = 0;
2585         unsigned int t_slot = 9; /* FIXME: get real slot time */
2586  
2587 +       mi->sta = sta;
2588         mi->lowest_rix = rate_lowest_index(sband, sta);
2589         ctl_rate = &sband->bitrates[mi->lowest_rix];
2590         mi->sp_ack_dur = ieee80211_frame_duration(sband->band, 10,
2591                                 ctl_rate->bitrate,
2592                                 !!(ctl_rate->flags & IEEE80211_RATE_ERP_G), 1);
2593  
2594 +       memset(mi->max_tp_rate, 0, sizeof(mi->max_tp_rate));
2595 +       mi->max_prob_rate = 0;
2596 +
2597         for (i = 0; i < sband->n_bitrates; i++) {
2598                 struct minstrel_rate *mr = &mi->r[n];
2599                 unsigned int tx_time = 0, tx_time_cts = 0, tx_time_rtscts = 0;
2600 @@ -460,6 +477,8 @@ minstrel_rate_init(void *priv, struct ie
2601                 } while ((tx_time < mp->segment_size) &&
2602                                 (++mr->retry_count < mp->max_retry));
2603                 mr->adjusted_retry_count = mr->retry_count;
2604 +               if (!(sband->bitrates[i].flags & IEEE80211_RATE_ERP_G))
2605 +                       mr->retry_count_cts = mr->retry_count;
2606         }
2607  
2608         for (i = n; i < sband->n_bitrates; i++) {
2609 @@ -471,6 +490,7 @@ minstrel_rate_init(void *priv, struct ie
2610         mi->stats_update = jiffies;
2611  
2612         init_sample_table(mi);
2613 +       minstrel_update_rates(mp, mi);
2614  }
2615  
2616  static void *
2617 --- a/net/mac80211/rc80211_minstrel.h
2618 +++ b/net/mac80211/rc80211_minstrel.h
2619 @@ -9,7 +9,8 @@
2620  #ifndef __RC_MINSTREL_H
2621  #define __RC_MINSTREL_H
2622  
2623 -#define EWMA_LEVEL     75      /* ewma weighting factor [%] */
2624 +#define EWMA_LEVEL     96      /* ewma weighting factor [/EWMA_DIV] */
2625 +#define EWMA_DIV       128
2626  #define SAMPLE_COLUMNS 10      /* number of columns in sample table */
2627  
2628  
2629 @@ -27,7 +28,7 @@
2630  static inline int
2631  minstrel_ewma(int old, int new, int weight)
2632  {
2633 -       return (new * (100 - weight) + old * weight) / 100;
2634 +       return (new * (EWMA_DIV - weight) + old * weight) / EWMA_DIV;
2635  }
2636  
2637  
2638 @@ -62,6 +63,8 @@ struct minstrel_rate {
2639  };
2640  
2641  struct minstrel_sta_info {
2642 +       struct ieee80211_sta *sta;
2643 +
2644         unsigned long stats_update;
2645         unsigned int sp_ack_dur;
2646         unsigned int rate_avg;
2647 --- a/net/mac80211/rc80211_minstrel_debugfs.c
2648 +++ b/net/mac80211/rc80211_minstrel_debugfs.c
2649 @@ -68,7 +68,7 @@ minstrel_stats_open(struct inode *inode,
2650  
2651         file->private_data = ms;
2652         p = ms->buf;
2653 -       p += sprintf(p, "rate     throughput  ewma prob   this prob  "
2654 +       p += sprintf(p, "rate      throughput  ewma prob  this prob  "
2655                         "this succ/attempt   success    attempts\n");
2656         for (i = 0; i < mi->n_rates; i++) {
2657                 struct minstrel_rate *mr = &mi->r[i];
2658 @@ -86,7 +86,7 @@ minstrel_stats_open(struct inode *inode,
2659                 eprob = MINSTREL_TRUNC(mr->probability * 1000);
2660  
2661                 p += sprintf(p, "  %6u.%1u   %6u.%1u   %6u.%1u        "
2662 -                               "%3u(%3u)   %8llu    %8llu\n",
2663 +                               "   %3u(%3u)  %8llu    %8llu\n",
2664                                 tp / 10, tp % 10,
2665                                 eprob / 10, eprob % 10,
2666                                 prob / 10, prob % 10,
2667 --- a/net/mac80211/rc80211_minstrel_ht.c
2668 +++ b/net/mac80211/rc80211_minstrel_ht.c
2669 @@ -126,6 +126,9 @@ const struct mcs_group minstrel_mcs_grou
2670  
2671  static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES];
2672  
2673 +static void
2674 +minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi);
2675 +
2676  /*
2677   * Look up an MCS group index based on mac80211 rate information
2678   */
2679 @@ -244,6 +247,7 @@ minstrel_ht_update_stats(struct minstrel
2680         struct minstrel_rate_stats *mr;
2681         int cur_prob, cur_prob_tp, cur_tp, cur_tp2;
2682         int group, i, index;
2683 +       bool mi_rates_valid = false;
2684  
2685         if (mi->ampdu_packets > 0) {
2686                 mi->avg_ampdu_len = minstrel_ewma(mi->avg_ampdu_len,
2687 @@ -254,11 +258,10 @@ minstrel_ht_update_stats(struct minstrel
2688  
2689         mi->sample_slow = 0;
2690         mi->sample_count = 0;
2691 -       mi->max_tp_rate = 0;
2692 -       mi->max_tp_rate2 = 0;
2693 -       mi->max_prob_rate = 0;
2694  
2695         for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
2696 +               bool mg_rates_valid = false;
2697 +
2698                 cur_prob = 0;
2699                 cur_prob_tp = 0;
2700                 cur_tp = 0;
2701 @@ -268,15 +271,24 @@ minstrel_ht_update_stats(struct minstrel
2702                 if (!mg->supported)
2703                         continue;
2704  
2705 -               mg->max_tp_rate = 0;
2706 -               mg->max_tp_rate2 = 0;
2707 -               mg->max_prob_rate = 0;
2708                 mi->sample_count++;
2709  
2710                 for (i = 0; i < MCS_GROUP_RATES; i++) {
2711                         if (!(mg->supported & BIT(i)))
2712                                 continue;
2713  
2714 +                       /* initialize rates selections starting indexes */
2715 +                       if (!mg_rates_valid) {
2716 +                               mg->max_tp_rate = mg->max_tp_rate2 =
2717 +                                       mg->max_prob_rate = i;
2718 +                               if (!mi_rates_valid) {
2719 +                                       mi->max_tp_rate = mi->max_tp_rate2 =
2720 +                                               mi->max_prob_rate = i;
2721 +                                       mi_rates_valid = true;
2722 +                               }
2723 +                               mg_rates_valid = true;
2724 +                       }
2725 +
2726                         mr = &mg->rates[i];
2727                         mr->retry_updated = false;
2728                         index = MCS_GROUP_RATES * group + i;
2729 @@ -456,7 +468,7 @@ minstrel_ht_tx_status(void *priv, struct
2730         struct ieee80211_tx_rate *ar = info->status.rates;
2731         struct minstrel_rate_stats *rate, *rate2;
2732         struct minstrel_priv *mp = priv;
2733 -       bool last;
2734 +       bool last, update = false;
2735         int i;
2736  
2737         if (!msp->is_ht)
2738 @@ -505,21 +517,29 @@ minstrel_ht_tx_status(void *priv, struct
2739         rate = minstrel_get_ratestats(mi, mi->max_tp_rate);
2740         if (rate->attempts > 30 &&
2741             MINSTREL_FRAC(rate->success, rate->attempts) <
2742 -           MINSTREL_FRAC(20, 100))
2743 +           MINSTREL_FRAC(20, 100)) {
2744                 minstrel_downgrade_rate(mi, &mi->max_tp_rate, true);
2745 +               update = true;
2746 +       }
2747  
2748         rate2 = minstrel_get_ratestats(mi, mi->max_tp_rate2);
2749         if (rate2->attempts > 30 &&
2750             MINSTREL_FRAC(rate2->success, rate2->attempts) <
2751 -           MINSTREL_FRAC(20, 100))
2752 +           MINSTREL_FRAC(20, 100)) {
2753                 minstrel_downgrade_rate(mi, &mi->max_tp_rate2, false);
2754 +               update = true;
2755 +       }
2756  
2757         if (time_after(jiffies, mi->stats_update + (mp->update_interval / 2 * HZ) / 1000)) {
2758 +               update = true;
2759                 minstrel_ht_update_stats(mp, mi);
2760                 if (!(info->flags & IEEE80211_TX_CTL_AMPDU) &&
2761                     mi->max_prob_rate / MCS_GROUP_RATES != MINSTREL_CCK_GROUP)
2762                         minstrel_aggr_check(sta, skb);
2763         }
2764 +
2765 +       if (update)
2766 +               minstrel_ht_update_rates(mp, mi);
2767  }
2768  
2769  static void
2770 @@ -580,39 +600,73 @@ minstrel_calc_retransmit(struct minstrel
2771                  (++mr->retry_count < mp->max_retry));
2772  }
2773  
2774 -
2775  static void
2776  minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
2777 -                     struct ieee80211_tx_rate *rate, int index,
2778 -                     bool sample, bool rtscts)
2779 +                     struct ieee80211_sta_rates *ratetbl, int offset, int index)
2780  {
2781         const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
2782         struct minstrel_rate_stats *mr;
2783 +       u8 idx;
2784 +       u16 flags;
2785  
2786         mr = minstrel_get_ratestats(mi, index);
2787         if (!mr->retry_updated)
2788                 minstrel_calc_retransmit(mp, mi, index);
2789  
2790 -       if (sample)
2791 -               rate->count = 1;
2792 -       else if (mr->probability < MINSTREL_FRAC(20, 100))
2793 -               rate->count = 2;
2794 -       else if (rtscts)
2795 -               rate->count = mr->retry_count_rtscts;
2796 -       else
2797 -               rate->count = mr->retry_count;
2798 -
2799 -       rate->flags = 0;
2800 -       if (rtscts)
2801 -               rate->flags |= IEEE80211_TX_RC_USE_RTS_CTS;
2802 +       if (mr->probability < MINSTREL_FRAC(20, 100) || !mr->retry_count) {
2803 +               ratetbl->rate[offset].count = 2;
2804 +               ratetbl->rate[offset].count_rts = 2;
2805 +               ratetbl->rate[offset].count_cts = 2;
2806 +       } else {
2807 +               ratetbl->rate[offset].count = mr->retry_count;
2808 +               ratetbl->rate[offset].count_cts = mr->retry_count;
2809 +               ratetbl->rate[offset].count_rts = mr->retry_count_rtscts;
2810 +       }
2811  
2812         if (index / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) {
2813 -               rate->idx = mp->cck_rates[index % ARRAY_SIZE(mp->cck_rates)];
2814 +               idx = mp->cck_rates[index % ARRAY_SIZE(mp->cck_rates)];
2815 +               flags = 0;
2816 +       } else {
2817 +               idx = index % MCS_GROUP_RATES +
2818 +                     (group->streams - 1) * MCS_GROUP_RATES;
2819 +               flags = IEEE80211_TX_RC_MCS | group->flags;
2820 +       }
2821 +
2822 +       if (offset > 0) {
2823 +               ratetbl->rate[offset].count = ratetbl->rate[offset].count_rts;
2824 +               flags |= IEEE80211_TX_RC_USE_RTS_CTS;
2825 +       }
2826 +
2827 +       ratetbl->rate[offset].idx = idx;
2828 +       ratetbl->rate[offset].flags = flags;
2829 +}
2830 +
2831 +static void
2832 +minstrel_ht_update_rates(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
2833 +{
2834 +       struct ieee80211_sta_rates *rates;
2835 +       int i = 0;
2836 +
2837 +       rates = kzalloc(sizeof(*rates), GFP_ATOMIC);
2838 +       if (!rates)
2839                 return;
2840 +
2841 +       /* Start with max_tp_rate */
2842 +       minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate);
2843 +
2844 +       if (mp->hw->max_rates >= 3) {
2845 +               /* At least 3 tx rates supported, use max_tp_rate2 next */
2846 +               minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_tp_rate2);
2847 +       }
2848 +
2849 +       if (mp->hw->max_rates >= 2) {
2850 +               /*
2851 +                * At least 2 tx rates supported, use max_prob_rate next */
2852 +               minstrel_ht_set_rate(mp, mi, rates, i++, mi->max_prob_rate);
2853         }
2854  
2855 -       rate->flags |= IEEE80211_TX_RC_MCS | group->flags;
2856 -       rate->idx = index % MCS_GROUP_RATES + (group->streams - 1) * MCS_GROUP_RATES;
2857 +       rates->rate[i].idx = -1;
2858 +       rate_control_set_rates(mp->hw, mi->sta, rates);
2859  }
2860  
2861  static inline int
2862 @@ -702,13 +756,13 @@ static void
2863  minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
2864                       struct ieee80211_tx_rate_control *txrc)
2865  {
2866 +       const struct mcs_group *sample_group;
2867         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
2868 -       struct ieee80211_tx_rate *ar = info->status.rates;
2869 +       struct ieee80211_tx_rate *rate = &info->status.rates[0];
2870         struct minstrel_ht_sta_priv *msp = priv_sta;
2871         struct minstrel_ht_sta *mi = &msp->ht;
2872         struct minstrel_priv *mp = priv;
2873         int sample_idx;
2874 -       bool sample = false;
2875  
2876         if (rate_control_send_low(sta, priv_sta, txrc))
2877                 return;
2878 @@ -736,51 +790,6 @@ minstrel_ht_get_rate(void *priv, struct 
2879         }
2880  #endif
2881  
2882 -       if (sample_idx >= 0) {
2883 -               sample = true;
2884 -               minstrel_ht_set_rate(mp, mi, &ar[0], sample_idx,
2885 -                       true, false);
2886 -               info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
2887 -       } else {
2888 -               minstrel_ht_set_rate(mp, mi, &ar[0], mi->max_tp_rate,
2889 -                       false, false);
2890 -       }
2891 -
2892 -       if (mp->hw->max_rates >= 3) {
2893 -               /*
2894 -                * At least 3 tx rates supported, use
2895 -                * sample_rate -> max_tp_rate -> max_prob_rate for sampling and
2896 -                * max_tp_rate -> max_tp_rate2 -> max_prob_rate by default.
2897 -                */
2898 -               if (sample_idx >= 0)
2899 -                       minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_tp_rate,
2900 -                               false, false);
2901 -               else
2902 -                       minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_tp_rate2,
2903 -                               false, true);
2904 -
2905 -               minstrel_ht_set_rate(mp, mi, &ar[2], mi->max_prob_rate,
2906 -                                    false, !sample);
2907 -
2908 -               ar[3].count = 0;
2909 -               ar[3].idx = -1;
2910 -       } else if (mp->hw->max_rates == 2) {
2911 -               /*
2912 -                * Only 2 tx rates supported, use
2913 -                * sample_rate -> max_prob_rate for sampling and
2914 -                * max_tp_rate -> max_prob_rate by default.
2915 -                */
2916 -               minstrel_ht_set_rate(mp, mi, &ar[1], mi->max_prob_rate,
2917 -                                    false, !sample);
2918 -
2919 -               ar[2].count = 0;
2920 -               ar[2].idx = -1;
2921 -       } else {
2922 -               /* Not using MRR, only use the first rate */
2923 -               ar[1].count = 0;
2924 -               ar[1].idx = -1;
2925 -       }
2926 -
2927         mi->total_packets++;
2928  
2929         /* wraparound */
2930 @@ -788,6 +797,16 @@ minstrel_ht_get_rate(void *priv, struct 
2931                 mi->total_packets = 0;
2932                 mi->sample_packets = 0;
2933         }
2934 +
2935 +       if (sample_idx < 0)
2936 +               return;
2937 +
2938 +       sample_group = &minstrel_mcs_groups[sample_idx / MCS_GROUP_RATES];
2939 +       info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
2940 +       rate->idx = sample_idx % MCS_GROUP_RATES +
2941 +                   (sample_group->streams - 1) * MCS_GROUP_RATES;
2942 +       rate->flags = IEEE80211_TX_RC_MCS | sample_group->flags;
2943 +       rate->count = 1;
2944  }
2945  
2946  static void
2947 @@ -837,6 +856,8 @@ minstrel_ht_update_caps(void *priv, stru
2948  
2949         msp->is_ht = true;
2950         memset(mi, 0, sizeof(*mi));
2951 +
2952 +       mi->sta = sta;
2953         mi->stats_update = jiffies;
2954  
2955         ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1);
2956 @@ -898,6 +919,9 @@ minstrel_ht_update_caps(void *priv, stru
2957         if (!n_supported)
2958                 goto use_legacy;
2959  
2960 +       minstrel_ht_update_stats(mp, mi);
2961 +       minstrel_ht_update_rates(mp, mi);
2962 +
2963         return;
2964  
2965  use_legacy:
2966 --- a/net/mac80211/rc80211_minstrel_ht.h
2967 +++ b/net/mac80211/rc80211_minstrel_ht.h
2968 @@ -65,6 +65,8 @@ struct minstrel_mcs_group_data {
2969  };
2970  
2971  struct minstrel_ht_sta {
2972 +       struct ieee80211_sta *sta;
2973 +
2974         /* ampdu length (average, per sampling interval) */
2975         unsigned int ampdu_len;
2976         unsigned int ampdu_packets;
2977 --- a/net/mac80211/rx.c
2978 +++ b/net/mac80211/rx.c
2979 @@ -1372,6 +1372,7 @@ ieee80211_rx_h_sta_process(struct ieee80
2980         struct sk_buff *skb = rx->skb;
2981         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
2982         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2983 +       int i;
2984  
2985         if (!sta)
2986                 return RX_CONTINUE;
2987 @@ -1422,6 +1423,19 @@ ieee80211_rx_h_sta_process(struct ieee80
2988                 ewma_add(&sta->avg_signal, -status->signal);
2989         }
2990  
2991 +       if (status->chains) {
2992 +               sta->chains = status->chains;
2993 +               for (i = 0; i < ARRAY_SIZE(status->chain_signal); i++) {
2994 +                       int signal = status->chain_signal[i];
2995 +
2996 +                       if (!(status->chains & BIT(i)))
2997 +                               continue;
2998 +
2999 +                       sta->chain_signal_last[i] = signal;
3000 +                       ewma_add(&sta->chain_signal_avg[i], -signal);
3001 +               }
3002 +       }
3003 +
3004         /*
3005          * Change STA power saving mode only at the end of a frame
3006          * exchange sequence.
3007 @@ -2085,6 +2099,7 @@ ieee80211_rx_h_mesh_fwding(struct ieee80
3008         }
3009  
3010         fwd_hdr =  (struct ieee80211_hdr *) fwd_skb->data;
3011 +       fwd_hdr->frame_control &= ~cpu_to_le16(IEEE80211_FCTL_RETRY);
3012         info = IEEE80211_SKB_CB(fwd_skb);
3013         memset(info, 0, sizeof(*info));
3014         info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
3015 @@ -2356,6 +2371,7 @@ ieee80211_rx_h_action(struct ieee80211_r
3016                     sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3017                     sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
3018                     sdata->vif.type != NL80211_IFTYPE_AP &&
3019 +                   sdata->vif.type != NL80211_IFTYPE_WDS &&
3020                     sdata->vif.type != NL80211_IFTYPE_ADHOC)
3021                         break;
3022  
3023 @@ -2423,6 +2439,22 @@ ieee80211_rx_h_action(struct ieee80211_r
3024                 }
3025  
3026                 break;
3027 +       case WLAN_CATEGORY_PUBLIC:
3028 +               if (len < IEEE80211_MIN_ACTION_SIZE + 1)
3029 +                       goto invalid;
3030 +               if (sdata->vif.type != NL80211_IFTYPE_STATION)
3031 +                       break;
3032 +               if (!rx->sta)
3033 +                       break;
3034 +               if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid))
3035 +                       break;
3036 +               if (mgmt->u.action.u.ext_chan_switch.action_code !=
3037 +                               WLAN_PUB_ACTION_EXT_CHANSW_ANN)
3038 +                       break;
3039 +               if (len < offsetof(struct ieee80211_mgmt,
3040 +                                  u.action.u.ext_chan_switch.variable))
3041 +                       goto invalid;
3042 +               goto queue;
3043         case WLAN_CATEGORY_VHT:
3044                 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
3045                     sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
3046 @@ -2506,10 +2538,6 @@ ieee80211_rx_h_action(struct ieee80211_r
3047                         ieee80211_process_measurement_req(sdata, mgmt, len);
3048                         goto handled;
3049                 case WLAN_ACTION_SPCT_CHL_SWITCH:
3050 -                       if (len < (IEEE80211_MIN_ACTION_SIZE +
3051 -                                  sizeof(mgmt->u.action.u.chan_switch)))
3052 -                               break;
3053 -
3054                         if (sdata->vif.type != NL80211_IFTYPE_STATION)
3055                                 break;
3056  
3057 @@ -2695,14 +2723,15 @@ ieee80211_rx_h_mgmt(struct ieee80211_rx_
3058  
3059         if (!ieee80211_vif_is_mesh(&sdata->vif) &&
3060             sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3061 -           sdata->vif.type != NL80211_IFTYPE_STATION)
3062 +           sdata->vif.type != NL80211_IFTYPE_STATION &&
3063 +           sdata->vif.type != NL80211_IFTYPE_WDS)
3064                 return RX_DROP_MONITOR;
3065  
3066         switch (stype) {
3067         case cpu_to_le16(IEEE80211_STYPE_AUTH):
3068         case cpu_to_le16(IEEE80211_STYPE_BEACON):
3069         case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
3070 -               /* process for all: mesh, mlme, ibss */
3071 +               /* process for all: mesh, mlme, ibss, wds */
3072                 break;
3073         case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
3074         case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
3075 @@ -3031,10 +3060,16 @@ static int prepare_for_handlers(struct i
3076                 }
3077                 break;
3078         case NL80211_IFTYPE_WDS:
3079 -               if (bssid || !ieee80211_is_data(hdr->frame_control))
3080 -                       return 0;
3081                 if (!ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2))
3082                         return 0;
3083 +
3084 +               if (ieee80211_is_data(hdr->frame_control) ||
3085 +                   ieee80211_is_action(hdr->frame_control)) {
3086 +                       if (compare_ether_addr(sdata->vif.addr, hdr->addr1))
3087 +                               return 0;
3088 +               } else if (!ieee80211_is_beacon(hdr->frame_control))
3089 +                       return 0;
3090 +
3091                 break;
3092         case NL80211_IFTYPE_P2P_DEVICE:
3093                 if (!ieee80211_is_public_action(hdr, skb->len) &&
3094 --- a/net/mac80211/scan.c
3095 +++ b/net/mac80211/scan.c
3096 @@ -181,7 +181,7 @@ void ieee80211_scan_rx(struct ieee80211_
3097         if (baselen > skb->len)
3098                 return;
3099  
3100 -       ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
3101 +       ieee802_11_parse_elems(elements, skb->len - baselen, false, &elems);
3102  
3103         channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
3104  
3105 --- a/net/mac80211/sta_info.h
3106 +++ b/net/mac80211/sta_info.h
3107 @@ -32,7 +32,6 @@
3108   * @WLAN_STA_SHORT_PREAMBLE: Station is capable of receiving short-preamble
3109   *     frames.
3110   * @WLAN_STA_WME: Station is a QoS-STA.
3111 - * @WLAN_STA_WDS: Station is one of our WDS peers.
3112   * @WLAN_STA_CLEAR_PS_FILT: Clear PS filter in hardware (using the
3113   *     IEEE80211_TX_CTL_CLEAR_PS_FILT control flag) when the next
3114   *     frame to this station is transmitted.
3115 @@ -66,7 +65,6 @@ enum ieee80211_sta_info_flags {
3116         WLAN_STA_AUTHORIZED,
3117         WLAN_STA_SHORT_PREAMBLE,
3118         WLAN_STA_WME,
3119 -       WLAN_STA_WDS,
3120         WLAN_STA_CLEAR_PS_FILT,
3121         WLAN_STA_MFP,
3122         WLAN_STA_BLOCK_BA,
3123 @@ -344,6 +342,11 @@ struct sta_info {
3124         int last_signal;
3125         struct ewma avg_signal;
3126         int last_ack_signal;
3127 +
3128 +       u8 chains;
3129 +       s8 chain_signal_last[IEEE80211_MAX_CHAINS];
3130 +       struct ewma chain_signal_avg[IEEE80211_MAX_CHAINS];
3131 +
3132         /* Plus 1 for non-QoS frames */
3133         __le16 last_seq_ctrl[IEEE80211_NUM_TIDS + 1];
3134  
3135 --- a/net/mac80211/trace.h
3136 +++ b/net/mac80211/trace.h
3137 @@ -990,23 +990,23 @@ TRACE_EVENT(drv_channel_switch,
3138  
3139         TP_STRUCT__entry(
3140                 LOCAL_ENTRY
3141 +               CHANDEF_ENTRY
3142                 __field(u64, timestamp)
3143                 __field(bool, block_tx)
3144 -               __field(u16, freq)
3145                 __field(u8, count)
3146         ),
3147  
3148         TP_fast_assign(
3149                 LOCAL_ASSIGN;
3150 +               CHANDEF_ASSIGN(&ch_switch->chandef)
3151                 __entry->timestamp = ch_switch->timestamp;
3152                 __entry->block_tx = ch_switch->block_tx;
3153 -               __entry->freq = ch_switch->channel->center_freq;
3154                 __entry->count = ch_switch->count;
3155         ),
3156  
3157         TP_printk(
3158 -               LOCAL_PR_FMT " new freq:%u count:%d",
3159 -               LOCAL_PR_ARG, __entry->freq, __entry->count
3160 +               LOCAL_PR_FMT " new " CHANDEF_PR_FMT " count:%d",
3161 +               LOCAL_PR_ARG, CHANDEF_PR_ARG, __entry->count
3162         )
3163  );
3164  
3165 --- a/net/mac80211/tx.c
3166 +++ b/net/mac80211/tx.c
3167 @@ -48,15 +48,15 @@ static __le16 ieee80211_duration(struct 
3168         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3169  
3170         /* assume HW handles this */
3171 -       if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
3172 +       if (tx->rate.flags & IEEE80211_TX_RC_MCS)
3173                 return 0;
3174  
3175         /* uh huh? */
3176 -       if (WARN_ON_ONCE(info->control.rates[0].idx < 0))
3177 +       if (WARN_ON_ONCE(tx->rate.idx < 0))
3178                 return 0;
3179  
3180         sband = local->hw.wiphy->bands[info->band];
3181 -       txrate = &sband->bitrates[info->control.rates[0].idx];
3182 +       txrate = &sband->bitrates[tx->rate.idx];
3183  
3184         erp = txrate->flags & IEEE80211_RATE_ERP_G;
3185  
3186 @@ -617,11 +617,9 @@ ieee80211_tx_h_rate_ctrl(struct ieee8021
3187         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
3188         struct ieee80211_hdr *hdr = (void *)tx->skb->data;
3189         struct ieee80211_supported_band *sband;
3190 -       struct ieee80211_rate *rate;
3191 -       int i;
3192         u32 len;
3193 -       bool inval = false, rts = false, short_preamble = false;
3194         struct ieee80211_tx_rate_control txrc;
3195 +       struct ieee80211_sta_rates *ratetbl = NULL;
3196         bool assoc = false;
3197  
3198         memset(&txrc, 0, sizeof(txrc));
3199 @@ -642,18 +640,23 @@ ieee80211_tx_h_rate_ctrl(struct ieee8021
3200                 txrc.max_rate_idx = -1;
3201         else
3202                 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
3203 -       memcpy(txrc.rate_idx_mcs_mask,
3204 -              tx->sdata->rc_rateidx_mcs_mask[info->band],
3205 -              sizeof(txrc.rate_idx_mcs_mask));
3206 +
3207 +       if (tx->sdata->rc_has_mcs_mask[info->band])
3208 +               txrc.rate_idx_mcs_mask =
3209 +                       tx->sdata->rc_rateidx_mcs_mask[info->band];
3210 +
3211         txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
3212                     tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
3213                     tx->sdata->vif.type == NL80211_IFTYPE_ADHOC);
3214  
3215         /* set up RTS protection if desired */
3216         if (len > tx->local->hw.wiphy->rts_threshold) {
3217 -               txrc.rts = rts = true;
3218 +               txrc.rts = true;
3219         }
3220  
3221 +       info->control.use_rts = txrc.rts;
3222 +       info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
3223 +
3224         /*
3225          * Use short preamble if the BSS can handle it, but not for
3226          * management frames unless we know the receiver can handle
3227 @@ -663,7 +666,9 @@ ieee80211_tx_h_rate_ctrl(struct ieee8021
3228         if (tx->sdata->vif.bss_conf.use_short_preamble &&
3229             (ieee80211_is_data(hdr->frame_control) ||
3230              (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
3231 -               txrc.short_preamble = short_preamble = true;
3232 +               txrc.short_preamble = true;
3233 +
3234 +       info->control.short_preamble = txrc.short_preamble;
3235  
3236         if (tx->sta)
3237                 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
3238 @@ -687,16 +692,38 @@ ieee80211_tx_h_rate_ctrl(struct ieee8021
3239          */
3240         rate_control_get_rate(tx->sdata, tx->sta, &txrc);
3241  
3242 -       if (unlikely(info->control.rates[0].idx < 0))
3243 -               return TX_DROP;
3244 +       if (tx->sta && !info->control.skip_table)
3245 +               ratetbl = rcu_dereference(tx->sta->sta.rates);
3246 +
3247 +       if (unlikely(info->control.rates[0].idx < 0)) {
3248 +               if (ratetbl) {
3249 +                       struct ieee80211_tx_rate rate = {
3250 +                               .idx = ratetbl->rate[0].idx,
3251 +                               .flags = ratetbl->rate[0].flags,
3252 +                               .count = ratetbl->rate[0].count
3253 +                       };
3254 +
3255 +                       if (ratetbl->rate[0].idx < 0)
3256 +                               return TX_DROP;
3257 +
3258 +                       tx->rate = rate;
3259 +               } else {
3260 +                       return TX_DROP;
3261 +               }
3262 +       } else {
3263 +               tx->rate = info->control.rates[0];
3264 +       }
3265  
3266         if (txrc.reported_rate.idx < 0) {
3267 -               txrc.reported_rate = info->control.rates[0];
3268 +               txrc.reported_rate = tx->rate;
3269                 if (tx->sta && ieee80211_is_data(hdr->frame_control))
3270                         tx->sta->last_tx_rate = txrc.reported_rate;
3271         } else if (tx->sta)
3272                 tx->sta->last_tx_rate = txrc.reported_rate;
3273  
3274 +       if (ratetbl)
3275 +               return TX_CONTINUE;
3276 +
3277         if (unlikely(!info->control.rates[0].count))
3278                 info->control.rates[0].count = 1;
3279  
3280 @@ -704,91 +731,6 @@ ieee80211_tx_h_rate_ctrl(struct ieee8021
3281                          (info->flags & IEEE80211_TX_CTL_NO_ACK)))
3282                 info->control.rates[0].count = 1;
3283  
3284 -       if (is_multicast_ether_addr(hdr->addr1)) {
3285 -               /*
3286 -                * XXX: verify the rate is in the basic rateset
3287 -                */
3288 -               return TX_CONTINUE;
3289 -       }
3290 -
3291 -       /*
3292 -        * set up the RTS/CTS rate as the fastest basic rate
3293 -        * that is not faster than the data rate
3294 -        *
3295 -        * XXX: Should this check all retry rates?
3296 -        */
3297 -       if (!(info->control.rates[0].flags & IEEE80211_TX_RC_MCS)) {
3298 -               s8 baserate = 0;
3299 -
3300 -               rate = &sband->bitrates[info->control.rates[0].idx];
3301 -
3302 -               for (i = 0; i < sband->n_bitrates; i++) {
3303 -                       /* must be a basic rate */
3304 -                       if (!(tx->sdata->vif.bss_conf.basic_rates & BIT(i)))
3305 -                               continue;
3306 -                       /* must not be faster than the data rate */
3307 -                       if (sband->bitrates[i].bitrate > rate->bitrate)
3308 -                               continue;
3309 -                       /* maximum */
3310 -                       if (sband->bitrates[baserate].bitrate <
3311 -                            sband->bitrates[i].bitrate)
3312 -                               baserate = i;
3313 -               }
3314 -
3315 -               info->control.rts_cts_rate_idx = baserate;
3316 -       }
3317 -
3318 -       for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
3319 -               /*
3320 -                * make sure there's no valid rate following
3321 -                * an invalid one, just in case drivers don't
3322 -                * take the API seriously to stop at -1.
3323 -                */
3324 -               if (inval) {
3325 -                       info->control.rates[i].idx = -1;
3326 -                       continue;
3327 -               }
3328 -               if (info->control.rates[i].idx < 0) {
3329 -                       inval = true;
3330 -                       continue;
3331 -               }
3332 -
3333 -               /*
3334 -                * For now assume MCS is already set up correctly, this
3335 -                * needs to be fixed.
3336 -                */
3337 -               if (info->control.rates[i].flags & IEEE80211_TX_RC_MCS) {
3338 -                       WARN_ON(info->control.rates[i].idx > 76);
3339 -                       continue;
3340 -               }
3341 -
3342 -               /* set up RTS protection if desired */
3343 -               if (rts)
3344 -                       info->control.rates[i].flags |=
3345 -                               IEEE80211_TX_RC_USE_RTS_CTS;
3346 -
3347 -               /* RC is busted */
3348 -               if (WARN_ON_ONCE(info->control.rates[i].idx >=
3349 -                                sband->n_bitrates)) {
3350 -                       info->control.rates[i].idx = -1;
3351 -                       continue;
3352 -               }
3353 -
3354 -               rate = &sband->bitrates[info->control.rates[i].idx];
3355 -
3356 -               /* set up short preamble */
3357 -               if (short_preamble &&
3358 -                   rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
3359 -                       info->control.rates[i].flags |=
3360 -                               IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
3361 -
3362 -               /* set up G protection */
3363 -               if (!rts && tx->sdata->vif.bss_conf.use_cts_prot &&
3364 -                   rate->flags & IEEE80211_RATE_ERP_G)
3365 -                       info->control.rates[i].flags |=
3366 -                               IEEE80211_TX_RC_USE_CTS_PROTECT;
3367 -       }
3368 -
3369         return TX_CONTINUE;
3370  }
3371  
3372 @@ -2508,8 +2450,6 @@ struct sk_buff *ieee80211_beacon_get_tim
3373                 txrc.max_rate_idx = -1;
3374         else
3375                 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
3376 -       memcpy(txrc.rate_idx_mcs_mask, sdata->rc_rateidx_mcs_mask[band],
3377 -              sizeof(txrc.rate_idx_mcs_mask));
3378         txrc.bss = true;
3379         rate_control_get_rate(sdata, NULL, &txrc);
3380  
3381 --- a/net/mac80211/util.c
3382 +++ b/net/mac80211/util.c
3383 @@ -485,7 +485,8 @@ int ieee80211_queue_stopped(struct ieee8
3384                 return true;
3385  
3386         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
3387 -       ret = !!local->queue_stop_reasons[queue];
3388 +       ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
3389 +                      &local->queue_stop_reasons[queue]);
3390         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
3391         return ret;
3392  }
3393 @@ -660,7 +661,7 @@ void ieee80211_queue_delayed_work(struct
3394  }
3395  EXPORT_SYMBOL(ieee80211_queue_delayed_work);
3396  
3397 -u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
3398 +u32 ieee802_11_parse_elems_crc(u8 *start, size_t len, bool action,
3399                                struct ieee802_11_elems *elems,
3400                                u64 filter, u32 crc)
3401  {
3402 @@ -668,6 +669,7 @@ u32 ieee802_11_parse_elems_crc(u8 *start
3403         u8 *pos = start;
3404         bool calc_crc = filter != 0;
3405         DECLARE_BITMAP(seen_elems, 256);
3406 +       const u8 *ie;
3407  
3408         bitmap_zero(seen_elems, 256);
3409         memset(elems, 0, sizeof(*elems));
3410 @@ -715,6 +717,12 @@ u32 ieee802_11_parse_elems_crc(u8 *start
3411                 case WLAN_EID_COUNTRY:
3412                 case WLAN_EID_PWR_CONSTRAINT:
3413                 case WLAN_EID_TIMEOUT_INTERVAL:
3414 +               case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
3415 +               case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
3416 +               /*
3417 +                * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
3418 +                * that if the content gets bigger it might be needed more than once
3419 +                */
3420                         if (test_bit(id, seen_elems)) {
3421                                 elems->parse_error = true;
3422                                 left -= elen;
3423 @@ -862,6 +870,48 @@ u32 ieee802_11_parse_elems_crc(u8 *start
3424                         }
3425                         elems->ch_switch_ie = (void *)pos;
3426                         break;
3427 +               case WLAN_EID_EXT_CHANSWITCH_ANN:
3428 +                       if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
3429 +                               elem_parse_failed = true;
3430 +                               break;
3431 +                       }
3432 +                       elems->ext_chansw_ie = (void *)pos;
3433 +                       break;
3434 +               case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
3435 +                       if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
3436 +                               elem_parse_failed = true;
3437 +                               break;
3438 +                       }
3439 +                       elems->sec_chan_offs = (void *)pos;
3440 +                       break;
3441 +               case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
3442 +                       if (!action ||
3443 +                           elen != sizeof(*elems->wide_bw_chansw_ie)) {
3444 +                               elem_parse_failed = true;
3445 +                               break;
3446 +                       }
3447 +                       elems->wide_bw_chansw_ie = (void *)pos;
3448 +                       break;
3449 +               case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
3450 +                       if (action) {
3451 +                               elem_parse_failed = true;
3452 +                               break;
3453 +                       }
3454 +                       /*
3455 +                        * This is a bit tricky, but as we only care about
3456 +                        * the wide bandwidth channel switch element, so
3457 +                        * just parse it out manually.
3458 +                        */
3459 +                       ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
3460 +                                             pos, elen);
3461 +                       if (ie) {
3462 +                               if (ie[1] == sizeof(*elems->wide_bw_chansw_ie))
3463 +                                       elems->wide_bw_chansw_ie =
3464 +                                               (void *)(ie + 2);
3465 +                               else
3466 +                                       elem_parse_failed = true;
3467 +                       }
3468 +                       break;
3469                 case WLAN_EID_COUNTRY:
3470                         elems->country_elem = pos;
3471                         elems->country_elem_len = elen;
3472 --- a/net/wireless/reg.c
3473 +++ b/net/wireless/reg.c
3474 @@ -857,7 +857,7 @@ static void handle_channel(struct wiphy 
3475                         return;
3476  
3477                 REG_DBG_PRINT("Disabling freq %d MHz\n", chan->center_freq);
3478 -               chan->flags = IEEE80211_CHAN_DISABLED;
3479 +               chan->flags |= IEEE80211_CHAN_DISABLED;
3480                 return;
3481         }
3482  
3483 --- a/net/wireless/util.c
3484 +++ b/net/wireless/util.c
3485 @@ -1156,6 +1156,26 @@ int cfg80211_get_p2p_attr(const u8 *ies,
3486  }
3487  EXPORT_SYMBOL(cfg80211_get_p2p_attr);
3488  
3489 +bool ieee80211_operating_class_to_band(u8 operating_class,
3490 +                                      enum ieee80211_band *band)
3491 +{
3492 +       switch (operating_class) {
3493 +       case 112:
3494 +       case 115 ... 127:
3495 +               *band = IEEE80211_BAND_5GHZ;
3496 +               return true;
3497 +       case 81:
3498 +       case 82:
3499 +       case 83:
3500 +       case 84:
3501 +               *band = IEEE80211_BAND_2GHZ;
3502 +               return true;
3503 +       }
3504 +
3505 +       return false;
3506 +}
3507 +EXPORT_SYMBOL(ieee80211_operating_class_to_band);
3508 +
3509  int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
3510                                  u32 beacon_int)
3511  {
3512 --- a/include/uapi/linux/nl80211.h
3513 +++ b/include/uapi/linux/nl80211.h
3514 @@ -1973,6 +1973,10 @@ enum nl80211_sta_bss_param {
3515   * @NL80211_STA_INFO_PEER_PM: peer mesh STA link-specific power mode
3516   * @NL80211_STA_INFO_NONPEER_PM: neighbor mesh STA power save mode towards
3517   *     non-peer STA
3518 + * @NL80211_STA_INFO_CHAIN_SIGNAL: per-chain signal strength of last PPDU
3519 + *     Contains a nested array of signal strength attributes (u8, dBm)
3520 + * @NL80211_STA_INFO_CHAIN_SIGNAL_AVG: per-chain signal strength average
3521 + *     Same format as NL80211_STA_INFO_CHAIN_SIGNAL.
3522   * @__NL80211_STA_INFO_AFTER_LAST: internal
3523   * @NL80211_STA_INFO_MAX: highest possible station info attribute
3524   */
3525 @@ -2002,6 +2006,8 @@ enum nl80211_sta_info {
3526         NL80211_STA_INFO_NONPEER_PM,
3527         NL80211_STA_INFO_RX_BYTES64,
3528         NL80211_STA_INFO_TX_BYTES64,
3529 +       NL80211_STA_INFO_CHAIN_SIGNAL,
3530 +       NL80211_STA_INFO_CHAIN_SIGNAL_AVG,
3531  
3532         /* keep last */
3533         __NL80211_STA_INFO_AFTER_LAST,
3534 --- a/net/mac80211/sta_info.c
3535 +++ b/net/mac80211/sta_info.c
3536 @@ -358,6 +358,8 @@ struct sta_info *sta_info_alloc(struct i
3537         do_posix_clock_monotonic_gettime(&uptime);
3538         sta->last_connected = uptime.tv_sec;
3539         ewma_init(&sta->avg_signal, 1024, 8);
3540 +       for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
3541 +               ewma_init(&sta->chain_signal_avg[i], 1024, 8);
3542  
3543         if (sta_prepare_rate_control(local, sta, gfp)) {
3544                 kfree(sta);
3545 --- a/net/wireless/nl80211.c
3546 +++ b/net/wireless/nl80211.c
3547 @@ -3367,6 +3367,32 @@ static bool nl80211_put_sta_rate(struct 
3548         return true;
3549  }
3550  
3551 +static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3552 +                              int id)
3553 +{
3554 +       void *attr;
3555 +       int i = 0;
3556 +
3557 +       if (!mask)
3558 +               return true;
3559 +
3560 +       attr = nla_nest_start(msg, id);
3561 +       if (!attr)
3562 +               return false;
3563 +
3564 +       for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3565 +               if (!(mask & BIT(i)))
3566 +                       continue;
3567 +
3568 +               if (nla_put_u8(msg, i, signal[i]))
3569 +                       return false;
3570 +       }
3571 +
3572 +       nla_nest_end(msg, attr);
3573 +
3574 +       return true;
3575 +}
3576 +
3577  static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
3578                                 int flags,
3579                                 struct cfg80211_registered_device *rdev,
3580 @@ -3438,6 +3464,18 @@ static int nl80211_send_station(struct s
3581         default:
3582                 break;
3583         }
3584 +       if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3585 +               if (!nl80211_put_signal(msg, sinfo->chains,
3586 +                                       sinfo->chain_signal,
3587 +                                       NL80211_STA_INFO_CHAIN_SIGNAL))
3588 +                       goto nla_put_failure;
3589 +       }
3590 +       if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3591 +               if (!nl80211_put_signal(msg, sinfo->chains,
3592 +                                       sinfo->chain_signal_avg,
3593 +                                       NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3594 +                       goto nla_put_failure;
3595 +       }
3596         if (sinfo->filled & STATION_INFO_TX_BITRATE) {
3597                 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3598                                           NL80211_STA_INFO_TX_BITRATE))
3599 --- a/drivers/net/wireless/ath/ath9k/init.c
3600 +++ b/drivers/net/wireless/ath/ath9k/init.c
3601 @@ -768,7 +768,8 @@ void ath9k_set_hw_capab(struct ath_softc
3602                 IEEE80211_HW_SUPPORTS_PS |
3603                 IEEE80211_HW_PS_NULLFUNC_STACK |
3604                 IEEE80211_HW_SPECTRUM_MGMT |
3605 -               IEEE80211_HW_REPORTS_TX_ACK_STATUS;
3606 +               IEEE80211_HW_REPORTS_TX_ACK_STATUS |
3607 +               IEEE80211_HW_SUPPORTS_RC_TABLE;
3608  
3609         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_HT)
3610                  hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
3611 --- a/drivers/net/wireless/ath/ath9k/xmit.c
3612 +++ b/drivers/net/wireless/ath/ath9k/xmit.c
3613 @@ -157,6 +157,13 @@ static void ath_send_bar(struct ath_atx_
3614                            seqno << IEEE80211_SEQ_SEQ_SHIFT);
3615  }
3616  
3617 +static void ath_set_rates(struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3618 +                         struct ath_buf *bf)
3619 +{
3620 +       ieee80211_get_tx_rates(vif, sta, bf->bf_mpdu, bf->rates,
3621 +                              ARRAY_SIZE(bf->rates));
3622 +}
3623 +
3624  static void ath_tx_flush_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
3625  {
3626         struct ath_txq *txq = tid->ac->txq;
3627 @@ -189,6 +196,7 @@ static void ath_tx_flush_tid(struct ath_
3628                         ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, 0);
3629                         sendbar = true;
3630                 } else {
3631 +                       ath_set_rates(tid->an->vif, tid->an->sta, bf);
3632                         ath_tx_send_normal(sc, txq, NULL, skb);
3633                 }
3634         }
3635 @@ -407,7 +415,7 @@ static void ath_tx_complete_aggr(struct 
3636  
3637         tx_info = IEEE80211_SKB_CB(skb);
3638  
3639 -       memcpy(rates, tx_info->control.rates, sizeof(rates));
3640 +       memcpy(rates, bf->rates, sizeof(rates));
3641  
3642         retries = ts->ts_longretry + 1;
3643         for (i = 0; i < ts->ts_rateindex; i++)
3644 @@ -736,8 +744,6 @@ static int ath_compute_num_delims(struct
3645                                   bool first_subfrm)
3646  {
3647  #define FIRST_DESC_NDELIMS 60
3648 -       struct sk_buff *skb = bf->bf_mpdu;
3649 -       struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
3650         u32 nsymbits, nsymbols;
3651         u16 minlen;
3652         u8 flags, rix;
3653 @@ -778,8 +784,8 @@ static int ath_compute_num_delims(struct
3654         if (tid->an->mpdudensity == 0)
3655                 return ndelim;
3656  
3657 -       rix = tx_info->control.rates[0].idx;
3658 -       flags = tx_info->control.rates[0].flags;
3659 +       rix = bf->rates[0].idx;
3660 +       flags = bf->rates[0].flags;
3661         width = (flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ? 1 : 0;
3662         half_gi = (flags & IEEE80211_TX_RC_SHORT_GI) ? 1 : 0;
3663  
3664 @@ -858,6 +864,7 @@ static enum ATH_AGGR_STATUS ath_tx_form_
3665                         bf_first = bf;
3666  
3667                 if (!rl) {
3668 +                       ath_set_rates(tid->an->vif, tid->an->sta, bf);
3669                         aggr_limit = ath_lookup_rate(sc, bf, tid);
3670                         rl = 1;
3671                 }
3672 @@ -998,14 +1005,14 @@ static void ath_buf_set_rate(struct ath_
3673  
3674         skb = bf->bf_mpdu;
3675         tx_info = IEEE80211_SKB_CB(skb);
3676 -       rates = tx_info->control.rates;
3677 +       rates = bf->rates;
3678         hdr = (struct ieee80211_hdr *)skb->data;
3679  
3680         /* set dur_update_en for l-sig computation except for PS-Poll frames */
3681         info->dur_update = !ieee80211_is_pspoll(hdr->frame_control);
3682         info->rtscts_rate = fi->rtscts_rate;
3683  
3684 -       for (i = 0; i < 4; i++) {
3685 +       for (i = 0; i < ARRAY_SIZE(bf->rates); i++) {
3686                 bool is_40, is_sgi, is_sp;
3687                 int phy;
3688  
3689 @@ -1743,6 +1750,7 @@ static void ath_tx_send_ampdu(struct ath
3690                 return;
3691         }
3692  
3693 +       ath_set_rates(tid->an->vif, tid->an->sta, bf);
3694         bf->bf_state.bf_type = BUF_AMPDU;
3695         INIT_LIST_HEAD(&bf_head);
3696         list_add(&bf->list, &bf_head);
3697 @@ -1892,49 +1900,6 @@ static struct ath_buf *ath_tx_setup_buff
3698         return bf;
3699  }
3700  
3701 -/* FIXME: tx power */
3702 -static void ath_tx_start_dma(struct ath_softc *sc, struct sk_buff *skb,
3703 -                            struct ath_tx_control *txctl)
3704 -{
3705 -       struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
3706 -       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3707 -       struct ath_atx_tid *tid = NULL;
3708 -       struct ath_buf *bf;
3709 -       u8 tidno;
3710 -
3711 -       if (txctl->an && ieee80211_is_data_qos(hdr->frame_control)) {
3712 -               tidno = ieee80211_get_qos_ctl(hdr)[0] &
3713 -                       IEEE80211_QOS_CTL_TID_MASK;
3714 -               tid = ATH_AN_2_TID(txctl->an, tidno);
3715 -
3716 -               WARN_ON(tid->ac->txq != txctl->txq);
3717 -       }
3718 -
3719 -       if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) && tid) {
3720 -               /*
3721 -                * Try aggregation if it's a unicast data frame
3722 -                * and the destination is HT capable.
3723 -                */
3724 -               ath_tx_send_ampdu(sc, tid, skb, txctl);
3725 -       } else {
3726 -               bf = ath_tx_setup_buffer(sc, txctl->txq, tid, skb);
3727 -               if (!bf) {
3728 -                       if (txctl->paprd)
3729 -                               dev_kfree_skb_any(skb);
3730 -                       else
3731 -                               ieee80211_free_txskb(sc->hw, skb);
3732 -                       return;
3733 -               }
3734 -
3735 -               bf->bf_state.bfs_paprd = txctl->paprd;
3736 -
3737 -               if (txctl->paprd)
3738 -                       bf->bf_state.bfs_paprd_timestamp = jiffies;
3739 -
3740 -               ath_tx_send_normal(sc, txctl->txq, tid, skb);
3741 -       }
3742 -}
3743 -
3744  /* Upon failure caller should free skb */
3745  int ath_tx_start(struct ieee80211_hw *hw, struct sk_buff *skb,
3746                  struct ath_tx_control *txctl)
3747 @@ -1945,8 +1910,11 @@ int ath_tx_start(struct ieee80211_hw *hw
3748         struct ieee80211_vif *vif = info->control.vif;
3749         struct ath_softc *sc = hw->priv;
3750         struct ath_txq *txq = txctl->txq;
3751 +       struct ath_atx_tid *tid = NULL;
3752 +       struct ath_buf *bf;
3753         int padpos, padsize;
3754         int frmlen = skb->len + FCS_LEN;
3755 +       u8 tidno;
3756         int q;
3757  
3758         /* NOTE:  sta can be NULL according to net/mac80211.h */
3759 @@ -2002,8 +1970,41 @@ int ath_tx_start(struct ieee80211_hw *hw
3760                 txq->stopped = true;
3761         }
3762  
3763 -       ath_tx_start_dma(sc, skb, txctl);
3764 +       if (txctl->an && ieee80211_is_data_qos(hdr->frame_control)) {
3765 +               tidno = ieee80211_get_qos_ctl(hdr)[0] &
3766 +                       IEEE80211_QOS_CTL_TID_MASK;
3767 +               tid = ATH_AN_2_TID(txctl->an, tidno);
3768 +
3769 +               WARN_ON(tid->ac->txq != txctl->txq);
3770 +       }
3771 +
3772 +       if ((info->flags & IEEE80211_TX_CTL_AMPDU) && tid) {
3773 +               /*
3774 +                * Try aggregation if it's a unicast data frame
3775 +                * and the destination is HT capable.
3776 +                */
3777 +               ath_tx_send_ampdu(sc, tid, skb, txctl);
3778 +               goto out;
3779 +       }
3780 +
3781 +       bf = ath_tx_setup_buffer(sc, txctl->txq, tid, skb);
3782 +       if (!bf) {
3783 +               if (txctl->paprd)
3784 +                       dev_kfree_skb_any(skb);
3785 +               else
3786 +                       ieee80211_free_txskb(sc->hw, skb);
3787 +               goto out;
3788 +       }
3789 +
3790 +       bf->bf_state.bfs_paprd = txctl->paprd;
3791 +
3792 +       if (txctl->paprd)
3793 +               bf->bf_state.bfs_paprd_timestamp = jiffies;
3794 +
3795 +       ath_set_rates(vif, sta, bf);
3796 +       ath_tx_send_normal(sc, txctl->txq, tid, skb);
3797  
3798 +out:
3799         ath_txq_unlock(sc, txq);
3800  
3801         return 0;
3802 --- a/drivers/net/wireless/ath/ath9k/recv.c
3803 +++ b/drivers/net/wireless/ath/ath9k/recv.c
3804 @@ -124,7 +124,7 @@ static bool ath_rx_edma_buf_link(struct 
3805  
3806         SKB_CB_ATHBUF(skb) = bf;
3807         ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
3808 -       skb_queue_tail(&rx_edma->rx_fifo, skb);
3809 +       __skb_queue_tail(&rx_edma->rx_fifo, skb);
3810  
3811         return true;
3812  }
3813 @@ -155,7 +155,7 @@ static void ath_rx_remove_buffer(struct 
3814  
3815         rx_edma = &sc->rx.rx_edma[qtype];
3816  
3817 -       while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
3818 +       while ((skb = __skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
3819                 bf = SKB_CB_ATHBUF(skb);
3820                 BUG_ON(!bf);
3821                 list_add_tail(&bf->list, &sc->rx.rxbuf);
3822 @@ -1287,13 +1287,13 @@ int ath_rx_tasklet(struct ath_softc *sc,
3823                         goto requeue_drop_frag;
3824                 }
3825  
3826 -               bf->bf_mpdu = requeue_skb;
3827 -               bf->bf_buf_addr = new_buf_addr;
3828 -
3829                 /* Unmap the frame */
3830                 dma_unmap_single(sc->dev, bf->bf_buf_addr,
3831                                  common->rx_bufsize, dma_type);
3832  
3833 +               bf->bf_mpdu = requeue_skb;
3834 +               bf->bf_buf_addr = new_buf_addr;
3835 +
3836                 skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
3837                 if (ah->caps.rx_status_len)
3838                         skb_pull(skb, ah->caps.rx_status_len);