ath9k: change the BSSID mask calculation to fix setting arbitrary MAC address on...
[openwrt.git] / package / mac80211 / patches / 300-pending_work.patch
1 --- a/drivers/net/wireless/ath/ath9k/common.h
2 +++ b/drivers/net/wireless/ath/ath9k/common.h
3 @@ -27,7 +27,7 @@
4  #define WME_MAX_BA              WME_BA_BMP_SIZE
5  #define ATH_TID_MAX_BUFS        (2 * WME_MAX_BA)
6  
7 -#define ATH_RSSI_DUMMY_MARKER   0x127
8 +#define ATH_RSSI_DUMMY_MARKER   127
9  #define ATH_RSSI_LPF_LEN               10
10  #define RSSI_LPF_THRESHOLD             -20
11  #define ATH_RSSI_EP_MULTIPLIER     (1<<7)
12 --- a/drivers/net/wireless/ath/ath9k/htc_drv_txrx.c
13 +++ b/drivers/net/wireless/ath/ath9k/htc_drv_txrx.c
14 @@ -1067,15 +1067,19 @@ static bool ath9k_rx_prepare(struct ath9
15  
16         last_rssi = priv->rx.last_rssi;
17  
18 -       if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
19 -               rxbuf->rxstatus.rs_rssi = ATH_EP_RND(last_rssi,
20 -                                                    ATH_RSSI_EP_MULTIPLIER);
21 +       if (ieee80211_is_beacon(hdr->frame_control) &&
22 +           !is_zero_ether_addr(common->curbssid) &&
23 +           ether_addr_equal(hdr->addr3, common->curbssid)) {
24 +               s8 rssi = rxbuf->rxstatus.rs_rssi;
25  
26 -       if (rxbuf->rxstatus.rs_rssi < 0)
27 -               rxbuf->rxstatus.rs_rssi = 0;
28 +               if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
29 +                       rssi = ATH_EP_RND(last_rssi, ATH_RSSI_EP_MULTIPLIER);
30  
31 -       if (ieee80211_is_beacon(fc))
32 -               priv->ah->stats.avgbrssi = rxbuf->rxstatus.rs_rssi;
33 +               if (rssi < 0)
34 +                       rssi = 0;
35 +
36 +               priv->ah->stats.avgbrssi = rssi;
37 +       }
38  
39         rx_status->mactime = be64_to_cpu(rxbuf->rxstatus.rs_tstamp);
40         rx_status->band = hw->conf.channel->band;
41 --- a/include/linux/ieee80211.h
42 +++ b/include/linux/ieee80211.h
43 @@ -185,7 +185,7 @@ struct ieee80211_hdr {
44         u8 addr3[6];
45         __le16 seq_ctrl;
46         u8 addr4[6];
47 -} __packed;
48 +} __packed __aligned(2);
49  
50  struct ieee80211_hdr_3addr {
51         __le16 frame_control;
52 @@ -194,7 +194,7 @@ struct ieee80211_hdr_3addr {
53         u8 addr2[6];
54         u8 addr3[6];
55         __le16 seq_ctrl;
56 -} __packed;
57 +} __packed __aligned(2);
58  
59  struct ieee80211_qos_hdr {
60         __le16 frame_control;
61 @@ -204,7 +204,7 @@ struct ieee80211_qos_hdr {
62         u8 addr3[6];
63         __le16 seq_ctrl;
64         __le16 qos_ctrl;
65 -} __packed;
66 +} __packed __aligned(2);
67  
68  /**
69   * ieee80211_has_tods - check if IEEE80211_FCTL_TODS is set
70 @@ -581,7 +581,7 @@ struct ieee80211s_hdr {
71         __le32 seqnum;
72         u8 eaddr1[6];
73         u8 eaddr2[6];
74 -} __packed;
75 +} __packed __aligned(2);
76  
77  /* Mesh flags */
78  #define MESH_FLAGS_AE_A4       0x1
79 @@ -875,7 +875,7 @@ struct ieee80211_mgmt {
80                         } u;
81                 } __packed action;
82         } u;
83 -} __packed;
84 +} __packed __aligned(2);
85  
86  /* Supported Rates value encodings in 802.11n-2009 7.3.2.2 */
87  #define BSS_MEMBERSHIP_SELECTOR_HT_PHY 127
88 @@ -906,20 +906,20 @@ struct ieee80211_rts {
89         __le16 duration;
90         u8 ra[6];
91         u8 ta[6];
92 -} __packed;
93 +} __packed __aligned(2);
94  
95  struct ieee80211_cts {
96         __le16 frame_control;
97         __le16 duration;
98         u8 ra[6];
99 -} __packed;
100 +} __packed __aligned(2);
101  
102  struct ieee80211_pspoll {
103         __le16 frame_control;
104         __le16 aid;
105         u8 bssid[6];
106         u8 ta[6];
107 -} __packed;
108 +} __packed __aligned(2);
109  
110  /* TDLS */
111  
112 --- a/net/mac80211/agg-rx.c
113 +++ b/net/mac80211/agg-rx.c
114 @@ -204,6 +204,8 @@ static void ieee80211_send_addba_resp(st
115                 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
116         else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
117                 memcpy(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN);
118 +       else if (sdata->vif.type == NL80211_IFTYPE_WDS)
119 +               memcpy(mgmt->bssid, da, ETH_ALEN);
120  
121         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
122                                           IEEE80211_STYPE_ACTION);
123 --- a/net/mac80211/agg-tx.c
124 +++ b/net/mac80211/agg-tx.c
125 @@ -81,7 +81,8 @@ static void ieee80211_send_addba_request
126         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
127         if (sdata->vif.type == NL80211_IFTYPE_AP ||
128             sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
129 -           sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
130 +           sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
131 +           sdata->vif.type == NL80211_IFTYPE_WDS)
132                 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
133         else if (sdata->vif.type == NL80211_IFTYPE_STATION)
134                 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
135 @@ -527,6 +528,7 @@ int ieee80211_start_tx_ba_session(struct
136             sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
137             sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
138             sdata->vif.type != NL80211_IFTYPE_AP &&
139 +           sdata->vif.type != NL80211_IFTYPE_WDS &&
140             sdata->vif.type != NL80211_IFTYPE_ADHOC)
141                 return -EINVAL;
142  
143 --- a/net/mac80211/debugfs_sta.c
144 +++ b/net/mac80211/debugfs_sta.c
145 @@ -65,11 +65,11 @@ static ssize_t sta_flags_read(struct fil
146         test_sta_flag(sta, WLAN_STA_##flg) ? #flg "\n" : ""
147  
148         int res = scnprintf(buf, sizeof(buf),
149 -                           "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
150 +                           "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
151                             TEST(AUTH), TEST(ASSOC), TEST(PS_STA),
152                             TEST(PS_DRIVER), TEST(AUTHORIZED),
153                             TEST(SHORT_PREAMBLE),
154 -                           TEST(WME), TEST(WDS), TEST(CLEAR_PS_FILT),
155 +                           TEST(WME), TEST(CLEAR_PS_FILT),
156                             TEST(MFP), TEST(BLOCK_BA), TEST(PSPOLL),
157                             TEST(UAPSD), TEST(SP), TEST(TDLS_PEER),
158                             TEST(TDLS_PEER_AUTH), TEST(4ADDR_EVENT),
159 --- a/net/mac80211/iface.c
160 +++ b/net/mac80211/iface.c
161 @@ -78,7 +78,7 @@ void ieee80211_recalc_txpower(struct iee
162                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_TXPOWER);
163  }
164  
165 -static u32 ieee80211_idle_off(struct ieee80211_local *local)
166 +u32 ieee80211_idle_off(struct ieee80211_local *local)
167  {
168         if (!(local->hw.conf.flags & IEEE80211_CONF_IDLE))
169                 return 0;
170 @@ -107,7 +107,7 @@ void ieee80211_recalc_idle(struct ieee80
171  
172         lockdep_assert_held(&local->mtx);
173  
174 -       active = !list_empty(&local->chanctx_list);
175 +       active = !list_empty(&local->chanctx_list) || local->monitors;
176  
177         if (!local->ops->remain_on_channel) {
178                 list_for_each_entry(roc, &local->roc_list, list) {
179 @@ -436,7 +436,6 @@ int ieee80211_do_open(struct wireless_de
180         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
181         struct net_device *dev = wdev->netdev;
182         struct ieee80211_local *local = sdata->local;
183 -       struct sta_info *sta;
184         u32 changed = 0;
185         int res;
186         u32 hw_reconf_flags = 0;
187 @@ -541,6 +540,9 @@ int ieee80211_do_open(struct wireless_de
188  
189                 ieee80211_adjust_monitor_flags(sdata, 1);
190                 ieee80211_configure_filter(local);
191 +               mutex_lock(&local->mtx);
192 +               ieee80211_recalc_idle(local);
193 +               mutex_unlock(&local->mtx);
194  
195                 netif_carrier_on(dev);
196                 break;
197 @@ -595,30 +597,8 @@ int ieee80211_do_open(struct wireless_de
198  
199         set_bit(SDATA_STATE_RUNNING, &sdata->state);
200  
201 -       if (sdata->vif.type == NL80211_IFTYPE_WDS) {
202 -               /* Create STA entry for the WDS peer */
203 -               sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
204 -                                    GFP_KERNEL);
205 -               if (!sta) {
206 -                       res = -ENOMEM;
207 -                       goto err_del_interface;
208 -               }
209 -
210 -               sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
211 -               sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
212 -               sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
213 -
214 -               res = sta_info_insert(sta);
215 -               if (res) {
216 -                       /* STA has been freed */
217 -                       goto err_del_interface;
218 -               }
219 -
220 -               rate_control_rate_init(sta);
221 -               netif_carrier_on(dev);
222 -       } else if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
223 +       if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE)
224                 rcu_assign_pointer(local->p2p_sdata, sdata);
225 -       }
226  
227         /*
228          * set_multicast_list will be invoked by the networking core
229 @@ -817,6 +797,9 @@ static void ieee80211_do_stop(struct iee
230  
231                 ieee80211_adjust_monitor_flags(sdata, -1);
232                 ieee80211_configure_filter(local);
233 +               mutex_lock(&local->mtx);
234 +               ieee80211_recalc_idle(local);
235 +               mutex_unlock(&local->mtx);
236                 break;
237         case NL80211_IFTYPE_P2P_DEVICE:
238                 /* relies on synchronize_rcu() below */
239 @@ -1022,6 +1005,72 @@ static void ieee80211_if_setup(struct ne
240         dev->destructor = free_netdev;
241  }
242  
243 +static void ieee80211_wds_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
244 +                                        struct sk_buff *skb)
245 +{
246 +       struct ieee80211_local *local = sdata->local;
247 +       struct ieee80211_rx_status *rx_status;
248 +       struct ieee802_11_elems elems;
249 +       struct ieee80211_mgmt *mgmt;
250 +       struct sta_info *sta;
251 +       size_t baselen;
252 +       u32 rates = 0;
253 +       u16 stype;
254 +       bool new = false;
255 +       enum ieee80211_band band = local->hw.conf.channel->band;
256 +       struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
257 +
258 +       rx_status = IEEE80211_SKB_RXCB(skb);
259 +       mgmt = (struct ieee80211_mgmt *) skb->data;
260 +       stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
261 +
262 +       if (stype != IEEE80211_STYPE_BEACON)
263 +               return;
264 +
265 +       baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
266 +       if (baselen > skb->len)
267 +               return;
268 +
269 +       ieee802_11_parse_elems(mgmt->u.probe_resp.variable,
270 +                              skb->len - baselen, &elems);
271 +
272 +       rates = ieee80211_sta_get_rates(local, &elems, band, NULL);
273 +
274 +       rcu_read_lock();
275 +
276 +       sta = sta_info_get(sdata, sdata->u.wds.remote_addr);
277 +
278 +       if (!sta) {
279 +               rcu_read_unlock();
280 +               sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
281 +                                    GFP_KERNEL);
282 +               if (!sta)
283 +                       return;
284 +
285 +               new = true;
286 +       }
287 +
288 +       sta->last_rx = jiffies;
289 +       sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
290 +
291 +       if (elems.ht_cap_elem)
292 +               ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
293 +                               elems.ht_cap_elem, sta);
294 +
295 +       if (elems.wmm_param)
296 +               set_sta_flag(sta, WLAN_STA_WME);
297 +
298 +       if (new) {
299 +               sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
300 +               sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
301 +               sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
302 +               rate_control_rate_init(sta);
303 +               sta_info_insert_rcu(sta);
304 +       }
305 +
306 +       rcu_read_unlock();
307 +}
308 +
309  static void ieee80211_iface_work(struct work_struct *work)
310  {
311         struct ieee80211_sub_if_data *sdata =
312 @@ -1126,6 +1175,9 @@ static void ieee80211_iface_work(struct 
313                                 break;
314                         ieee80211_mesh_rx_queued_mgmt(sdata, skb);
315                         break;
316 +               case NL80211_IFTYPE_WDS:
317 +                       ieee80211_wds_rx_queued_mgmt(sdata, skb);
318 +                       break;
319                 default:
320                         WARN(1, "frame for unexpected interface type");
321                         break;
322 --- a/net/mac80211/rx.c
323 +++ b/net/mac80211/rx.c
324 @@ -2365,6 +2365,7 @@ ieee80211_rx_h_action(struct ieee80211_r
325                     sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
326                     sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
327                     sdata->vif.type != NL80211_IFTYPE_AP &&
328 +                   sdata->vif.type != NL80211_IFTYPE_WDS &&
329                     sdata->vif.type != NL80211_IFTYPE_ADHOC)
330                         break;
331  
332 @@ -2692,14 +2693,15 @@ ieee80211_rx_h_mgmt(struct ieee80211_rx_
333  
334         if (!ieee80211_vif_is_mesh(&sdata->vif) &&
335             sdata->vif.type != NL80211_IFTYPE_ADHOC &&
336 -           sdata->vif.type != NL80211_IFTYPE_STATION)
337 +           sdata->vif.type != NL80211_IFTYPE_STATION &&
338 +           sdata->vif.type != NL80211_IFTYPE_WDS)
339                 return RX_DROP_MONITOR;
340  
341         switch (stype) {
342         case cpu_to_le16(IEEE80211_STYPE_AUTH):
343         case cpu_to_le16(IEEE80211_STYPE_BEACON):
344         case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
345 -               /* process for all: mesh, mlme, ibss */
346 +               /* process for all: mesh, mlme, ibss, wds */
347                 break;
348         case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
349         case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
350 @@ -3028,10 +3030,16 @@ static int prepare_for_handlers(struct i
351                 }
352                 break;
353         case NL80211_IFTYPE_WDS:
354 -               if (bssid || !ieee80211_is_data(hdr->frame_control))
355 -                       return 0;
356                 if (!ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2))
357                         return 0;
358 +
359 +               if (ieee80211_is_data(hdr->frame_control) ||
360 +                   ieee80211_is_action(hdr->frame_control)) {
361 +                       if (compare_ether_addr(sdata->vif.addr, hdr->addr1))
362 +                               return 0;
363 +               } else if (!ieee80211_is_beacon(hdr->frame_control))
364 +                       return 0;
365 +
366                 break;
367         case NL80211_IFTYPE_P2P_DEVICE:
368                 if (!ieee80211_is_public_action(hdr, skb->len) &&
369 --- a/net/mac80211/sta_info.h
370 +++ b/net/mac80211/sta_info.h
371 @@ -32,7 +32,6 @@
372   * @WLAN_STA_SHORT_PREAMBLE: Station is capable of receiving short-preamble
373   *     frames.
374   * @WLAN_STA_WME: Station is a QoS-STA.
375 - * @WLAN_STA_WDS: Station is one of our WDS peers.
376   * @WLAN_STA_CLEAR_PS_FILT: Clear PS filter in hardware (using the
377   *     IEEE80211_TX_CTL_CLEAR_PS_FILT control flag) when the next
378   *     frame to this station is transmitted.
379 @@ -66,7 +65,6 @@ enum ieee80211_sta_info_flags {
380         WLAN_STA_AUTHORIZED,
381         WLAN_STA_SHORT_PREAMBLE,
382         WLAN_STA_WME,
383 -       WLAN_STA_WDS,
384         WLAN_STA_CLEAR_PS_FILT,
385         WLAN_STA_MFP,
386         WLAN_STA_BLOCK_BA,
387 --- a/net/mac80211/tx.c
388 +++ b/net/mac80211/tx.c
389 @@ -1231,34 +1231,40 @@ static bool ieee80211_tx_frags(struct ie
390                 if (local->queue_stop_reasons[q] ||
391                     (!txpending && !skb_queue_empty(&local->pending[q]))) {
392                         if (unlikely(info->flags &
393 -                                       IEEE80211_TX_INTFL_OFFCHAN_TX_OK &&
394 -                                    local->queue_stop_reasons[q] &
395 -                                       ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL))) {
396 +                                    IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
397 +                               if (local->queue_stop_reasons[q] &
398 +                                   ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
399 +                                       /*
400 +                                        * Drop off-channel frames if queues
401 +                                        * are stopped for any reason other
402 +                                        * than off-channel operation. Never
403 +                                        * queue them.
404 +                                        */
405 +                                       spin_unlock_irqrestore(
406 +                                               &local->queue_stop_reason_lock,
407 +                                               flags);
408 +                                       ieee80211_purge_tx_queue(&local->hw,
409 +                                                                skbs);
410 +                                       return true;
411 +                               }
412 +                       } else {
413 +
414                                 /*
415 -                                * Drop off-channel frames if queues are stopped
416 -                                * for any reason other than off-channel
417 -                                * operation. Never queue them.
418 +                                * Since queue is stopped, queue up frames for
419 +                                * later transmission from the tx-pending
420 +                                * tasklet when the queue is woken again.
421                                  */
422 -                               spin_unlock_irqrestore(
423 -                                       &local->queue_stop_reason_lock, flags);
424 -                               ieee80211_purge_tx_queue(&local->hw, skbs);
425 -                               return true;
426 +                               if (txpending)
427 +                                       skb_queue_splice_init(skbs,
428 +                                                             &local->pending[q]);
429 +                               else
430 +                                       skb_queue_splice_tail_init(skbs,
431 +                                                                  &local->pending[q]);
432 +
433 +                               spin_unlock_irqrestore(&local->queue_stop_reason_lock,
434 +                                                      flags);
435 +                               return false;
436                         }
437 -
438 -                       /*
439 -                        * Since queue is stopped, queue up frames for later
440 -                        * transmission from the tx-pending tasklet when the
441 -                        * queue is woken again.
442 -                        */
443 -                       if (txpending)
444 -                               skb_queue_splice_init(skbs, &local->pending[q]);
445 -                       else
446 -                               skb_queue_splice_tail_init(skbs,
447 -                                                          &local->pending[q]);
448 -
449 -                       spin_unlock_irqrestore(&local->queue_stop_reason_lock,
450 -                                              flags);
451 -                       return false;
452                 }
453                 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
454  
455 @@ -1848,9 +1854,24 @@ netdev_tx_t ieee80211_subif_start_xmit(s
456                 }
457  
458                 if (!is_multicast_ether_addr(skb->data)) {
459 +                       struct sta_info *next_hop;
460 +                       bool mpp_lookup = true;
461 +
462                         mpath = mesh_path_lookup(sdata, skb->data);
463 -                       if (!mpath)
464 +                       if (mpath) {
465 +                               mpp_lookup = false;
466 +                               next_hop = rcu_dereference(mpath->next_hop);
467 +                               if (!next_hop ||
468 +                                   !(mpath->flags & (MESH_PATH_ACTIVE |
469 +                                                     MESH_PATH_RESOLVING)))
470 +                                       mpp_lookup = true;
471 +                       }
472 +
473 +                       if (mpp_lookup)
474                                 mppath = mpp_path_lookup(sdata, skb->data);
475 +
476 +                       if (mppath && mpath)
477 +                               mesh_path_del(mpath->sdata, mpath->dst);
478                 }
479  
480                 /*
481 --- a/net/wireless/nl80211.c
482 +++ b/net/wireless/nl80211.c
483 @@ -554,16 +554,9 @@ static int nl80211_msg_put_channel(struc
484         if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
485             nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
486                 goto nla_put_failure;
487 -       if (chan->flags & IEEE80211_CHAN_RADAR) {
488 -               u32 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
489 -               if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
490 -                       goto nla_put_failure;
491 -               if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
492 -                               chan->dfs_state))
493 -                       goto nla_put_failure;
494 -               if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME, time))
495 -                       goto nla_put_failure;
496 -       }
497 +       if ((chan->flags & IEEE80211_CHAN_RADAR) &&
498 +           nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
499 +               goto nla_put_failure;
500         if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
501             nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
502                 goto nla_put_failure;
503 @@ -900,9 +893,6 @@ static int nl80211_put_iface_combination
504                     nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
505                                 c->max_interfaces))
506                         goto nla_put_failure;
507 -               if (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
508 -                               c->radar_detect_widths))
509 -                       goto nla_put_failure;
510  
511                 nla_nest_end(msg, nl_combi);
512         }
513 @@ -914,48 +904,6 @@ nla_put_failure:
514         return -ENOBUFS;
515  }
516  
517 -#ifdef CONFIG_PM
518 -static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
519 -                                       struct sk_buff *msg)
520 -{
521 -       const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan.tcp;
522 -       struct nlattr *nl_tcp;
523 -
524 -       if (!tcp)
525 -               return 0;
526 -
527 -       nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
528 -       if (!nl_tcp)
529 -               return -ENOBUFS;
530 -
531 -       if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
532 -                       tcp->data_payload_max))
533 -               return -ENOBUFS;
534 -
535 -       if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
536 -                       tcp->data_payload_max))
537 -               return -ENOBUFS;
538 -
539 -       if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
540 -               return -ENOBUFS;
541 -
542 -       if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
543 -                               sizeof(*tcp->tok), tcp->tok))
544 -               return -ENOBUFS;
545 -
546 -       if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
547 -                       tcp->data_interval_max))
548 -               return -ENOBUFS;
549 -
550 -       if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
551 -                       tcp->wake_payload_max))
552 -               return -ENOBUFS;
553 -
554 -       nla_nest_end(msg, nl_tcp);
555 -       return 0;
556 -}
557 -#endif
558 -
559  static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
560                               struct cfg80211_registered_device *dev)
561  {
562 @@ -1330,9 +1278,6 @@ static int nl80211_send_wiphy(struct sk_
563                                 goto nla_put_failure;
564                 }
565  
566 -               if (nl80211_send_wowlan_tcp_caps(dev, msg))
567 -                       goto nla_put_failure;
568 -
569                 nla_nest_end(msg, nl_wowlan);
570         }
571  #endif
572 --- a/net/mac80211/cfg.c
573 +++ b/net/mac80211/cfg.c
574 @@ -3285,13 +3285,19 @@ static int ieee80211_cfg_get_channel(str
575                                      struct cfg80211_chan_def *chandef)
576  {
577         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
578 +       struct ieee80211_local *local = wiphy_priv(wiphy);
579         struct ieee80211_chanctx_conf *chanctx_conf;
580         int ret = -ENODATA;
581  
582         rcu_read_lock();
583 -       chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
584 -       if (chanctx_conf) {
585 -               *chandef = chanctx_conf->def;
586 +       if (local->use_chanctx) {
587 +               chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
588 +               if (chanctx_conf) {
589 +                       *chandef = chanctx_conf->def;
590 +                       ret = 0;
591 +               }
592 +       } else if (local->open_count == local->monitors) {
593 +               *chandef = local->monitor_chandef;
594                 ret = 0;
595         }
596         rcu_read_unlock();
597 --- a/drivers/net/wireless/ath/ath9k/hw.c
598 +++ b/drivers/net/wireless/ath/ath9k/hw.c
599 @@ -1463,7 +1463,9 @@ static bool ath9k_hw_chip_reset(struct a
600                         reset_type = ATH9K_RESET_POWER_ON;
601                 else
602                         reset_type = ATH9K_RESET_COLD;
603 -       }
604 +       } else if (ah->chip_fullsleep || REG_READ(ah, AR_Q_TXE) ||
605 +                  (REG_READ(ah, AR_CR) & AR_CR_RXE))
606 +               reset_type = ATH9K_RESET_COLD;
607  
608         if (!ath9k_hw_set_reset_reg(ah, reset_type))
609                 return false;
610 @@ -1876,13 +1878,12 @@ int ath9k_hw_reset(struct ath_hw *ah, st
611  
612         ENABLE_REGWRITE_BUFFER(ah);
613  
614 -       REG_WRITE(ah, AR_STA_ID0, get_unaligned_le32(common->macaddr));
615 -       REG_WRITE(ah, AR_STA_ID1, get_unaligned_le16(common->macaddr + 4)
616 -                 | macStaId1
617 +       REG_RMW(ah, AR_STA_ID1, macStaId1
618                   | AR_STA_ID1_RTS_USE_DEF
619                   | (ah->config.
620                      ack_6mb ? AR_STA_ID1_ACKCTS_6MB : 0)
621 -                 | ah->sta_id1_defaults);
622 +                 | ah->sta_id1_defaults,
623 +                 ~AR_STA_ID1_SADH_MASK);
624         ath_hw_setbssidmask(common);
625         REG_WRITE(ah, AR_DEF_ANTENNA, saveDefAntenna);
626         ath9k_hw_write_associd(ah);
627 --- a/drivers/net/wireless/rt2x00/rt2x00dev.c
628 +++ b/drivers/net/wireless/rt2x00/rt2x00dev.c
629 @@ -1236,8 +1236,10 @@ static inline void rt2x00lib_set_if_comb
630          */
631         if_limit = &rt2x00dev->if_limits_ap;
632         if_limit->max = rt2x00dev->ops->max_ap_intf;
633 -       if_limit->types = BIT(NL80211_IFTYPE_AP) |
634 -                       BIT(NL80211_IFTYPE_MESH_POINT);
635 +       if_limit->types = BIT(NL80211_IFTYPE_AP);
636 +#ifdef CONFIG_MAC80211_MESH
637 +       if_limit->types |= BIT(NL80211_IFTYPE_MESH_POINT);
638 +#endif
639  
640         /*
641          * Build up AP interface combinations structure.
642 @@ -1309,7 +1311,9 @@ int rt2x00lib_probe_dev(struct rt2x00_de
643                 rt2x00dev->hw->wiphy->interface_modes |=
644                     BIT(NL80211_IFTYPE_ADHOC) |
645                     BIT(NL80211_IFTYPE_AP) |
646 +#ifdef CONFIG_MAC80211_MESH
647                     BIT(NL80211_IFTYPE_MESH_POINT) |
648 +#endif
649                     BIT(NL80211_IFTYPE_WDS);
650  
651         rt2x00dev->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
652 --- a/net/mac80211/rc80211_minstrel_ht.c
653 +++ b/net/mac80211/rc80211_minstrel_ht.c
654 @@ -17,8 +17,6 @@
655  #include "rc80211_minstrel_ht.h"
656  
657  #define AVG_PKT_SIZE   1200
658 -#define SAMPLE_COLUMNS 10
659 -#define EWMA_LEVEL             75
660  
661  /* Number of bits for an average sized packet */
662  #define MCS_NBITS (AVG_PKT_SIZE << 3)
663 @@ -26,11 +24,11 @@
664  /* Number of symbols for a packet with (bps) bits per symbol */
665  #define MCS_NSYMS(bps) ((MCS_NBITS + (bps) - 1) / (bps))
666  
667 -/* Transmission time for a packet containing (syms) symbols */
668 +/* Transmission time (nanoseconds) for a packet containing (syms) symbols */
669  #define MCS_SYMBOL_TIME(sgi, syms)                                     \
670         (sgi ?                                                          \
671 -         ((syms) * 18 + 4) / 5 :       /* syms * 3.6 us */             \
672 -         (syms) << 2                   /* syms * 4 us */               \
673 +         ((syms) * 18000 + 4000) / 5 : /* syms * 3.6 us */             \
674 +         ((syms) * 1000) << 2          /* syms * 4 us */               \
675         )
676  
677  /* Transmit duration for the raw data part of an average sized packet */
678 @@ -64,9 +62,9 @@
679  }
680  
681  #define CCK_DURATION(_bitrate, _short, _len)           \
682 -       (10 /* SIFS */ +                                \
683 +       (1000 * (10 /* SIFS */ +                        \
684          (_short ? 72 + 24 : 144 + 48 ) +               \
685 -        (8 * (_len + 4) * 10) / (_bitrate))
686 +        (8 * (_len + 4) * 10) / (_bitrate)))
687  
688  #define CCK_ACK_DURATION(_bitrate, _short)                     \
689         (CCK_DURATION((_bitrate > 10 ? 20 : 10), false, 60) +   \
690 @@ -129,15 +127,6 @@ const struct mcs_group minstrel_mcs_grou
691  static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES];
692  
693  /*
694 - * Perform EWMA (Exponentially Weighted Moving Average) calculation
695 - */
696 -static int
697 -minstrel_ewma(int old, int new, int weight)
698 -{
699 -       return (new * (100 - weight) + old * weight) / 100;
700 -}
701 -
702 -/*
703   * Look up an MCS group index based on mac80211 rate information
704   */
705  static int
706 @@ -211,20 +200,32 @@ static void
707  minstrel_ht_calc_tp(struct minstrel_ht_sta *mi, int group, int rate)
708  {
709         struct minstrel_rate_stats *mr;
710 -       unsigned int usecs = 0;
711 +       unsigned int nsecs = 0;
712 +       unsigned int tp;
713 +       unsigned int prob;
714  
715         mr = &mi->groups[group].rates[rate];
716 +       prob = mr->probability;
717  
718 -       if (mr->probability < MINSTREL_FRAC(1, 10)) {
719 +       if (prob < MINSTREL_FRAC(1, 10)) {
720                 mr->cur_tp = 0;
721                 return;
722         }
723  
724 +       /*
725 +        * For the throughput calculation, limit the probability value to 90% to
726 +        * account for collision related packet error rate fluctuation
727 +        */
728 +       if (prob > MINSTREL_FRAC(9, 10))
729 +               prob = MINSTREL_FRAC(9, 10);
730 +
731         if (group != MINSTREL_CCK_GROUP)
732 -               usecs = mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len);
733 +               nsecs = 1000 * mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len);
734 +
735 +       nsecs += minstrel_mcs_groups[group].duration[rate];
736 +       tp = 1000000 * ((mr->probability * 1000) / nsecs);
737  
738 -       usecs += minstrel_mcs_groups[group].duration[rate];
739 -       mr->cur_tp = MINSTREL_TRUNC((1000000 / usecs) * mr->probability);
740 +       mr->cur_tp = MINSTREL_TRUNC(tp);
741  }
742  
743  /*
744 @@ -308,8 +309,8 @@ minstrel_ht_update_stats(struct minstrel
745                 }
746         }
747  
748 -       /* try to sample up to half of the available rates during each interval */
749 -       mi->sample_count *= 4;
750 +       /* try to sample all available rates during each interval */
751 +       mi->sample_count *= 8;
752  
753         cur_prob = 0;
754         cur_prob_tp = 0;
755 @@ -320,20 +321,13 @@ minstrel_ht_update_stats(struct minstrel
756                 if (!mg->supported)
757                         continue;
758  
759 -               mr = minstrel_get_ratestats(mi, mg->max_prob_rate);
760 -               if (cur_prob_tp < mr->cur_tp &&
761 -                   minstrel_mcs_groups[group].streams == 1) {
762 -                       mi->max_prob_rate = mg->max_prob_rate;
763 -                       cur_prob = mr->cur_prob;
764 -                       cur_prob_tp = mr->cur_tp;
765 -               }
766 -
767                 mr = minstrel_get_ratestats(mi, mg->max_tp_rate);
768                 if (cur_tp < mr->cur_tp) {
769                         mi->max_tp_rate2 = mi->max_tp_rate;
770                         cur_tp2 = cur_tp;
771                         mi->max_tp_rate = mg->max_tp_rate;
772                         cur_tp = mr->cur_tp;
773 +                       mi->max_prob_streams = minstrel_mcs_groups[group].streams - 1;
774                 }
775  
776                 mr = minstrel_get_ratestats(mi, mg->max_tp_rate2);
777 @@ -343,6 +337,23 @@ minstrel_ht_update_stats(struct minstrel
778                 }
779         }
780  
781 +       if (mi->max_prob_streams < 1)
782 +               mi->max_prob_streams = 1;
783 +
784 +       for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
785 +               mg = &mi->groups[group];
786 +               if (!mg->supported)
787 +                       continue;
788 +               mr = minstrel_get_ratestats(mi, mg->max_prob_rate);
789 +               if (cur_prob_tp < mr->cur_tp &&
790 +                   minstrel_mcs_groups[group].streams <= mi->max_prob_streams) {
791 +                       mi->max_prob_rate = mg->max_prob_rate;
792 +                       cur_prob = mr->cur_prob;
793 +                       cur_prob_tp = mr->cur_tp;
794 +               }
795 +       }
796 +
797 +
798         mi->stats_update = jiffies;
799  }
800  
801 @@ -467,7 +478,7 @@ minstrel_ht_tx_status(void *priv, struct
802  
803         if (!mi->sample_wait && !mi->sample_tries && mi->sample_count > 0) {
804                 mi->sample_wait = 16 + 2 * MINSTREL_TRUNC(mi->avg_ampdu_len);
805 -               mi->sample_tries = 2;
806 +               mi->sample_tries = 1;
807                 mi->sample_count--;
808         }
809  
810 @@ -536,7 +547,7 @@ minstrel_calc_retransmit(struct minstrel
811         mr->retry_updated = true;
812  
813         group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
814 -       tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len;
815 +       tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len / 1000;
816  
817         /* Contention time for first 2 tries */
818         ctime = (t_slot * cw) >> 1;
819 @@ -616,6 +627,7 @@ minstrel_get_sample_rate(struct minstrel
820  {
821         struct minstrel_rate_stats *mr;
822         struct minstrel_mcs_group_data *mg;
823 +       unsigned int sample_dur, sample_group;
824         int sample_idx = 0;
825  
826         if (mi->sample_wait > 0) {
827 @@ -626,39 +638,46 @@ minstrel_get_sample_rate(struct minstrel
828         if (!mi->sample_tries)
829                 return -1;
830  
831 -       mi->sample_tries--;
832         mg = &mi->groups[mi->sample_group];
833         sample_idx = sample_table[mg->column][mg->index];
834         mr = &mg->rates[sample_idx];
835 -       sample_idx += mi->sample_group * MCS_GROUP_RATES;
836 +       sample_group = mi->sample_group;
837 +       sample_idx += sample_group * MCS_GROUP_RATES;
838         minstrel_next_sample_idx(mi);
839  
840         /*
841          * Sampling might add some overhead (RTS, no aggregation)
842          * to the frame. Hence, don't use sampling for the currently
843 -        * used max TP rate.
844 +        * used rates.
845          */
846 -       if (sample_idx == mi->max_tp_rate)
847 +       if (sample_idx == mi->max_tp_rate ||
848 +           sample_idx == mi->max_tp_rate2 ||
849 +           sample_idx == mi->max_prob_rate)
850                 return -1;
851 +
852         /*
853 -        * When not using MRR, do not sample if the probability is already
854 -        * higher than 95% to avoid wasting airtime
855 +        * Do not sample if the probability is already higher than 95%
856 +        * to avoid wasting airtime.
857          */
858 -       if (!mp->has_mrr && (mr->probability > MINSTREL_FRAC(95, 100)))
859 +       if (mr->probability > MINSTREL_FRAC(95, 100))
860                 return -1;
861  
862         /*
863          * Make sure that lower rates get sampled only occasionally,
864          * if the link is working perfectly.
865          */
866 -       if (minstrel_get_duration(sample_idx) >
867 -           minstrel_get_duration(mi->max_tp_rate)) {
868 +       sample_dur = minstrel_get_duration(sample_idx);
869 +       if (sample_dur >= minstrel_get_duration(mi->max_tp_rate2) &&
870 +           (mi->max_prob_streams <
871 +            minstrel_mcs_groups[sample_group].streams ||
872 +            sample_dur >= minstrel_get_duration(mi->max_prob_rate))) {
873                 if (mr->sample_skipped < 20)
874                         return -1;
875  
876                 if (mi->sample_slow++ > 2)
877                         return -1;
878         }
879 +       mi->sample_tries--;
880  
881         return sample_idx;
882  }
883 --- a/net/mac80211/rc80211_minstrel_ht.h
884 +++ b/net/mac80211/rc80211_minstrel_ht.h
885 @@ -16,11 +16,6 @@
886  #define MINSTREL_MAX_STREAMS   3
887  #define MINSTREL_STREAM_GROUPS 4
888  
889 -/* scaled fraction values */
890 -#define MINSTREL_SCALE 16
891 -#define MINSTREL_FRAC(val, div) (((val) << MINSTREL_SCALE) / div)
892 -#define MINSTREL_TRUNC(val) ((val) >> MINSTREL_SCALE)
893 -
894  #define MCS_GROUP_RATES        8
895  
896  struct mcs_group {
897 @@ -85,6 +80,7 @@ struct minstrel_ht_sta {
898  
899         /* best probability rate */
900         unsigned int max_prob_rate;
901 +       unsigned int max_prob_streams;
902  
903         /* time of last status update */
904         unsigned long stats_update;
905 --- a/drivers/net/wireless/ath/ath9k/ar9003_calib.c
906 +++ b/drivers/net/wireless/ath/ath9k/ar9003_calib.c
907 @@ -1023,6 +1023,7 @@ static bool ar9003_hw_init_cal(struct at
908                                           AR_PHY_AGC_CONTROL_FLTR_CAL   |
909                                           AR_PHY_AGC_CONTROL_PKDET_CAL;
910  
911 +       /* Use chip chainmask only for calibration */
912         ar9003_hw_set_chain_masks(ah, ah->caps.rx_chainmask, ah->caps.tx_chainmask);
913  
914         if (rtt) {
915 @@ -1150,6 +1151,9 @@ skip_tx_iqcal:
916                 ar9003_hw_rtt_disable(ah);
917         }
918  
919 +       /* Revert chainmask to runtime parameters */
920 +       ar9003_hw_set_chain_masks(ah, ah->rxchainmask, ah->txchainmask);
921 +
922         /* Initialize list pointers */
923         ah->cal_list = ah->cal_list_last = ah->cal_list_curr = NULL;
924  
925 --- a/drivers/net/wireless/ath/ath9k/ar9003_eeprom.c
926 +++ b/drivers/net/wireless/ath/ath9k/ar9003_eeprom.c
927 @@ -3606,6 +3606,12 @@ static void ar9003_hw_ant_ctrl_apply(str
928         value = ar9003_hw_ant_ctrl_common_2_get(ah, is2ghz);
929         REG_RMW_FIELD(ah, AR_PHY_SWITCH_COM_2, AR_SWITCH_TABLE_COM2_ALL, value);
930  
931 +       if ((AR_SREV_9462(ah)) && (ah->rxchainmask == 0x2)) {
932 +               value = ar9003_hw_ant_ctrl_chain_get(ah, 1, is2ghz);
933 +               REG_RMW_FIELD(ah, switch_chain_reg[0],
934 +                             AR_SWITCH_TABLE_ALL, value);
935 +       }
936 +
937         for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
938                 if ((ah->rxchainmask & BIT(chain)) ||
939                     (ah->txchainmask & BIT(chain))) {
940 @@ -3772,6 +3778,17 @@ static void ar9003_hw_atten_apply(struct
941                                           AR_PHY_EXT_ATTEN_CTL_2,
942                                          };
943  
944 +       if ((AR_SREV_9462(ah)) && (ah->rxchainmask == 0x2)) {
945 +               value = ar9003_hw_atten_chain_get(ah, 1, chan);
946 +               REG_RMW_FIELD(ah, ext_atten_reg[0],
947 +                             AR_PHY_EXT_ATTEN_CTL_XATTEN1_DB, value);
948 +
949 +               value = ar9003_hw_atten_chain_get_margin(ah, 1, chan);
950 +               REG_RMW_FIELD(ah, ext_atten_reg[0],
951 +                             AR_PHY_EXT_ATTEN_CTL_XATTEN1_MARGIN,
952 +                             value);
953 +       }
954 +
955         /* Test value. if 0 then attenuation is unused. Don't load anything. */
956         for (i = 0; i < 3; i++) {
957                 if (ah->txchainmask & BIT(i)) {
958 --- a/drivers/net/wireless/ath/ath9k/link.c
959 +++ b/drivers/net/wireless/ath/ath9k/link.c
960 @@ -28,21 +28,21 @@ void ath_tx_complete_poll_work(struct wo
961         int i;
962         bool needreset = false;
963  
964 -       for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
965 -               if (ATH_TXQ_SETUP(sc, i)) {
966 -                       txq = &sc->tx.txq[i];
967 -                       ath_txq_lock(sc, txq);
968 -                       if (txq->axq_depth) {
969 -                               if (txq->axq_tx_inprogress) {
970 -                                       needreset = true;
971 -                                       ath_txq_unlock(sc, txq);
972 -                                       break;
973 -                               } else {
974 -                                       txq->axq_tx_inprogress = true;
975 -                               }
976 +       for (i = 0; i < IEEE80211_NUM_ACS; i++) {
977 +               txq = sc->tx.txq_map[i];
978 +
979 +               ath_txq_lock(sc, txq);
980 +               if (txq->axq_depth) {
981 +                       if (txq->axq_tx_inprogress) {
982 +                               needreset = true;
983 +                               ath_txq_unlock(sc, txq);
984 +                               break;
985 +                       } else {
986 +                               txq->axq_tx_inprogress = true;
987                         }
988 -                       ath_txq_unlock_complete(sc, txq);
989                 }
990 +               ath_txq_unlock_complete(sc, txq);
991 +       }
992  
993         if (needreset) {
994                 ath_dbg(ath9k_hw_common(sc->sc_ah), RESET,
995 --- a/net/mac80211/sta_info.c
996 +++ b/net/mac80211/sta_info.c
997 @@ -766,6 +766,7 @@ int __must_check __sta_info_destroy(stru
998         struct ieee80211_local *local;
999         struct ieee80211_sub_if_data *sdata;
1000         int ret, i;
1001 +       bool have_key = false;
1002  
1003         might_sleep();
1004  
1005 @@ -793,12 +794,19 @@ int __must_check __sta_info_destroy(stru
1006         list_del_rcu(&sta->list);
1007  
1008         mutex_lock(&local->key_mtx);
1009 -       for (i = 0; i < NUM_DEFAULT_KEYS; i++)
1010 +       for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1011                 __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
1012 -       if (sta->ptk)
1013 +               have_key = true;
1014 +       }
1015 +       if (sta->ptk) {
1016                 __ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
1017 +               have_key = true;
1018 +       }
1019         mutex_unlock(&local->key_mtx);
1020  
1021 +       if (!have_key)
1022 +               synchronize_net();
1023 +
1024         sta->dead = true;
1025  
1026         local->num_sta--;
1027 --- a/net/mac80211/chan.c
1028 +++ b/net/mac80211/chan.c
1029 @@ -63,6 +63,7 @@ ieee80211_new_chanctx(struct ieee80211_l
1030                       enum ieee80211_chanctx_mode mode)
1031  {
1032         struct ieee80211_chanctx *ctx;
1033 +       u32 changed;
1034         int err;
1035  
1036         lockdep_assert_held(&local->chanctx_mtx);
1037 @@ -76,6 +77,13 @@ ieee80211_new_chanctx(struct ieee80211_l
1038         ctx->conf.rx_chains_dynamic = 1;
1039         ctx->mode = mode;
1040  
1041 +       /* acquire mutex to prevent idle from changing */
1042 +       mutex_lock(&local->mtx);
1043 +       /* turn idle off *before* setting channel -- some drivers need that */
1044 +       changed = ieee80211_idle_off(local);
1045 +       if (changed)
1046 +               ieee80211_hw_config(local, changed);
1047 +
1048         if (!local->use_chanctx) {
1049                 local->_oper_channel_type =
1050                         cfg80211_get_chandef_type(chandef);
1051 @@ -85,14 +93,17 @@ ieee80211_new_chanctx(struct ieee80211_l
1052                 err = drv_add_chanctx(local, ctx);
1053                 if (err) {
1054                         kfree(ctx);
1055 -                       return ERR_PTR(err);
1056 +                       ctx = ERR_PTR(err);
1057 +
1058 +                       ieee80211_recalc_idle(local);
1059 +                       goto out;
1060                 }
1061         }
1062  
1063 +       /* and keep the mutex held until the new chanctx is on the list */
1064         list_add_rcu(&ctx->list, &local->chanctx_list);
1065  
1066 -       mutex_lock(&local->mtx);
1067 -       ieee80211_recalc_idle(local);
1068 + out:
1069         mutex_unlock(&local->mtx);
1070  
1071         return ctx;
1072 --- a/net/mac80211/ieee80211_i.h
1073 +++ b/net/mac80211/ieee80211_i.h
1074 @@ -1366,6 +1366,7 @@ int ieee80211_if_change_type(struct ieee
1075                              enum nl80211_iftype type);
1076  void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata);
1077  void ieee80211_remove_interfaces(struct ieee80211_local *local);
1078 +u32 ieee80211_idle_off(struct ieee80211_local *local);
1079  void ieee80211_recalc_idle(struct ieee80211_local *local);
1080  void ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data *sdata,
1081                                     const int offset);
1082 --- a/drivers/net/wireless/ath/ath9k/htc_drv_init.c
1083 +++ b/drivers/net/wireless/ath/ath9k/htc_drv_init.c
1084 @@ -799,7 +799,7 @@ static int ath9k_init_firmware_version(s
1085          * required version.
1086          */
1087         if (priv->fw_version_major != MAJOR_VERSION_REQ ||
1088 -           priv->fw_version_minor != MINOR_VERSION_REQ) {
1089 +           priv->fw_version_minor < MINOR_VERSION_REQ) {
1090                 dev_err(priv->dev, "ath9k_htc: Please upgrade to FW version %d.%d\n",
1091                         MAJOR_VERSION_REQ, MINOR_VERSION_REQ);
1092                 return -EINVAL;
1093 --- a/drivers/net/wireless/ath/ath9k/xmit.c
1094 +++ b/drivers/net/wireless/ath/ath9k/xmit.c
1095 @@ -516,8 +516,7 @@ static void ath_tx_complete_aggr(struct 
1096                  * not a holding desc.
1097                  */
1098                 INIT_LIST_HEAD(&bf_head);
1099 -               if ((sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) ||
1100 -                   bf_next != NULL || !bf_last->bf_stale)
1101 +               if (bf_next != NULL || !bf_last->bf_stale)
1102                         list_move_tail(&bf->list, &bf_head);
1103  
1104                 if (!txpending || (tid->state & AGGR_CLEANUP)) {
1105 @@ -537,8 +536,7 @@ static void ath_tx_complete_aggr(struct 
1106                                 !txfail);
1107                 } else {
1108                         /* retry the un-acked ones */
1109 -                       if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
1110 -                           bf->bf_next == NULL && bf_last->bf_stale) {
1111 +                       if (bf->bf_next == NULL && bf_last->bf_stale) {
1112                                 struct ath_buf *tbf;
1113  
1114                                 tbf = ath_clone_txbuf(sc, bf_last);
1115 @@ -2264,6 +2262,7 @@ void ath_tx_edma_tasklet(struct ath_soft
1116         struct ath_txq *txq;
1117         struct ath_buf *bf, *lastbf;
1118         struct list_head bf_head;
1119 +       struct list_head *fifo_list;
1120         int status;
1121  
1122         for (;;) {
1123 @@ -2291,20 +2290,24 @@ void ath_tx_edma_tasklet(struct ath_soft
1124  
1125                 TX_STAT_INC(txq->axq_qnum, txprocdesc);
1126  
1127 -               if (list_empty(&txq->txq_fifo[txq->txq_tailidx])) {
1128 +               fifo_list = &txq->txq_fifo[txq->txq_tailidx];
1129 +               if (list_empty(fifo_list)) {
1130                         ath_txq_unlock(sc, txq);
1131                         return;
1132                 }
1133  
1134 -               bf = list_first_entry(&txq->txq_fifo[txq->txq_tailidx],
1135 -                                     struct ath_buf, list);
1136 +               bf = list_first_entry(fifo_list, struct ath_buf, list);
1137 +               if (bf->bf_stale) {
1138 +                       list_del(&bf->list);
1139 +                       ath_tx_return_buffer(sc, bf);
1140 +                       bf = list_first_entry(fifo_list, struct ath_buf, list);
1141 +               }
1142 +
1143                 lastbf = bf->bf_lastbf;
1144  
1145                 INIT_LIST_HEAD(&bf_head);
1146 -               list_cut_position(&bf_head, &txq->txq_fifo[txq->txq_tailidx],
1147 -                                 &lastbf->list);
1148 -
1149 -               if (list_empty(&txq->txq_fifo[txq->txq_tailidx])) {
1150 +               if (list_is_last(&lastbf->list, fifo_list)) {
1151 +                       list_splice_tail_init(fifo_list, &bf_head);
1152                         INCR(txq->txq_tailidx, ATH_TXFIFO_DEPTH);
1153  
1154                         if (!list_empty(&txq->axq_q)) {
1155 @@ -2315,6 +2318,11 @@ void ath_tx_edma_tasklet(struct ath_soft
1156                                 list_splice_tail_init(&txq->axq_q, &bf_q);
1157                                 ath_tx_txqaddbuf(sc, txq, &bf_q, true);
1158                         }
1159 +               } else {
1160 +                       lastbf->bf_stale = true;
1161 +                       if (bf != lastbf)
1162 +                               list_cut_position(&bf_head, fifo_list,
1163 +                                                 lastbf->list.prev);
1164                 }
1165  
1166                 ath_tx_process_buffer(sc, txq, &ts, bf, &bf_head);
1167 --- a/drivers/net/wireless/ath/ath9k/ar9580_1p0_initvals.h
1168 +++ b/drivers/net/wireless/ath/ath9k/ar9580_1p0_initvals.h
1169 @@ -519,7 +519,7 @@ static const u32 ar9580_1p0_mac_core[][2
1170         {0x00008258, 0x00000000},
1171         {0x0000825c, 0x40000000},
1172         {0x00008260, 0x00080922},
1173 -       {0x00008264, 0x9bc00010},
1174 +       {0x00008264, 0x9d400010},
1175         {0x00008268, 0xffffffff},
1176         {0x0000826c, 0x0000ffff},
1177         {0x00008270, 0x00000000},
1178 --- a/net/mac80211/mlme.c
1179 +++ b/net/mac80211/mlme.c
1180 @@ -3955,8 +3955,16 @@ int ieee80211_mgd_auth(struct ieee80211_
1181         /* prep auth_data so we don't go into idle on disassoc */
1182         ifmgd->auth_data = auth_data;
1183  
1184 -       if (ifmgd->associated)
1185 -               ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
1186 +       if (ifmgd->associated) {
1187 +               u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
1188 +
1189 +               ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
1190 +                                      WLAN_REASON_UNSPECIFIED,
1191 +                                      false, frame_buf);
1192 +
1193 +               __cfg80211_send_deauth(sdata->dev, frame_buf,
1194 +                                      sizeof(frame_buf));
1195 +       }
1196  
1197         sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
1198  
1199 @@ -4016,8 +4024,16 @@ int ieee80211_mgd_assoc(struct ieee80211
1200  
1201         mutex_lock(&ifmgd->mtx);
1202  
1203 -       if (ifmgd->associated)
1204 -               ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
1205 +       if (ifmgd->associated) {
1206 +               u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
1207 +
1208 +               ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
1209 +                                      WLAN_REASON_UNSPECIFIED,
1210 +                                      false, frame_buf);
1211 +
1212 +               __cfg80211_send_deauth(sdata->dev, frame_buf,
1213 +                                      sizeof(frame_buf));
1214 +       }
1215  
1216         if (ifmgd->auth_data && !ifmgd->auth_data->done) {
1217                 err = -EBUSY;
1218 --- a/net/mac80211/rc80211_minstrel.c
1219 +++ b/net/mac80211/rc80211_minstrel.c
1220 @@ -55,7 +55,6 @@
1221  #include "rate.h"
1222  #include "rc80211_minstrel.h"
1223  
1224 -#define SAMPLE_COLUMNS 10
1225  #define SAMPLE_TBL(_mi, _idx, _col) \
1226                 _mi->sample_table[(_idx * SAMPLE_COLUMNS) + _col]
1227  
1228 @@ -70,16 +69,31 @@ rix_to_ndx(struct minstrel_sta_info *mi,
1229         return i;
1230  }
1231  
1232 +/* find & sort topmost throughput rates */
1233 +static inline void
1234 +minstrel_sort_best_tp_rates(struct minstrel_sta_info *mi, int i, u8 *tp_list)
1235 +{
1236 +       int j = MAX_THR_RATES;
1237 +
1238 +       while (j > 0 && mi->r[i].cur_tp > mi->r[tp_list[j - 1]].cur_tp)
1239 +               j--;
1240 +       if (j < MAX_THR_RATES - 1)
1241 +               memmove(&tp_list[j + 1], &tp_list[j], MAX_THR_RATES - (j + 1));
1242 +       if (j < MAX_THR_RATES)
1243 +               tp_list[j] = i;
1244 +}
1245 +
1246  static void
1247  minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
1248  {
1249 -       u32 max_tp = 0, index_max_tp = 0, index_max_tp2 = 0;
1250 -       u32 max_prob = 0, index_max_prob = 0;
1251 +       u8 tmp_tp_rate[MAX_THR_RATES];
1252 +       u8 tmp_prob_rate = 0;
1253         u32 usecs;
1254 -       u32 p;
1255         int i;
1256  
1257 -       mi->stats_update = jiffies;
1258 +       for (i=0; i < MAX_THR_RATES; i++)
1259 +           tmp_tp_rate[i] = 0;
1260 +
1261         for (i = 0; i < mi->n_rates; i++) {
1262                 struct minstrel_rate *mr = &mi->r[i];
1263  
1264 @@ -87,27 +101,32 @@ minstrel_update_stats(struct minstrel_pr
1265                 if (!usecs)
1266                         usecs = 1000000;
1267  
1268 -               /* To avoid rounding issues, probabilities scale from 0 (0%)
1269 -                * to 18000 (100%) */
1270 -               if (mr->attempts) {
1271 -                       p = (mr->success * 18000) / mr->attempts;
1272 +               if (unlikely(mr->attempts > 0)) {
1273 +                       mr->sample_skipped = 0;
1274 +                       mr->cur_prob = MINSTREL_FRAC(mr->success, mr->attempts);
1275                         mr->succ_hist += mr->success;
1276                         mr->att_hist += mr->attempts;
1277 -                       mr->cur_prob = p;
1278 -                       p = ((p * (100 - mp->ewma_level)) + (mr->probability *
1279 -                               mp->ewma_level)) / 100;
1280 -                       mr->probability = p;
1281 -                       mr->cur_tp = p * (1000000 / usecs);
1282 -               }
1283 +                       mr->probability = minstrel_ewma(mr->probability,
1284 +                                                       mr->cur_prob,
1285 +                                                       EWMA_LEVEL);
1286 +               } else
1287 +                       mr->sample_skipped++;
1288  
1289                 mr->last_success = mr->success;
1290                 mr->last_attempts = mr->attempts;
1291                 mr->success = 0;
1292                 mr->attempts = 0;
1293  
1294 +               /* Update throughput per rate, reset thr. below 10% success */
1295 +               if (mr->probability < MINSTREL_FRAC(10, 100))
1296 +                       mr->cur_tp = 0;
1297 +               else
1298 +                       mr->cur_tp = mr->probability * (1000000 / usecs);
1299 +
1300                 /* Sample less often below the 10% chance of success.
1301                  * Sample less often above the 95% chance of success. */
1302 -               if ((mr->probability > 17100) || (mr->probability < 1800)) {
1303 +               if (mr->probability > MINSTREL_FRAC(95, 100) ||
1304 +                   mr->probability < MINSTREL_FRAC(10, 100)) {
1305                         mr->adjusted_retry_count = mr->retry_count >> 1;
1306                         if (mr->adjusted_retry_count > 2)
1307                                 mr->adjusted_retry_count = 2;
1308 @@ -118,35 +137,30 @@ minstrel_update_stats(struct minstrel_pr
1309                 }
1310                 if (!mr->adjusted_retry_count)
1311                         mr->adjusted_retry_count = 2;
1312 -       }
1313  
1314 -       for (i = 0; i < mi->n_rates; i++) {
1315 -               struct minstrel_rate *mr = &mi->r[i];
1316 -               if (max_tp < mr->cur_tp) {
1317 -                       index_max_tp = i;
1318 -                       max_tp = mr->cur_tp;
1319 -               }
1320 -               if (max_prob < mr->probability) {
1321 -                       index_max_prob = i;
1322 -                       max_prob = mr->probability;
1323 +               minstrel_sort_best_tp_rates(mi, i, tmp_tp_rate);
1324 +
1325 +               /* To determine the most robust rate (max_prob_rate) used at
1326 +                * 3rd mmr stage we distinct between two cases:
1327 +                * (1) if any success probabilitiy >= 95%, out of those rates
1328 +                * choose the maximum throughput rate as max_prob_rate
1329 +                * (2) if all success probabilities < 95%, the rate with
1330 +                * highest success probability is choosen as max_prob_rate */
1331 +               if (mr->probability >= MINSTREL_FRAC(95,100)) {
1332 +                       if (mr->cur_tp >= mi->r[tmp_prob_rate].cur_tp)
1333 +                               tmp_prob_rate = i;
1334 +               } else {
1335 +                       if (mr->probability >= mi->r[tmp_prob_rate].probability)
1336 +                               tmp_prob_rate = i;
1337                 }
1338         }
1339  
1340 -       max_tp = 0;
1341 -       for (i = 0; i < mi->n_rates; i++) {
1342 -               struct minstrel_rate *mr = &mi->r[i];
1343 -
1344 -               if (i == index_max_tp)
1345 -                       continue;
1346 +       /* Assign the new rate set */
1347 +       memcpy(mi->max_tp_rate, tmp_tp_rate, sizeof(mi->max_tp_rate));
1348 +       mi->max_prob_rate = tmp_prob_rate;
1349  
1350 -               if (max_tp < mr->cur_tp) {
1351 -                       index_max_tp2 = i;
1352 -                       max_tp = mr->cur_tp;
1353 -               }
1354 -       }
1355 -       mi->max_tp_rate = index_max_tp;
1356 -       mi->max_tp_rate2 = index_max_tp2;
1357 -       mi->max_prob_rate = index_max_prob;
1358 +       /* Reset update timer */
1359 +       mi->stats_update = jiffies;
1360  }
1361  
1362  static void
1363 @@ -207,10 +221,10 @@ static int
1364  minstrel_get_next_sample(struct minstrel_sta_info *mi)
1365  {
1366         unsigned int sample_ndx;
1367 -       sample_ndx = SAMPLE_TBL(mi, mi->sample_idx, mi->sample_column);
1368 -       mi->sample_idx++;
1369 -       if ((int) mi->sample_idx > (mi->n_rates - 2)) {
1370 -               mi->sample_idx = 0;
1371 +       sample_ndx = SAMPLE_TBL(mi, mi->sample_row, mi->sample_column);
1372 +       mi->sample_row++;
1373 +       if ((int) mi->sample_row >= mi->n_rates) {
1374 +               mi->sample_row = 0;
1375                 mi->sample_column++;
1376                 if (mi->sample_column >= SAMPLE_COLUMNS)
1377                         mi->sample_column = 0;
1378 @@ -228,31 +242,37 @@ minstrel_get_rate(void *priv, struct iee
1379         struct minstrel_priv *mp = priv;
1380         struct ieee80211_tx_rate *ar = info->control.rates;
1381         unsigned int ndx, sample_ndx = 0;
1382 -       bool mrr;
1383 -       bool sample_slower = false;
1384 -       bool sample = false;
1385 +       bool mrr_capable;
1386 +       bool indirect_rate_sampling = false;
1387 +       bool rate_sampling = false;
1388         int i, delta;
1389         int mrr_ndx[3];
1390 -       int sample_rate;
1391 +       int sampling_ratio;
1392  
1393 +       /* management/no-ack frames do not use rate control */
1394         if (rate_control_send_low(sta, priv_sta, txrc))
1395                 return;
1396  
1397 -       mrr = mp->has_mrr && !txrc->rts && !txrc->bss_conf->use_cts_prot;
1398 -
1399 -       ndx = mi->max_tp_rate;
1400 -
1401 -       if (mrr)
1402 -               sample_rate = mp->lookaround_rate_mrr;
1403 +       /* check multi-rate-retry capabilities & adjust lookaround_rate */
1404 +       mrr_capable = mp->has_mrr &&
1405 +                     !txrc->rts &&
1406 +                     !txrc->bss_conf->use_cts_prot;
1407 +       if (mrr_capable)
1408 +               sampling_ratio = mp->lookaround_rate_mrr;
1409         else
1410 -               sample_rate = mp->lookaround_rate;
1411 +               sampling_ratio = mp->lookaround_rate;
1412 +
1413 +       /* init rateindex [ndx] with max throughput rate */
1414 +       ndx = mi->max_tp_rate[0];
1415  
1416 +       /* increase sum packet counter */
1417         mi->packet_count++;
1418 -       delta = (mi->packet_count * sample_rate / 100) -
1419 +
1420 +       delta = (mi->packet_count * sampling_ratio / 100) -
1421                         (mi->sample_count + mi->sample_deferred / 2);
1422  
1423         /* delta > 0: sampling required */
1424 -       if ((delta > 0) && (mrr || !mi->prev_sample)) {
1425 +       if ((delta > 0) && (mrr_capable || !mi->prev_sample)) {
1426                 struct minstrel_rate *msr;
1427                 if (mi->packet_count >= 10000) {
1428                         mi->sample_deferred = 0;
1429 @@ -271,21 +291,28 @@ minstrel_get_rate(void *priv, struct iee
1430                         mi->sample_count += (delta - mi->n_rates * 2);
1431                 }
1432  
1433 +               /* get next random rate sample */
1434                 sample_ndx = minstrel_get_next_sample(mi);
1435                 msr = &mi->r[sample_ndx];
1436 -               sample = true;
1437 -               sample_slower = mrr && (msr->perfect_tx_time >
1438 -                       mi->r[ndx].perfect_tx_time);
1439 +               rate_sampling = true;
1440  
1441 -               if (!sample_slower) {
1442 +               /* Decide if direct ( 1st mrr stage) or indirect (2nd mrr stage)
1443 +                * rate sampling method should be used.
1444 +                * Respect such rates that are not sampled for 20 interations.
1445 +                */
1446 +               if (mrr_capable &&
1447 +                   msr->perfect_tx_time > mi->r[ndx].perfect_tx_time &&
1448 +                   msr->sample_skipped < 20)
1449 +                               indirect_rate_sampling = true;
1450 +
1451 +               if (!indirect_rate_sampling) {
1452                         if (msr->sample_limit != 0) {
1453                                 ndx = sample_ndx;
1454                                 mi->sample_count++;
1455                                 if (msr->sample_limit > 0)
1456                                         msr->sample_limit--;
1457 -                       } else {
1458 -                               sample = false;
1459 -                       }
1460 +                       } else
1461 +                               rate_sampling = false;
1462                 } else {
1463                         /* Only use IEEE80211_TX_CTL_RATE_CTRL_PROBE to mark
1464                          * packets that have the sampling rate deferred to the
1465 @@ -297,34 +324,39 @@ minstrel_get_rate(void *priv, struct iee
1466                         mi->sample_deferred++;
1467                 }
1468         }
1469 -       mi->prev_sample = sample;
1470 +       mi->prev_sample = rate_sampling;
1471  
1472         /* If we're not using MRR and the sampling rate already
1473          * has a probability of >95%, we shouldn't be attempting
1474          * to use it, as this only wastes precious airtime */
1475 -       if (!mrr && sample && (mi->r[ndx].probability > 17100))
1476 -               ndx = mi->max_tp_rate;
1477 +       if (!mrr_capable && rate_sampling &&
1478 +          (mi->r[ndx].probability > MINSTREL_FRAC(95, 100)))
1479 +               ndx = mi->max_tp_rate[0];
1480  
1481 +       /* mrr setup for 1st stage */
1482         ar[0].idx = mi->r[ndx].rix;
1483         ar[0].count = minstrel_get_retry_count(&mi->r[ndx], info);
1484  
1485 -       if (!mrr) {
1486 -               if (!sample)
1487 +       /* non mrr setup for 2nd stage */
1488 +       if (!mrr_capable) {
1489 +               if (!rate_sampling)
1490                         ar[0].count = mp->max_retry;
1491                 ar[1].idx = mi->lowest_rix;
1492                 ar[1].count = mp->max_retry;
1493                 return;
1494         }
1495  
1496 -       /* MRR setup */
1497 -       if (sample) {
1498 -               if (sample_slower)
1499 +       /* mrr setup for 2nd stage */
1500 +       if (rate_sampling) {
1501 +               if (indirect_rate_sampling)
1502                         mrr_ndx[0] = sample_ndx;
1503                 else
1504 -                       mrr_ndx[0] = mi->max_tp_rate;
1505 +                       mrr_ndx[0] = mi->max_tp_rate[0];
1506         } else {
1507 -               mrr_ndx[0] = mi->max_tp_rate2;
1508 +               mrr_ndx[0] = mi->max_tp_rate[1];
1509         }
1510 +
1511 +       /* mrr setup for 3rd & 4th stage */
1512         mrr_ndx[1] = mi->max_prob_rate;
1513         mrr_ndx[2] = 0;
1514         for (i = 1; i < 4; i++) {
1515 @@ -351,26 +383,21 @@ static void
1516  init_sample_table(struct minstrel_sta_info *mi)
1517  {
1518         unsigned int i, col, new_idx;
1519 -       unsigned int n_srates = mi->n_rates - 1;
1520         u8 rnd[8];
1521  
1522         mi->sample_column = 0;
1523 -       mi->sample_idx = 0;
1524 -       memset(mi->sample_table, 0, SAMPLE_COLUMNS * mi->n_rates);
1525 +       mi->sample_row = 0;
1526 +       memset(mi->sample_table, 0xff, SAMPLE_COLUMNS * mi->n_rates);
1527  
1528         for (col = 0; col < SAMPLE_COLUMNS; col++) {
1529 -               for (i = 0; i < n_srates; i++) {
1530 +               for (i = 0; i < mi->n_rates; i++) {
1531                         get_random_bytes(rnd, sizeof(rnd));
1532 -                       new_idx = (i + rnd[i & 7]) % n_srates;
1533 +                       new_idx = (i + rnd[i & 7]) % mi->n_rates;
1534  
1535 -                       while (SAMPLE_TBL(mi, new_idx, col) != 0)
1536 -                               new_idx = (new_idx + 1) % n_srates;
1537 +                       while (SAMPLE_TBL(mi, new_idx, col) != 0xff)
1538 +                               new_idx = (new_idx + 1) % mi->n_rates;
1539  
1540 -                       /* Don't sample the slowest rate (i.e. slowest base
1541 -                        * rate). We must presume that the slowest rate works
1542 -                        * fine, or else other management frames will also be
1543 -                        * failing and the link will break */
1544 -                       SAMPLE_TBL(mi, new_idx, col) = i + 1;
1545 +                       SAMPLE_TBL(mi, new_idx, col) = i;
1546                 }
1547         }
1548  }
1549 @@ -542,9 +569,6 @@ minstrel_alloc(struct ieee80211_hw *hw, 
1550         mp->lookaround_rate = 5;
1551         mp->lookaround_rate_mrr = 10;
1552  
1553 -       /* moving average weight for EWMA */
1554 -       mp->ewma_level = 75;
1555 -
1556         /* maximum time that the hw is allowed to stay in one MRR segment */
1557         mp->segment_size = 6000;
1558  
1559 --- a/net/mac80211/rc80211_minstrel.h
1560 +++ b/net/mac80211/rc80211_minstrel.h
1561 @@ -9,6 +9,28 @@
1562  #ifndef __RC_MINSTREL_H
1563  #define __RC_MINSTREL_H
1564  
1565 +#define EWMA_LEVEL     75      /* ewma weighting factor [%] */
1566 +#define SAMPLE_COLUMNS 10      /* number of columns in sample table */
1567 +
1568 +
1569 +/* scaled fraction values */
1570 +#define MINSTREL_SCALE  16
1571 +#define MINSTREL_FRAC(val, div) (((val) << MINSTREL_SCALE) / div)
1572 +#define MINSTREL_TRUNC(val) ((val) >> MINSTREL_SCALE)
1573 +
1574 +/* number of highest throughput rates to consider*/
1575 +#define MAX_THR_RATES 4
1576 +
1577 +/*
1578 + * Perform EWMA (Exponentially Weighted Moving Average) calculation
1579 +  */
1580 +static inline int
1581 +minstrel_ewma(int old, int new, int weight)
1582 +{
1583 +       return (new * (100 - weight) + old * weight) / 100;
1584 +}
1585 +
1586 +
1587  struct minstrel_rate {
1588         int bitrate;
1589         int rix;
1590 @@ -26,6 +48,7 @@ struct minstrel_rate {
1591         u32 attempts;
1592         u32 last_attempts;
1593         u32 last_success;
1594 +       u8 sample_skipped;
1595  
1596         /* parts per thousand */
1597         u32 cur_prob;
1598 @@ -45,14 +68,13 @@ struct minstrel_sta_info {
1599  
1600         unsigned int lowest_rix;
1601  
1602 -       unsigned int max_tp_rate;
1603 -       unsigned int max_tp_rate2;
1604 -       unsigned int max_prob_rate;
1605 +       u8 max_tp_rate[MAX_THR_RATES];
1606 +       u8 max_prob_rate;
1607         unsigned int packet_count;
1608         unsigned int sample_count;
1609         int sample_deferred;
1610  
1611 -       unsigned int sample_idx;
1612 +       unsigned int sample_row;
1613         unsigned int sample_column;
1614  
1615         int n_rates;
1616 @@ -73,7 +95,6 @@ struct minstrel_priv {
1617         unsigned int cw_min;
1618         unsigned int cw_max;
1619         unsigned int max_retry;
1620 -       unsigned int ewma_level;
1621         unsigned int segment_size;
1622         unsigned int update_interval;
1623         unsigned int lookaround_rate;
1624 --- a/net/mac80211/rc80211_minstrel_debugfs.c
1625 +++ b/net/mac80211/rc80211_minstrel_debugfs.c
1626 @@ -73,15 +73,17 @@ minstrel_stats_open(struct inode *inode,
1627         for (i = 0; i < mi->n_rates; i++) {
1628                 struct minstrel_rate *mr = &mi->r[i];
1629  
1630 -               *(p++) = (i == mi->max_tp_rate) ? 'T' : ' ';
1631 -               *(p++) = (i == mi->max_tp_rate2) ? 't' : ' ';
1632 +               *(p++) = (i == mi->max_tp_rate[0]) ? 'A' : ' ';
1633 +               *(p++) = (i == mi->max_tp_rate[1]) ? 'B' : ' ';
1634 +               *(p++) = (i == mi->max_tp_rate[2]) ? 'C' : ' ';
1635 +               *(p++) = (i == mi->max_tp_rate[3]) ? 'D' : ' ';
1636                 *(p++) = (i == mi->max_prob_rate) ? 'P' : ' ';
1637                 p += sprintf(p, "%3u%s", mr->bitrate / 2,
1638                                 (mr->bitrate & 1 ? ".5" : "  "));
1639  
1640 -               tp = mr->cur_tp / ((18000 << 10) / 96);
1641 -               prob = mr->cur_prob / 18;
1642 -               eprob = mr->probability / 18;
1643 +               tp = MINSTREL_TRUNC(mr->cur_tp / 10);
1644 +               prob = MINSTREL_TRUNC(mr->cur_prob * 1000);
1645 +               eprob = MINSTREL_TRUNC(mr->probability * 1000);
1646  
1647                 p += sprintf(p, "  %6u.%1u   %6u.%1u   %6u.%1u        "
1648                                 "%3u(%3u)   %8llu    %8llu\n",
1649 --- a/drivers/net/wireless/ath/ath9k/ath9k.h
1650 +++ b/drivers/net/wireless/ath/ath9k/ath9k.h
1651 @@ -657,11 +657,10 @@ enum sc_op_flags {
1652  struct ath_rate_table;
1653  
1654  struct ath9k_vif_iter_data {
1655 -       const u8 *hw_macaddr; /* phy's hardware address, set
1656 -                              * before starting iteration for
1657 -                              * valid bssid mask.
1658 -                              */
1659 +       u8 hw_macaddr[ETH_ALEN]; /* address of the first vif */
1660         u8 mask[ETH_ALEN]; /* bssid mask */
1661 +       bool has_hw_macaddr;
1662 +
1663         int naps;      /* number of AP vifs */
1664         int nmeshes;   /* number of mesh vifs */
1665         int nstations; /* number of station vifs */
1666 --- a/drivers/net/wireless/ath/ath9k/main.c
1667 +++ b/drivers/net/wireless/ath/ath9k/main.c
1668 @@ -835,10 +835,14 @@ static void ath9k_vif_iter(void *data, u
1669         struct ath9k_vif_iter_data *iter_data = data;
1670         int i;
1671  
1672 -       if (iter_data->hw_macaddr)
1673 +       if (iter_data->has_hw_macaddr) {
1674                 for (i = 0; i < ETH_ALEN; i++)
1675                         iter_data->mask[i] &=
1676                                 ~(iter_data->hw_macaddr[i] ^ mac[i]);
1677 +       } else {
1678 +               memcpy(iter_data->hw_macaddr, mac, ETH_ALEN);
1679 +               iter_data->has_hw_macaddr = true;
1680 +       }
1681  
1682         switch (vif->type) {
1683         case NL80211_IFTYPE_AP:
1684 @@ -887,7 +891,6 @@ void ath9k_calculate_iter_data(struct ie
1685          * together with the BSSID mask when matching addresses.
1686          */
1687         memset(iter_data, 0, sizeof(*iter_data));
1688 -       iter_data->hw_macaddr = common->macaddr;
1689         memset(&iter_data->mask, 0xff, ETH_ALEN);
1690  
1691         if (vif)
1692 @@ -897,6 +900,8 @@ void ath9k_calculate_iter_data(struct ie
1693         ieee80211_iterate_active_interfaces_atomic(
1694                 sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1695                 ath9k_vif_iter, iter_data);
1696 +
1697 +       memcpy(common->macaddr, iter_data->hw_macaddr, ETH_ALEN);
1698  }
1699  
1700  /* Called with sc->mutex held. */
1701 --- a/drivers/net/wireless/ath/ath9k/reg.h
1702 +++ b/drivers/net/wireless/ath/ath9k/reg.h
1703 @@ -1493,9 +1493,6 @@ enum {
1704  #define AR9271_RADIO_RF_RST                    0x20
1705  #define AR9271_GATE_MAC_CTL                    0x4000
1706  
1707 -#define AR_STA_ID0                 0x8000
1708 -#define AR_STA_ID1                 0x8004
1709 -#define AR_STA_ID1_SADH_MASK       0x0000FFFF
1710  #define AR_STA_ID1_STA_AP          0x00010000
1711  #define AR_STA_ID1_ADHOC           0x00020000
1712  #define AR_STA_ID1_PWR_SAV         0x00040000
1713 --- a/drivers/net/wireless/ath/hw.c
1714 +++ b/drivers/net/wireless/ath/hw.c
1715 @@ -118,6 +118,12 @@
1716  void ath_hw_setbssidmask(struct ath_common *common)
1717  {
1718         void *ah = common->ah;
1719 +       u32 id1;
1720 +
1721 +       REG_WRITE(ah, AR_STA_ID0, get_unaligned_le32(common->macaddr));
1722 +       id1 = REG_READ(ah, AR_STA_ID1) & ~AR_STA_ID1_SADH_MASK;
1723 +       id1 |= get_unaligned_le16(common->macaddr + 4);
1724 +       REG_WRITE(ah, AR_STA_ID1, id1);
1725  
1726         REG_WRITE(ah, AR_BSSMSKL, get_unaligned_le32(common->bssidmask));
1727         REG_WRITE(ah, AR_BSSMSKU, get_unaligned_le16(common->bssidmask + 4));
1728 --- a/drivers/net/wireless/ath/reg.h
1729 +++ b/drivers/net/wireless/ath/reg.h
1730 @@ -23,6 +23,10 @@
1731  #define AR_MIBC_CMC            0x00000004
1732  #define AR_MIBC_MCS            0x00000008
1733  
1734 +#define AR_STA_ID0             0x8000
1735 +#define AR_STA_ID1             0x8004
1736 +#define AR_STA_ID1_SADH_MASK   0x0000ffff
1737 +
1738  /*
1739   * BSSID mask registers. See ath_hw_set_bssid_mask()
1740   * for detailed documentation about these registers.