a4bfb99b422e3a1c4f5015eddc19cf6e0f7519a7
[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 --- a/drivers/net/wireless/rt2x00/rt2x00dev.c
611 +++ b/drivers/net/wireless/rt2x00/rt2x00dev.c
612 @@ -1236,8 +1236,10 @@ static inline void rt2x00lib_set_if_comb
613          */
614         if_limit = &rt2x00dev->if_limits_ap;
615         if_limit->max = rt2x00dev->ops->max_ap_intf;
616 -       if_limit->types = BIT(NL80211_IFTYPE_AP) |
617 -                       BIT(NL80211_IFTYPE_MESH_POINT);
618 +       if_limit->types = BIT(NL80211_IFTYPE_AP);
619 +#ifdef CONFIG_MAC80211_MESH
620 +       if_limit->types |= BIT(NL80211_IFTYPE_MESH_POINT);
621 +#endif
622  
623         /*
624          * Build up AP interface combinations structure.
625 @@ -1309,7 +1311,9 @@ int rt2x00lib_probe_dev(struct rt2x00_de
626                 rt2x00dev->hw->wiphy->interface_modes |=
627                     BIT(NL80211_IFTYPE_ADHOC) |
628                     BIT(NL80211_IFTYPE_AP) |
629 +#ifdef CONFIG_MAC80211_MESH
630                     BIT(NL80211_IFTYPE_MESH_POINT) |
631 +#endif
632                     BIT(NL80211_IFTYPE_WDS);
633  
634         rt2x00dev->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
635 --- a/net/mac80211/rc80211_minstrel_ht.c
636 +++ b/net/mac80211/rc80211_minstrel_ht.c
637 @@ -17,8 +17,6 @@
638  #include "rc80211_minstrel_ht.h"
639  
640  #define AVG_PKT_SIZE   1200
641 -#define SAMPLE_COLUMNS 10
642 -#define EWMA_LEVEL             75
643  
644  /* Number of bits for an average sized packet */
645  #define MCS_NBITS (AVG_PKT_SIZE << 3)
646 @@ -26,11 +24,11 @@
647  /* Number of symbols for a packet with (bps) bits per symbol */
648  #define MCS_NSYMS(bps) ((MCS_NBITS + (bps) - 1) / (bps))
649  
650 -/* Transmission time for a packet containing (syms) symbols */
651 +/* Transmission time (nanoseconds) for a packet containing (syms) symbols */
652  #define MCS_SYMBOL_TIME(sgi, syms)                                     \
653         (sgi ?                                                          \
654 -         ((syms) * 18 + 4) / 5 :       /* syms * 3.6 us */             \
655 -         (syms) << 2                   /* syms * 4 us */               \
656 +         ((syms) * 18000 + 4000) / 5 : /* syms * 3.6 us */             \
657 +         ((syms) * 1000) << 2          /* syms * 4 us */               \
658         )
659  
660  /* Transmit duration for the raw data part of an average sized packet */
661 @@ -64,9 +62,9 @@
662  }
663  
664  #define CCK_DURATION(_bitrate, _short, _len)           \
665 -       (10 /* SIFS */ +                                \
666 +       (1000 * (10 /* SIFS */ +                        \
667          (_short ? 72 + 24 : 144 + 48 ) +               \
668 -        (8 * (_len + 4) * 10) / (_bitrate))
669 +        (8 * (_len + 4) * 10) / (_bitrate)))
670  
671  #define CCK_ACK_DURATION(_bitrate, _short)                     \
672         (CCK_DURATION((_bitrate > 10 ? 20 : 10), false, 60) +   \
673 @@ -129,15 +127,6 @@ const struct mcs_group minstrel_mcs_grou
674  static u8 sample_table[SAMPLE_COLUMNS][MCS_GROUP_RATES];
675  
676  /*
677 - * Perform EWMA (Exponentially Weighted Moving Average) calculation
678 - */
679 -static int
680 -minstrel_ewma(int old, int new, int weight)
681 -{
682 -       return (new * (100 - weight) + old * weight) / 100;
683 -}
684 -
685 -/*
686   * Look up an MCS group index based on mac80211 rate information
687   */
688  static int
689 @@ -211,20 +200,32 @@ static void
690  minstrel_ht_calc_tp(struct minstrel_ht_sta *mi, int group, int rate)
691  {
692         struct minstrel_rate_stats *mr;
693 -       unsigned int usecs = 0;
694 +       unsigned int nsecs = 0;
695 +       unsigned int tp;
696 +       unsigned int prob;
697  
698         mr = &mi->groups[group].rates[rate];
699 +       prob = mr->probability;
700  
701 -       if (mr->probability < MINSTREL_FRAC(1, 10)) {
702 +       if (prob < MINSTREL_FRAC(1, 10)) {
703                 mr->cur_tp = 0;
704                 return;
705         }
706  
707 +       /*
708 +        * For the throughput calculation, limit the probability value to 90% to
709 +        * account for collision related packet error rate fluctuation
710 +        */
711 +       if (prob > MINSTREL_FRAC(9, 10))
712 +               prob = MINSTREL_FRAC(9, 10);
713 +
714         if (group != MINSTREL_CCK_GROUP)
715 -               usecs = mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len);
716 +               nsecs = 1000 * mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len);
717 +
718 +       nsecs += minstrel_mcs_groups[group].duration[rate];
719 +       tp = 1000000 * ((mr->probability * 1000) / nsecs);
720  
721 -       usecs += minstrel_mcs_groups[group].duration[rate];
722 -       mr->cur_tp = MINSTREL_TRUNC((1000000 / usecs) * mr->probability);
723 +       mr->cur_tp = MINSTREL_TRUNC(tp);
724  }
725  
726  /*
727 @@ -308,8 +309,8 @@ minstrel_ht_update_stats(struct minstrel
728                 }
729         }
730  
731 -       /* try to sample up to half of the available rates during each interval */
732 -       mi->sample_count *= 4;
733 +       /* try to sample all available rates during each interval */
734 +       mi->sample_count *= 8;
735  
736         cur_prob = 0;
737         cur_prob_tp = 0;
738 @@ -320,20 +321,13 @@ minstrel_ht_update_stats(struct minstrel
739                 if (!mg->supported)
740                         continue;
741  
742 -               mr = minstrel_get_ratestats(mi, mg->max_prob_rate);
743 -               if (cur_prob_tp < mr->cur_tp &&
744 -                   minstrel_mcs_groups[group].streams == 1) {
745 -                       mi->max_prob_rate = mg->max_prob_rate;
746 -                       cur_prob = mr->cur_prob;
747 -                       cur_prob_tp = mr->cur_tp;
748 -               }
749 -
750                 mr = minstrel_get_ratestats(mi, mg->max_tp_rate);
751                 if (cur_tp < mr->cur_tp) {
752                         mi->max_tp_rate2 = mi->max_tp_rate;
753                         cur_tp2 = cur_tp;
754                         mi->max_tp_rate = mg->max_tp_rate;
755                         cur_tp = mr->cur_tp;
756 +                       mi->max_prob_streams = minstrel_mcs_groups[group].streams - 1;
757                 }
758  
759                 mr = minstrel_get_ratestats(mi, mg->max_tp_rate2);
760 @@ -343,6 +337,23 @@ minstrel_ht_update_stats(struct minstrel
761                 }
762         }
763  
764 +       if (mi->max_prob_streams < 1)
765 +               mi->max_prob_streams = 1;
766 +
767 +       for (group = 0; group < ARRAY_SIZE(minstrel_mcs_groups); group++) {
768 +               mg = &mi->groups[group];
769 +               if (!mg->supported)
770 +                       continue;
771 +               mr = minstrel_get_ratestats(mi, mg->max_prob_rate);
772 +               if (cur_prob_tp < mr->cur_tp &&
773 +                   minstrel_mcs_groups[group].streams <= mi->max_prob_streams) {
774 +                       mi->max_prob_rate = mg->max_prob_rate;
775 +                       cur_prob = mr->cur_prob;
776 +                       cur_prob_tp = mr->cur_tp;
777 +               }
778 +       }
779 +
780 +
781         mi->stats_update = jiffies;
782  }
783  
784 @@ -467,7 +478,7 @@ minstrel_ht_tx_status(void *priv, struct
785  
786         if (!mi->sample_wait && !mi->sample_tries && mi->sample_count > 0) {
787                 mi->sample_wait = 16 + 2 * MINSTREL_TRUNC(mi->avg_ampdu_len);
788 -               mi->sample_tries = 2;
789 +               mi->sample_tries = 1;
790                 mi->sample_count--;
791         }
792  
793 @@ -536,7 +547,7 @@ minstrel_calc_retransmit(struct minstrel
794         mr->retry_updated = true;
795  
796         group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
797 -       tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len;
798 +       tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len / 1000;
799  
800         /* Contention time for first 2 tries */
801         ctime = (t_slot * cw) >> 1;
802 @@ -616,6 +627,7 @@ minstrel_get_sample_rate(struct minstrel
803  {
804         struct minstrel_rate_stats *mr;
805         struct minstrel_mcs_group_data *mg;
806 +       unsigned int sample_dur, sample_group;
807         int sample_idx = 0;
808  
809         if (mi->sample_wait > 0) {
810 @@ -626,39 +638,46 @@ minstrel_get_sample_rate(struct minstrel
811         if (!mi->sample_tries)
812                 return -1;
813  
814 -       mi->sample_tries--;
815         mg = &mi->groups[mi->sample_group];
816         sample_idx = sample_table[mg->column][mg->index];
817         mr = &mg->rates[sample_idx];
818 -       sample_idx += mi->sample_group * MCS_GROUP_RATES;
819 +       sample_group = mi->sample_group;
820 +       sample_idx += sample_group * MCS_GROUP_RATES;
821         minstrel_next_sample_idx(mi);
822  
823         /*
824          * Sampling might add some overhead (RTS, no aggregation)
825          * to the frame. Hence, don't use sampling for the currently
826 -        * used max TP rate.
827 +        * used rates.
828          */
829 -       if (sample_idx == mi->max_tp_rate)
830 +       if (sample_idx == mi->max_tp_rate ||
831 +           sample_idx == mi->max_tp_rate2 ||
832 +           sample_idx == mi->max_prob_rate)
833                 return -1;
834 +
835         /*
836 -        * When not using MRR, do not sample if the probability is already
837 -        * higher than 95% to avoid wasting airtime
838 +        * Do not sample if the probability is already higher than 95%
839 +        * to avoid wasting airtime.
840          */
841 -       if (!mp->has_mrr && (mr->probability > MINSTREL_FRAC(95, 100)))
842 +       if (mr->probability > MINSTREL_FRAC(95, 100))
843                 return -1;
844  
845         /*
846          * Make sure that lower rates get sampled only occasionally,
847          * if the link is working perfectly.
848          */
849 -       if (minstrel_get_duration(sample_idx) >
850 -           minstrel_get_duration(mi->max_tp_rate)) {
851 +       sample_dur = minstrel_get_duration(sample_idx);
852 +       if (sample_dur >= minstrel_get_duration(mi->max_tp_rate2) &&
853 +           (mi->max_prob_streams <
854 +            minstrel_mcs_groups[sample_group].streams ||
855 +            sample_dur >= minstrel_get_duration(mi->max_prob_rate))) {
856                 if (mr->sample_skipped < 20)
857                         return -1;
858  
859                 if (mi->sample_slow++ > 2)
860                         return -1;
861         }
862 +       mi->sample_tries--;
863  
864         return sample_idx;
865  }
866 --- a/net/mac80211/rc80211_minstrel_ht.h
867 +++ b/net/mac80211/rc80211_minstrel_ht.h
868 @@ -16,11 +16,6 @@
869  #define MINSTREL_MAX_STREAMS   3
870  #define MINSTREL_STREAM_GROUPS 4
871  
872 -/* scaled fraction values */
873 -#define MINSTREL_SCALE 16
874 -#define MINSTREL_FRAC(val, div) (((val) << MINSTREL_SCALE) / div)
875 -#define MINSTREL_TRUNC(val) ((val) >> MINSTREL_SCALE)
876 -
877  #define MCS_GROUP_RATES        8
878  
879  struct mcs_group {
880 @@ -85,6 +80,7 @@ struct minstrel_ht_sta {
881  
882         /* best probability rate */
883         unsigned int max_prob_rate;
884 +       unsigned int max_prob_streams;
885  
886         /* time of last status update */
887         unsigned long stats_update;
888 --- a/drivers/net/wireless/ath/ath9k/ar9003_calib.c
889 +++ b/drivers/net/wireless/ath/ath9k/ar9003_calib.c
890 @@ -1023,6 +1023,7 @@ static bool ar9003_hw_init_cal(struct at
891                                           AR_PHY_AGC_CONTROL_FLTR_CAL   |
892                                           AR_PHY_AGC_CONTROL_PKDET_CAL;
893  
894 +       /* Use chip chainmask only for calibration */
895         ar9003_hw_set_chain_masks(ah, ah->caps.rx_chainmask, ah->caps.tx_chainmask);
896  
897         if (rtt) {
898 @@ -1150,6 +1151,9 @@ skip_tx_iqcal:
899                 ar9003_hw_rtt_disable(ah);
900         }
901  
902 +       /* Revert chainmask to runtime parameters */
903 +       ar9003_hw_set_chain_masks(ah, ah->rxchainmask, ah->txchainmask);
904 +
905         /* Initialize list pointers */
906         ah->cal_list = ah->cal_list_last = ah->cal_list_curr = NULL;
907  
908 --- a/drivers/net/wireless/ath/ath9k/ar9003_eeprom.c
909 +++ b/drivers/net/wireless/ath/ath9k/ar9003_eeprom.c
910 @@ -3606,6 +3606,12 @@ static void ar9003_hw_ant_ctrl_apply(str
911         value = ar9003_hw_ant_ctrl_common_2_get(ah, is2ghz);
912         REG_RMW_FIELD(ah, AR_PHY_SWITCH_COM_2, AR_SWITCH_TABLE_COM2_ALL, value);
913  
914 +       if ((AR_SREV_9462(ah)) && (ah->rxchainmask == 0x2)) {
915 +               value = ar9003_hw_ant_ctrl_chain_get(ah, 1, is2ghz);
916 +               REG_RMW_FIELD(ah, switch_chain_reg[0],
917 +                             AR_SWITCH_TABLE_ALL, value);
918 +       }
919 +
920         for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
921                 if ((ah->rxchainmask & BIT(chain)) ||
922                     (ah->txchainmask & BIT(chain))) {
923 @@ -3772,6 +3778,17 @@ static void ar9003_hw_atten_apply(struct
924                                           AR_PHY_EXT_ATTEN_CTL_2,
925                                          };
926  
927 +       if ((AR_SREV_9462(ah)) && (ah->rxchainmask == 0x2)) {
928 +               value = ar9003_hw_atten_chain_get(ah, 1, chan);
929 +               REG_RMW_FIELD(ah, ext_atten_reg[0],
930 +                             AR_PHY_EXT_ATTEN_CTL_XATTEN1_DB, value);
931 +
932 +               value = ar9003_hw_atten_chain_get_margin(ah, 1, chan);
933 +               REG_RMW_FIELD(ah, ext_atten_reg[0],
934 +                             AR_PHY_EXT_ATTEN_CTL_XATTEN1_MARGIN,
935 +                             value);
936 +       }
937 +
938         /* Test value. if 0 then attenuation is unused. Don't load anything. */
939         for (i = 0; i < 3; i++) {
940                 if (ah->txchainmask & BIT(i)) {
941 --- a/drivers/net/wireless/ath/ath9k/link.c
942 +++ b/drivers/net/wireless/ath/ath9k/link.c
943 @@ -28,21 +28,21 @@ void ath_tx_complete_poll_work(struct wo
944         int i;
945         bool needreset = false;
946  
947 -       for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
948 -               if (ATH_TXQ_SETUP(sc, i)) {
949 -                       txq = &sc->tx.txq[i];
950 -                       ath_txq_lock(sc, txq);
951 -                       if (txq->axq_depth) {
952 -                               if (txq->axq_tx_inprogress) {
953 -                                       needreset = true;
954 -                                       ath_txq_unlock(sc, txq);
955 -                                       break;
956 -                               } else {
957 -                                       txq->axq_tx_inprogress = true;
958 -                               }
959 +       for (i = 0; i < IEEE80211_NUM_ACS; i++) {
960 +               txq = sc->tx.txq_map[i];
961 +
962 +               ath_txq_lock(sc, txq);
963 +               if (txq->axq_depth) {
964 +                       if (txq->axq_tx_inprogress) {
965 +                               needreset = true;
966 +                               ath_txq_unlock(sc, txq);
967 +                               break;
968 +                       } else {
969 +                               txq->axq_tx_inprogress = true;
970                         }
971 -                       ath_txq_unlock_complete(sc, txq);
972                 }
973 +               ath_txq_unlock_complete(sc, txq);
974 +       }
975  
976         if (needreset) {
977                 ath_dbg(ath9k_hw_common(sc->sc_ah), RESET,
978 --- a/net/mac80211/sta_info.c
979 +++ b/net/mac80211/sta_info.c
980 @@ -766,6 +766,7 @@ int __must_check __sta_info_destroy(stru
981         struct ieee80211_local *local;
982         struct ieee80211_sub_if_data *sdata;
983         int ret, i;
984 +       bool have_key = false;
985  
986         might_sleep();
987  
988 @@ -793,12 +794,19 @@ int __must_check __sta_info_destroy(stru
989         list_del_rcu(&sta->list);
990  
991         mutex_lock(&local->key_mtx);
992 -       for (i = 0; i < NUM_DEFAULT_KEYS; i++)
993 +       for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
994                 __ieee80211_key_free(key_mtx_dereference(local, sta->gtk[i]));
995 -       if (sta->ptk)
996 +               have_key = true;
997 +       }
998 +       if (sta->ptk) {
999                 __ieee80211_key_free(key_mtx_dereference(local, sta->ptk));
1000 +               have_key = true;
1001 +       }
1002         mutex_unlock(&local->key_mtx);
1003  
1004 +       if (!have_key)
1005 +               synchronize_net();
1006 +
1007         sta->dead = true;
1008  
1009         local->num_sta--;
1010 --- a/net/mac80211/chan.c
1011 +++ b/net/mac80211/chan.c
1012 @@ -63,6 +63,7 @@ ieee80211_new_chanctx(struct ieee80211_l
1013                       enum ieee80211_chanctx_mode mode)
1014  {
1015         struct ieee80211_chanctx *ctx;
1016 +       u32 changed;
1017         int err;
1018  
1019         lockdep_assert_held(&local->chanctx_mtx);
1020 @@ -76,6 +77,13 @@ ieee80211_new_chanctx(struct ieee80211_l
1021         ctx->conf.rx_chains_dynamic = 1;
1022         ctx->mode = mode;
1023  
1024 +       /* acquire mutex to prevent idle from changing */
1025 +       mutex_lock(&local->mtx);
1026 +       /* turn idle off *before* setting channel -- some drivers need that */
1027 +       changed = ieee80211_idle_off(local);
1028 +       if (changed)
1029 +               ieee80211_hw_config(local, changed);
1030 +
1031         if (!local->use_chanctx) {
1032                 local->_oper_channel_type =
1033                         cfg80211_get_chandef_type(chandef);
1034 @@ -85,14 +93,17 @@ ieee80211_new_chanctx(struct ieee80211_l
1035                 err = drv_add_chanctx(local, ctx);
1036                 if (err) {
1037                         kfree(ctx);
1038 -                       return ERR_PTR(err);
1039 +                       ctx = ERR_PTR(err);
1040 +
1041 +                       ieee80211_recalc_idle(local);
1042 +                       goto out;
1043                 }
1044         }
1045  
1046 +       /* and keep the mutex held until the new chanctx is on the list */
1047         list_add_rcu(&ctx->list, &local->chanctx_list);
1048  
1049 -       mutex_lock(&local->mtx);
1050 -       ieee80211_recalc_idle(local);
1051 + out:
1052         mutex_unlock(&local->mtx);
1053  
1054         return ctx;
1055 --- a/net/mac80211/ieee80211_i.h
1056 +++ b/net/mac80211/ieee80211_i.h
1057 @@ -1366,6 +1366,7 @@ int ieee80211_if_change_type(struct ieee
1058                              enum nl80211_iftype type);
1059  void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata);
1060  void ieee80211_remove_interfaces(struct ieee80211_local *local);
1061 +u32 ieee80211_idle_off(struct ieee80211_local *local);
1062  void ieee80211_recalc_idle(struct ieee80211_local *local);
1063  void ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data *sdata,
1064                                     const int offset);
1065 --- a/drivers/net/wireless/ath/ath9k/htc_drv_init.c
1066 +++ b/drivers/net/wireless/ath/ath9k/htc_drv_init.c
1067 @@ -799,7 +799,7 @@ static int ath9k_init_firmware_version(s
1068          * required version.
1069          */
1070         if (priv->fw_version_major != MAJOR_VERSION_REQ ||
1071 -           priv->fw_version_minor != MINOR_VERSION_REQ) {
1072 +           priv->fw_version_minor < MINOR_VERSION_REQ) {
1073                 dev_err(priv->dev, "ath9k_htc: Please upgrade to FW version %d.%d\n",
1074                         MAJOR_VERSION_REQ, MINOR_VERSION_REQ);
1075                 return -EINVAL;
1076 --- a/drivers/net/wireless/ath/ath9k/xmit.c
1077 +++ b/drivers/net/wireless/ath/ath9k/xmit.c
1078 @@ -516,8 +516,7 @@ static void ath_tx_complete_aggr(struct 
1079                  * not a holding desc.
1080                  */
1081                 INIT_LIST_HEAD(&bf_head);
1082 -               if ((sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) ||
1083 -                   bf_next != NULL || !bf_last->bf_stale)
1084 +               if (bf_next != NULL || !bf_last->bf_stale)
1085                         list_move_tail(&bf->list, &bf_head);
1086  
1087                 if (!txpending || (tid->state & AGGR_CLEANUP)) {
1088 @@ -537,8 +536,7 @@ static void ath_tx_complete_aggr(struct 
1089                                 !txfail);
1090                 } else {
1091                         /* retry the un-acked ones */
1092 -                       if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
1093 -                           bf->bf_next == NULL && bf_last->bf_stale) {
1094 +                       if (bf->bf_next == NULL && bf_last->bf_stale) {
1095                                 struct ath_buf *tbf;
1096  
1097                                 tbf = ath_clone_txbuf(sc, bf_last);
1098 @@ -2264,6 +2262,7 @@ void ath_tx_edma_tasklet(struct ath_soft
1099         struct ath_txq *txq;
1100         struct ath_buf *bf, *lastbf;
1101         struct list_head bf_head;
1102 +       struct list_head *fifo_list;
1103         int status;
1104  
1105         for (;;) {
1106 @@ -2291,20 +2290,24 @@ void ath_tx_edma_tasklet(struct ath_soft
1107  
1108                 TX_STAT_INC(txq->axq_qnum, txprocdesc);
1109  
1110 -               if (list_empty(&txq->txq_fifo[txq->txq_tailidx])) {
1111 +               fifo_list = &txq->txq_fifo[txq->txq_tailidx];
1112 +               if (list_empty(fifo_list)) {
1113                         ath_txq_unlock(sc, txq);
1114                         return;
1115                 }
1116  
1117 -               bf = list_first_entry(&txq->txq_fifo[txq->txq_tailidx],
1118 -                                     struct ath_buf, list);
1119 +               bf = list_first_entry(fifo_list, struct ath_buf, list);
1120 +               if (bf->bf_stale) {
1121 +                       list_del(&bf->list);
1122 +                       ath_tx_return_buffer(sc, bf);
1123 +                       bf = list_first_entry(fifo_list, struct ath_buf, list);
1124 +               }
1125 +
1126                 lastbf = bf->bf_lastbf;
1127  
1128                 INIT_LIST_HEAD(&bf_head);
1129 -               list_cut_position(&bf_head, &txq->txq_fifo[txq->txq_tailidx],
1130 -                                 &lastbf->list);
1131 -
1132 -               if (list_empty(&txq->txq_fifo[txq->txq_tailidx])) {
1133 +               if (list_is_last(&lastbf->list, fifo_list)) {
1134 +                       list_splice_tail_init(fifo_list, &bf_head);
1135                         INCR(txq->txq_tailidx, ATH_TXFIFO_DEPTH);
1136  
1137                         if (!list_empty(&txq->axq_q)) {
1138 @@ -2315,6 +2318,11 @@ void ath_tx_edma_tasklet(struct ath_soft
1139                                 list_splice_tail_init(&txq->axq_q, &bf_q);
1140                                 ath_tx_txqaddbuf(sc, txq, &bf_q, true);
1141                         }
1142 +               } else {
1143 +                       lastbf->bf_stale = true;
1144 +                       if (bf != lastbf)
1145 +                               list_cut_position(&bf_head, fifo_list,
1146 +                                                 lastbf->list.prev);
1147                 }
1148  
1149                 ath_tx_process_buffer(sc, txq, &ts, bf, &bf_head);
1150 --- a/drivers/net/wireless/ath/ath9k/ar9580_1p0_initvals.h
1151 +++ b/drivers/net/wireless/ath/ath9k/ar9580_1p0_initvals.h
1152 @@ -519,7 +519,7 @@ static const u32 ar9580_1p0_mac_core[][2
1153         {0x00008258, 0x00000000},
1154         {0x0000825c, 0x40000000},
1155         {0x00008260, 0x00080922},
1156 -       {0x00008264, 0x9bc00010},
1157 +       {0x00008264, 0x9d400010},
1158         {0x00008268, 0xffffffff},
1159         {0x0000826c, 0x0000ffff},
1160         {0x00008270, 0x00000000},
1161 --- a/net/mac80211/mlme.c
1162 +++ b/net/mac80211/mlme.c
1163 @@ -3955,8 +3955,16 @@ int ieee80211_mgd_auth(struct ieee80211_
1164         /* prep auth_data so we don't go into idle on disassoc */
1165         ifmgd->auth_data = auth_data;
1166  
1167 -       if (ifmgd->associated)
1168 -               ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
1169 +       if (ifmgd->associated) {
1170 +               u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
1171 +
1172 +               ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
1173 +                                      WLAN_REASON_UNSPECIFIED,
1174 +                                      false, frame_buf);
1175 +
1176 +               __cfg80211_send_deauth(sdata->dev, frame_buf,
1177 +                                      sizeof(frame_buf));
1178 +       }
1179  
1180         sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
1181  
1182 @@ -4016,8 +4024,16 @@ int ieee80211_mgd_assoc(struct ieee80211
1183  
1184         mutex_lock(&ifmgd->mtx);
1185  
1186 -       if (ifmgd->associated)
1187 -               ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
1188 +       if (ifmgd->associated) {
1189 +               u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
1190 +
1191 +               ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
1192 +                                      WLAN_REASON_UNSPECIFIED,
1193 +                                      false, frame_buf);
1194 +
1195 +               __cfg80211_send_deauth(sdata->dev, frame_buf,
1196 +                                      sizeof(frame_buf));
1197 +       }
1198  
1199         if (ifmgd->auth_data && !ifmgd->auth_data->done) {
1200                 err = -EBUSY;
1201 --- a/net/mac80211/rc80211_minstrel.c
1202 +++ b/net/mac80211/rc80211_minstrel.c
1203 @@ -55,7 +55,6 @@
1204  #include "rate.h"
1205  #include "rc80211_minstrel.h"
1206  
1207 -#define SAMPLE_COLUMNS 10
1208  #define SAMPLE_TBL(_mi, _idx, _col) \
1209                 _mi->sample_table[(_idx * SAMPLE_COLUMNS) + _col]
1210  
1211 @@ -70,16 +69,31 @@ rix_to_ndx(struct minstrel_sta_info *mi,
1212         return i;
1213  }
1214  
1215 +/* find & sort topmost throughput rates */
1216 +static inline void
1217 +minstrel_sort_best_tp_rates(struct minstrel_sta_info *mi, int i, u8 *tp_list)
1218 +{
1219 +       int j = MAX_THR_RATES;
1220 +
1221 +       while (j > 0 && mi->r[i].cur_tp > mi->r[tp_list[j - 1]].cur_tp)
1222 +               j--;
1223 +       if (j < MAX_THR_RATES - 1)
1224 +               memmove(&tp_list[j + 1], &tp_list[j], MAX_THR_RATES - (j + 1));
1225 +       if (j < MAX_THR_RATES)
1226 +               tp_list[j] = i;
1227 +}
1228 +
1229  static void
1230  minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
1231  {
1232 -       u32 max_tp = 0, index_max_tp = 0, index_max_tp2 = 0;
1233 -       u32 max_prob = 0, index_max_prob = 0;
1234 +       u8 tmp_tp_rate[MAX_THR_RATES];
1235 +       u8 tmp_prob_rate = 0;
1236         u32 usecs;
1237 -       u32 p;
1238         int i;
1239  
1240 -       mi->stats_update = jiffies;
1241 +       for (i=0; i < MAX_THR_RATES; i++)
1242 +           tmp_tp_rate[i] = 0;
1243 +
1244         for (i = 0; i < mi->n_rates; i++) {
1245                 struct minstrel_rate *mr = &mi->r[i];
1246  
1247 @@ -87,27 +101,32 @@ minstrel_update_stats(struct minstrel_pr
1248                 if (!usecs)
1249                         usecs = 1000000;
1250  
1251 -               /* To avoid rounding issues, probabilities scale from 0 (0%)
1252 -                * to 18000 (100%) */
1253 -               if (mr->attempts) {
1254 -                       p = (mr->success * 18000) / mr->attempts;
1255 +               if (unlikely(mr->attempts > 0)) {
1256 +                       mr->sample_skipped = 0;
1257 +                       mr->cur_prob = MINSTREL_FRAC(mr->success, mr->attempts);
1258                         mr->succ_hist += mr->success;
1259                         mr->att_hist += mr->attempts;
1260 -                       mr->cur_prob = p;
1261 -                       p = ((p * (100 - mp->ewma_level)) + (mr->probability *
1262 -                               mp->ewma_level)) / 100;
1263 -                       mr->probability = p;
1264 -                       mr->cur_tp = p * (1000000 / usecs);
1265 -               }
1266 +                       mr->probability = minstrel_ewma(mr->probability,
1267 +                                                       mr->cur_prob,
1268 +                                                       EWMA_LEVEL);
1269 +               } else
1270 +                       mr->sample_skipped++;
1271  
1272                 mr->last_success = mr->success;
1273                 mr->last_attempts = mr->attempts;
1274                 mr->success = 0;
1275                 mr->attempts = 0;
1276  
1277 +               /* Update throughput per rate, reset thr. below 10% success */
1278 +               if (mr->probability < MINSTREL_FRAC(10, 100))
1279 +                       mr->cur_tp = 0;
1280 +               else
1281 +                       mr->cur_tp = mr->probability * (1000000 / usecs);
1282 +
1283                 /* Sample less often below the 10% chance of success.
1284                  * Sample less often above the 95% chance of success. */
1285 -               if ((mr->probability > 17100) || (mr->probability < 1800)) {
1286 +               if (mr->probability > MINSTREL_FRAC(95, 100) ||
1287 +                   mr->probability < MINSTREL_FRAC(10, 100)) {
1288                         mr->adjusted_retry_count = mr->retry_count >> 1;
1289                         if (mr->adjusted_retry_count > 2)
1290                                 mr->adjusted_retry_count = 2;
1291 @@ -118,35 +137,30 @@ minstrel_update_stats(struct minstrel_pr
1292                 }
1293                 if (!mr->adjusted_retry_count)
1294                         mr->adjusted_retry_count = 2;
1295 -       }
1296  
1297 -       for (i = 0; i < mi->n_rates; i++) {
1298 -               struct minstrel_rate *mr = &mi->r[i];
1299 -               if (max_tp < mr->cur_tp) {
1300 -                       index_max_tp = i;
1301 -                       max_tp = mr->cur_tp;
1302 -               }
1303 -               if (max_prob < mr->probability) {
1304 -                       index_max_prob = i;
1305 -                       max_prob = mr->probability;
1306 +               minstrel_sort_best_tp_rates(mi, i, tmp_tp_rate);
1307 +
1308 +               /* To determine the most robust rate (max_prob_rate) used at
1309 +                * 3rd mmr stage we distinct between two cases:
1310 +                * (1) if any success probabilitiy >= 95%, out of those rates
1311 +                * choose the maximum throughput rate as max_prob_rate
1312 +                * (2) if all success probabilities < 95%, the rate with
1313 +                * highest success probability is choosen as max_prob_rate */
1314 +               if (mr->probability >= MINSTREL_FRAC(95,100)) {
1315 +                       if (mr->cur_tp >= mi->r[tmp_prob_rate].cur_tp)
1316 +                               tmp_prob_rate = i;
1317 +               } else {
1318 +                       if (mr->probability >= mi->r[tmp_prob_rate].probability)
1319 +                               tmp_prob_rate = i;
1320                 }
1321         }
1322  
1323 -       max_tp = 0;
1324 -       for (i = 0; i < mi->n_rates; i++) {
1325 -               struct minstrel_rate *mr = &mi->r[i];
1326 -
1327 -               if (i == index_max_tp)
1328 -                       continue;
1329 +       /* Assign the new rate set */
1330 +       memcpy(mi->max_tp_rate, tmp_tp_rate, sizeof(mi->max_tp_rate));
1331 +       mi->max_prob_rate = tmp_prob_rate;
1332  
1333 -               if (max_tp < mr->cur_tp) {
1334 -                       index_max_tp2 = i;
1335 -                       max_tp = mr->cur_tp;
1336 -               }
1337 -       }
1338 -       mi->max_tp_rate = index_max_tp;
1339 -       mi->max_tp_rate2 = index_max_tp2;
1340 -       mi->max_prob_rate = index_max_prob;
1341 +       /* Reset update timer */
1342 +       mi->stats_update = jiffies;
1343  }
1344  
1345  static void
1346 @@ -207,10 +221,10 @@ static int
1347  minstrel_get_next_sample(struct minstrel_sta_info *mi)
1348  {
1349         unsigned int sample_ndx;
1350 -       sample_ndx = SAMPLE_TBL(mi, mi->sample_idx, mi->sample_column);
1351 -       mi->sample_idx++;
1352 -       if ((int) mi->sample_idx > (mi->n_rates - 2)) {
1353 -               mi->sample_idx = 0;
1354 +       sample_ndx = SAMPLE_TBL(mi, mi->sample_row, mi->sample_column);
1355 +       mi->sample_row++;
1356 +       if ((int) mi->sample_row >= mi->n_rates) {
1357 +               mi->sample_row = 0;
1358                 mi->sample_column++;
1359                 if (mi->sample_column >= SAMPLE_COLUMNS)
1360                         mi->sample_column = 0;
1361 @@ -228,31 +242,37 @@ minstrel_get_rate(void *priv, struct iee
1362         struct minstrel_priv *mp = priv;
1363         struct ieee80211_tx_rate *ar = info->control.rates;
1364         unsigned int ndx, sample_ndx = 0;
1365 -       bool mrr;
1366 -       bool sample_slower = false;
1367 -       bool sample = false;
1368 +       bool mrr_capable;
1369 +       bool indirect_rate_sampling = false;
1370 +       bool rate_sampling = false;
1371         int i, delta;
1372         int mrr_ndx[3];
1373 -       int sample_rate;
1374 +       int sampling_ratio;
1375  
1376 +       /* management/no-ack frames do not use rate control */
1377         if (rate_control_send_low(sta, priv_sta, txrc))
1378                 return;
1379  
1380 -       mrr = mp->has_mrr && !txrc->rts && !txrc->bss_conf->use_cts_prot;
1381 -
1382 -       ndx = mi->max_tp_rate;
1383 -
1384 -       if (mrr)
1385 -               sample_rate = mp->lookaround_rate_mrr;
1386 +       /* check multi-rate-retry capabilities & adjust lookaround_rate */
1387 +       mrr_capable = mp->has_mrr &&
1388 +                     !txrc->rts &&
1389 +                     !txrc->bss_conf->use_cts_prot;
1390 +       if (mrr_capable)
1391 +               sampling_ratio = mp->lookaround_rate_mrr;
1392         else
1393 -               sample_rate = mp->lookaround_rate;
1394 +               sampling_ratio = mp->lookaround_rate;
1395 +
1396 +       /* init rateindex [ndx] with max throughput rate */
1397 +       ndx = mi->max_tp_rate[0];
1398  
1399 +       /* increase sum packet counter */
1400         mi->packet_count++;
1401 -       delta = (mi->packet_count * sample_rate / 100) -
1402 +
1403 +       delta = (mi->packet_count * sampling_ratio / 100) -
1404                         (mi->sample_count + mi->sample_deferred / 2);
1405  
1406         /* delta > 0: sampling required */
1407 -       if ((delta > 0) && (mrr || !mi->prev_sample)) {
1408 +       if ((delta > 0) && (mrr_capable || !mi->prev_sample)) {
1409                 struct minstrel_rate *msr;
1410                 if (mi->packet_count >= 10000) {
1411                         mi->sample_deferred = 0;
1412 @@ -271,21 +291,28 @@ minstrel_get_rate(void *priv, struct iee
1413                         mi->sample_count += (delta - mi->n_rates * 2);
1414                 }
1415  
1416 +               /* get next random rate sample */
1417                 sample_ndx = minstrel_get_next_sample(mi);
1418                 msr = &mi->r[sample_ndx];
1419 -               sample = true;
1420 -               sample_slower = mrr && (msr->perfect_tx_time >
1421 -                       mi->r[ndx].perfect_tx_time);
1422 +               rate_sampling = true;
1423  
1424 -               if (!sample_slower) {
1425 +               /* Decide if direct ( 1st mrr stage) or indirect (2nd mrr stage)
1426 +                * rate sampling method should be used.
1427 +                * Respect such rates that are not sampled for 20 interations.
1428 +                */
1429 +               if (mrr_capable &&
1430 +                   msr->perfect_tx_time > mi->r[ndx].perfect_tx_time &&
1431 +                   msr->sample_skipped < 20)
1432 +                               indirect_rate_sampling = true;
1433 +
1434 +               if (!indirect_rate_sampling) {
1435                         if (msr->sample_limit != 0) {
1436                                 ndx = sample_ndx;
1437                                 mi->sample_count++;
1438                                 if (msr->sample_limit > 0)
1439                                         msr->sample_limit--;
1440 -                       } else {
1441 -                               sample = false;
1442 -                       }
1443 +                       } else
1444 +                               rate_sampling = false;
1445                 } else {
1446                         /* Only use IEEE80211_TX_CTL_RATE_CTRL_PROBE to mark
1447                          * packets that have the sampling rate deferred to the
1448 @@ -297,34 +324,39 @@ minstrel_get_rate(void *priv, struct iee
1449                         mi->sample_deferred++;
1450                 }
1451         }
1452 -       mi->prev_sample = sample;
1453 +       mi->prev_sample = rate_sampling;
1454  
1455         /* If we're not using MRR and the sampling rate already
1456          * has a probability of >95%, we shouldn't be attempting
1457          * to use it, as this only wastes precious airtime */
1458 -       if (!mrr && sample && (mi->r[ndx].probability > 17100))
1459 -               ndx = mi->max_tp_rate;
1460 +       if (!mrr_capable && rate_sampling &&
1461 +          (mi->r[ndx].probability > MINSTREL_FRAC(95, 100)))
1462 +               ndx = mi->max_tp_rate[0];
1463  
1464 +       /* mrr setup for 1st stage */
1465         ar[0].idx = mi->r[ndx].rix;
1466         ar[0].count = minstrel_get_retry_count(&mi->r[ndx], info);
1467  
1468 -       if (!mrr) {
1469 -               if (!sample)
1470 +       /* non mrr setup for 2nd stage */
1471 +       if (!mrr_capable) {
1472 +               if (!rate_sampling)
1473                         ar[0].count = mp->max_retry;
1474                 ar[1].idx = mi->lowest_rix;
1475                 ar[1].count = mp->max_retry;
1476                 return;
1477         }
1478  
1479 -       /* MRR setup */
1480 -       if (sample) {
1481 -               if (sample_slower)
1482 +       /* mrr setup for 2nd stage */
1483 +       if (rate_sampling) {
1484 +               if (indirect_rate_sampling)
1485                         mrr_ndx[0] = sample_ndx;
1486                 else
1487 -                       mrr_ndx[0] = mi->max_tp_rate;
1488 +                       mrr_ndx[0] = mi->max_tp_rate[0];
1489         } else {
1490 -               mrr_ndx[0] = mi->max_tp_rate2;
1491 +               mrr_ndx[0] = mi->max_tp_rate[1];
1492         }
1493 +
1494 +       /* mrr setup for 3rd & 4th stage */
1495         mrr_ndx[1] = mi->max_prob_rate;
1496         mrr_ndx[2] = 0;
1497         for (i = 1; i < 4; i++) {
1498 @@ -351,26 +383,21 @@ static void
1499  init_sample_table(struct minstrel_sta_info *mi)
1500  {
1501         unsigned int i, col, new_idx;
1502 -       unsigned int n_srates = mi->n_rates - 1;
1503         u8 rnd[8];
1504  
1505         mi->sample_column = 0;
1506 -       mi->sample_idx = 0;
1507 -       memset(mi->sample_table, 0, SAMPLE_COLUMNS * mi->n_rates);
1508 +       mi->sample_row = 0;
1509 +       memset(mi->sample_table, 0xff, SAMPLE_COLUMNS * mi->n_rates);
1510  
1511         for (col = 0; col < SAMPLE_COLUMNS; col++) {
1512 -               for (i = 0; i < n_srates; i++) {
1513 +               for (i = 0; i < mi->n_rates; i++) {
1514                         get_random_bytes(rnd, sizeof(rnd));
1515 -                       new_idx = (i + rnd[i & 7]) % n_srates;
1516 +                       new_idx = (i + rnd[i & 7]) % mi->n_rates;
1517  
1518 -                       while (SAMPLE_TBL(mi, new_idx, col) != 0)
1519 -                               new_idx = (new_idx + 1) % n_srates;
1520 +                       while (SAMPLE_TBL(mi, new_idx, col) != 0xff)
1521 +                               new_idx = (new_idx + 1) % mi->n_rates;
1522  
1523 -                       /* Don't sample the slowest rate (i.e. slowest base
1524 -                        * rate). We must presume that the slowest rate works
1525 -                        * fine, or else other management frames will also be
1526 -                        * failing and the link will break */
1527 -                       SAMPLE_TBL(mi, new_idx, col) = i + 1;
1528 +                       SAMPLE_TBL(mi, new_idx, col) = i;
1529                 }
1530         }
1531  }
1532 @@ -542,9 +569,6 @@ minstrel_alloc(struct ieee80211_hw *hw, 
1533         mp->lookaround_rate = 5;
1534         mp->lookaround_rate_mrr = 10;
1535  
1536 -       /* moving average weight for EWMA */
1537 -       mp->ewma_level = 75;
1538 -
1539         /* maximum time that the hw is allowed to stay in one MRR segment */
1540         mp->segment_size = 6000;
1541  
1542 --- a/net/mac80211/rc80211_minstrel.h
1543 +++ b/net/mac80211/rc80211_minstrel.h
1544 @@ -9,6 +9,28 @@
1545  #ifndef __RC_MINSTREL_H
1546  #define __RC_MINSTREL_H
1547  
1548 +#define EWMA_LEVEL     75      /* ewma weighting factor [%] */
1549 +#define SAMPLE_COLUMNS 10      /* number of columns in sample table */
1550 +
1551 +
1552 +/* scaled fraction values */
1553 +#define MINSTREL_SCALE  16
1554 +#define MINSTREL_FRAC(val, div) (((val) << MINSTREL_SCALE) / div)
1555 +#define MINSTREL_TRUNC(val) ((val) >> MINSTREL_SCALE)
1556 +
1557 +/* number of highest throughput rates to consider*/
1558 +#define MAX_THR_RATES 4
1559 +
1560 +/*
1561 + * Perform EWMA (Exponentially Weighted Moving Average) calculation
1562 +  */
1563 +static inline int
1564 +minstrel_ewma(int old, int new, int weight)
1565 +{
1566 +       return (new * (100 - weight) + old * weight) / 100;
1567 +}
1568 +
1569 +
1570  struct minstrel_rate {
1571         int bitrate;
1572         int rix;
1573 @@ -26,6 +48,7 @@ struct minstrel_rate {
1574         u32 attempts;
1575         u32 last_attempts;
1576         u32 last_success;
1577 +       u8 sample_skipped;
1578  
1579         /* parts per thousand */
1580         u32 cur_prob;
1581 @@ -45,14 +68,13 @@ struct minstrel_sta_info {
1582  
1583         unsigned int lowest_rix;
1584  
1585 -       unsigned int max_tp_rate;
1586 -       unsigned int max_tp_rate2;
1587 -       unsigned int max_prob_rate;
1588 +       u8 max_tp_rate[MAX_THR_RATES];
1589 +       u8 max_prob_rate;
1590         unsigned int packet_count;
1591         unsigned int sample_count;
1592         int sample_deferred;
1593  
1594 -       unsigned int sample_idx;
1595 +       unsigned int sample_row;
1596         unsigned int sample_column;
1597  
1598         int n_rates;
1599 @@ -73,7 +95,6 @@ struct minstrel_priv {
1600         unsigned int cw_min;
1601         unsigned int cw_max;
1602         unsigned int max_retry;
1603 -       unsigned int ewma_level;
1604         unsigned int segment_size;
1605         unsigned int update_interval;
1606         unsigned int lookaround_rate;
1607 --- a/net/mac80211/rc80211_minstrel_debugfs.c
1608 +++ b/net/mac80211/rc80211_minstrel_debugfs.c
1609 @@ -73,15 +73,17 @@ minstrel_stats_open(struct inode *inode,
1610         for (i = 0; i < mi->n_rates; i++) {
1611                 struct minstrel_rate *mr = &mi->r[i];
1612  
1613 -               *(p++) = (i == mi->max_tp_rate) ? 'T' : ' ';
1614 -               *(p++) = (i == mi->max_tp_rate2) ? 't' : ' ';
1615 +               *(p++) = (i == mi->max_tp_rate[0]) ? 'A' : ' ';
1616 +               *(p++) = (i == mi->max_tp_rate[1]) ? 'B' : ' ';
1617 +               *(p++) = (i == mi->max_tp_rate[2]) ? 'C' : ' ';
1618 +               *(p++) = (i == mi->max_tp_rate[3]) ? 'D' : ' ';
1619                 *(p++) = (i == mi->max_prob_rate) ? 'P' : ' ';
1620                 p += sprintf(p, "%3u%s", mr->bitrate / 2,
1621                                 (mr->bitrate & 1 ? ".5" : "  "));
1622  
1623 -               tp = mr->cur_tp / ((18000 << 10) / 96);
1624 -               prob = mr->cur_prob / 18;
1625 -               eprob = mr->probability / 18;
1626 +               tp = MINSTREL_TRUNC(mr->cur_tp / 10);
1627 +               prob = MINSTREL_TRUNC(mr->cur_prob * 1000);
1628 +               eprob = MINSTREL_TRUNC(mr->probability * 1000);
1629  
1630                 p += sprintf(p, "  %6u.%1u   %6u.%1u   %6u.%1u        "
1631                                 "%3u(%3u)   %8llu    %8llu\n",