ath9k: merge a fix for tx processing issues with aggregation
[openwrt.git] / package / kernel / mac80211 / patches / 300-pending_work.patch
1 --- a/net/mac80211/agg-rx.c
2 +++ b/net/mac80211/agg-rx.c
3 @@ -204,6 +204,8 @@ static void ieee80211_send_addba_resp(st
4                 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
5         else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
6                 memcpy(mgmt->bssid, sdata->u.ibss.bssid, ETH_ALEN);
7 +       else if (sdata->vif.type == NL80211_IFTYPE_WDS)
8 +               memcpy(mgmt->bssid, da, ETH_ALEN);
9  
10         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
11                                           IEEE80211_STYPE_ACTION);
12 --- a/net/mac80211/agg-tx.c
13 +++ b/net/mac80211/agg-tx.c
14 @@ -81,7 +81,8 @@ static void ieee80211_send_addba_request
15         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
16         if (sdata->vif.type == NL80211_IFTYPE_AP ||
17             sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
18 -           sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
19 +           sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
20 +           sdata->vif.type == NL80211_IFTYPE_WDS)
21                 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
22         else if (sdata->vif.type == NL80211_IFTYPE_STATION)
23                 memcpy(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN);
24 @@ -527,6 +528,7 @@ int ieee80211_start_tx_ba_session(struct
25             sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
26             sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
27             sdata->vif.type != NL80211_IFTYPE_AP &&
28 +           sdata->vif.type != NL80211_IFTYPE_WDS &&
29             sdata->vif.type != NL80211_IFTYPE_ADHOC)
30                 return -EINVAL;
31  
32 --- a/net/mac80211/debugfs_sta.c
33 +++ b/net/mac80211/debugfs_sta.c
34 @@ -66,11 +66,11 @@ static ssize_t sta_flags_read(struct fil
35         test_sta_flag(sta, WLAN_STA_##flg) ? #flg "\n" : ""
36  
37         int res = scnprintf(buf, sizeof(buf),
38 -                           "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
39 +                           "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
40                             TEST(AUTH), TEST(ASSOC), TEST(PS_STA),
41                             TEST(PS_DRIVER), TEST(AUTHORIZED),
42                             TEST(SHORT_PREAMBLE),
43 -                           TEST(WME), TEST(WDS), TEST(CLEAR_PS_FILT),
44 +                           TEST(WME), TEST(CLEAR_PS_FILT),
45                             TEST(MFP), TEST(BLOCK_BA), TEST(PSPOLL),
46                             TEST(UAPSD), TEST(SP), TEST(TDLS_PEER),
47                             TEST(TDLS_PEER_AUTH), TEST(4ADDR_EVENT),
48 @@ -455,6 +455,15 @@ void ieee80211_sta_debugfs_add(struct st
49         DEBUGFS_ADD_COUNTER(tx_retry_count, tx_retry_count);
50         DEBUGFS_ADD_COUNTER(wep_weak_iv_count, wep_weak_iv_count);
51  
52 +       if (sizeof(sta->driver_buffered_tids) == sizeof(u32))
53 +               debugfs_create_x32("driver_buffered_tids", 0400,
54 +                                  sta->debugfs.dir,
55 +                                  (u32 *)&sta->driver_buffered_tids);
56 +       else
57 +               debugfs_create_x64("driver_buffered_tids", 0400,
58 +                                  sta->debugfs.dir,
59 +                                  (u64 *)&sta->driver_buffered_tids);
60 +
61         drv_sta_add_debugfs(local, sdata, &sta->sta, sta->debugfs.dir);
62  }
63  
64 --- a/net/mac80211/iface.c
65 +++ b/net/mac80211/iface.c
66 @@ -274,6 +274,12 @@ static int ieee80211_check_concurrent_if
67                         if (iftype == NL80211_IFTYPE_ADHOC &&
68                             nsdata->vif.type == NL80211_IFTYPE_ADHOC)
69                                 return -EBUSY;
70 +                       /*
71 +                        * will not add another interface while any channel
72 +                        * switch is active.
73 +                        */
74 +                       if (nsdata->vif.csa_active)
75 +                               return -EBUSY;
76  
77                         /*
78                          * The remaining checks are only performed for interfaces
79 @@ -463,7 +469,6 @@ int ieee80211_do_open(struct wireless_de
80         struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
81         struct net_device *dev = wdev->netdev;
82         struct ieee80211_local *local = sdata->local;
83 -       struct sta_info *sta;
84         u32 changed = 0;
85         int res;
86         u32 hw_reconf_flags = 0;
87 @@ -629,30 +634,8 @@ int ieee80211_do_open(struct wireless_de
88  
89         set_bit(SDATA_STATE_RUNNING, &sdata->state);
90  
91 -       if (sdata->vif.type == NL80211_IFTYPE_WDS) {
92 -               /* Create STA entry for the WDS peer */
93 -               sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
94 -                                    GFP_KERNEL);
95 -               if (!sta) {
96 -                       res = -ENOMEM;
97 -                       goto err_del_interface;
98 -               }
99 -
100 -               sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
101 -               sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
102 -               sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
103 -
104 -               res = sta_info_insert(sta);
105 -               if (res) {
106 -                       /* STA has been freed */
107 -                       goto err_del_interface;
108 -               }
109 -
110 -               rate_control_rate_init(sta);
111 -               netif_carrier_on(dev);
112 -       } else if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
113 +       if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE)
114                 rcu_assign_pointer(local->p2p_sdata, sdata);
115 -       }
116  
117         /*
118          * set_multicast_list will be invoked by the networking core
119 @@ -809,6 +792,8 @@ static void ieee80211_do_stop(struct iee
120         cancel_work_sync(&local->dynamic_ps_enable_work);
121  
122         cancel_work_sync(&sdata->recalc_smps);
123 +       sdata->vif.csa_active = false;
124 +       cancel_work_sync(&sdata->csa_finalize_work);
125  
126         cancel_delayed_work_sync(&sdata->dfs_cac_timer_work);
127  
128 @@ -1116,6 +1101,74 @@ static void ieee80211_if_setup(struct ne
129         dev->destructor = free_netdev;
130  }
131  
132 +static void ieee80211_wds_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
133 +                                        struct sk_buff *skb)
134 +{
135 +       struct ieee80211_local *local = sdata->local;
136 +       struct ieee80211_rx_status *rx_status;
137 +       struct ieee802_11_elems elems;
138 +       struct ieee80211_mgmt *mgmt;
139 +       struct sta_info *sta;
140 +       size_t baselen;
141 +       u32 rates = 0;
142 +       u16 stype;
143 +       bool new = false;
144 +       enum ieee80211_band band;
145 +       struct ieee80211_supported_band *sband;
146 +
147 +       rx_status = IEEE80211_SKB_RXCB(skb);
148 +       band = rx_status->band;
149 +       sband = local->hw.wiphy->bands[band];
150 +       mgmt = (struct ieee80211_mgmt *) skb->data;
151 +       stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
152 +
153 +       if (stype != IEEE80211_STYPE_BEACON)
154 +               return;
155 +
156 +       baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
157 +       if (baselen > skb->len)
158 +               return;
159 +
160 +       ieee802_11_parse_elems(mgmt->u.probe_resp.variable,
161 +                              skb->len - baselen, false, &elems);
162 +
163 +       rates = ieee80211_sta_get_rates(local, &elems, band, NULL);
164 +
165 +       rcu_read_lock();
166 +
167 +       sta = sta_info_get(sdata, sdata->u.wds.remote_addr);
168 +
169 +       if (!sta) {
170 +               rcu_read_unlock();
171 +               sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
172 +                                    GFP_KERNEL);
173 +               if (!sta)
174 +                       return;
175 +
176 +               new = true;
177 +       }
178 +
179 +       sta->last_rx = jiffies;
180 +       sta->sta.supp_rates[band] = rates;
181 +
182 +       if (elems.ht_cap_elem)
183 +               ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
184 +                               elems.ht_cap_elem, sta);
185 +
186 +       if (elems.wmm_param)
187 +               set_sta_flag(sta, WLAN_STA_WME);
188 +
189 +       if (new) {
190 +               sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
191 +               sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
192 +               sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
193 +               rate_control_rate_init(sta);
194 +               sta_info_insert_rcu(sta);
195 +       }
196 +
197 +       rcu_read_unlock();
198 +}
199 +
200  static void ieee80211_iface_work(struct work_struct *work)
201  {
202         struct ieee80211_sub_if_data *sdata =
203 @@ -1220,6 +1273,9 @@ static void ieee80211_iface_work(struct 
204                                 break;
205                         ieee80211_mesh_rx_queued_mgmt(sdata, skb);
206                         break;
207 +               case NL80211_IFTYPE_WDS:
208 +                       ieee80211_wds_rx_queued_mgmt(sdata, skb);
209 +                       break;
210                 default:
211                         WARN(1, "frame for unexpected interface type");
212                         break;
213 @@ -1282,6 +1338,7 @@ static void ieee80211_setup_sdata(struct
214         skb_queue_head_init(&sdata->skb_queue);
215         INIT_WORK(&sdata->work, ieee80211_iface_work);
216         INIT_WORK(&sdata->recalc_smps, ieee80211_recalc_smps_work);
217 +       INIT_WORK(&sdata->csa_finalize_work, ieee80211_csa_finalize_work);
218  
219         switch (type) {
220         case NL80211_IFTYPE_P2P_GO:
221 --- a/net/mac80211/rc80211_minstrel_ht.c
222 +++ b/net/mac80211/rc80211_minstrel_ht.c
223 @@ -365,6 +365,14 @@ minstrel_ht_update_stats(struct minstrel
224                 }
225         }
226  
227 +#ifdef CPTCFG_MAC80211_DEBUGFS
228 +       /* use fixed index if set */
229 +       if (mp->fixed_rate_idx != -1) {
230 +               mi->max_tp_rate = mp->fixed_rate_idx;
231 +               mi->max_tp_rate2 = mp->fixed_rate_idx;
232 +               mi->max_prob_rate = mp->fixed_rate_idx;
233 +       }
234 +#endif
235  
236         mi->stats_update = jiffies;
237  }
238 @@ -774,6 +782,11 @@ minstrel_ht_get_rate(void *priv, struct 
239         info->flags |= mi->tx_flags;
240         minstrel_ht_check_cck_shortpreamble(mp, mi, txrc->short_preamble);
241  
242 +#ifdef CPTCFG_MAC80211_DEBUGFS
243 +       if (mp->fixed_rate_idx != -1)
244 +               return;
245 +#endif
246 +
247         /* Don't use EAPOL frames for sampling on non-mrr hw */
248         if (mp->hw->max_rates == 1 &&
249             txrc->skb->protocol == cpu_to_be16(ETH_P_PAE))
250 @@ -781,16 +794,6 @@ minstrel_ht_get_rate(void *priv, struct 
251         else
252                 sample_idx = minstrel_get_sample_rate(mp, mi);
253  
254 -#ifdef CPTCFG_MAC80211_DEBUGFS
255 -       /* use fixed index if set */
256 -       if (mp->fixed_rate_idx != -1) {
257 -               mi->max_tp_rate = mp->fixed_rate_idx;
258 -               mi->max_tp_rate2 = mp->fixed_rate_idx;
259 -               mi->max_prob_rate = mp->fixed_rate_idx;
260 -               sample_idx = -1;
261 -       }
262 -#endif
263 -
264         mi->total_packets++;
265  
266         /* wraparound */
267 @@ -804,10 +807,18 @@ minstrel_ht_get_rate(void *priv, struct 
268  
269         sample_group = &minstrel_mcs_groups[sample_idx / MCS_GROUP_RATES];
270         info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
271 +       rate->count = 1;
272 +
273 +       if (sample_idx / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) {
274 +               int idx = sample_idx % ARRAY_SIZE(mp->cck_rates);
275 +               rate->idx = mp->cck_rates[idx];
276 +               rate->flags = 0;
277 +               return;
278 +       }
279 +
280         rate->idx = sample_idx % MCS_GROUP_RATES +
281                     (sample_group->streams - 1) * MCS_GROUP_RATES;
282         rate->flags = IEEE80211_TX_RC_MCS | sample_group->flags;
283 -       rate->count = 1;
284  }
285  
286  static void
287 @@ -820,6 +831,9 @@ minstrel_ht_update_cck(struct minstrel_p
288         if (sband->band != IEEE80211_BAND_2GHZ)
289                 return;
290  
291 +       if (!(mp->hw->flags & IEEE80211_HW_SUPPORTS_HT_CCK_RATES))
292 +               return;
293 +
294         mi->cck_supported = 0;
295         mi->cck_supported_short = 0;
296         for (i = 0; i < 4; i++) {
297 --- a/net/mac80211/rx.c
298 +++ b/net/mac80211/rx.c
299 @@ -936,8 +936,14 @@ ieee80211_rx_h_check(struct ieee80211_rx
300         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
301         struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
302  
303 -       /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
304 -       if (rx->sta && !is_multicast_ether_addr(hdr->addr1)) {
305 +       /*
306 +        * Drop duplicate 802.11 retransmissions
307 +        * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
308 +        */
309 +       if (rx->skb->len >= 24 && rx->sta &&
310 +           !ieee80211_is_ctl(hdr->frame_control) &&
311 +           !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
312 +           !is_multicast_ether_addr(hdr->addr1)) {
313                 if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
314                              rx->sta->last_seq_ctrl[rx->seqno_idx] ==
315                              hdr->seq_ctrl)) {
316 @@ -2369,6 +2375,7 @@ ieee80211_rx_h_action(struct ieee80211_r
317                     sdata->vif.type != NL80211_IFTYPE_MESH_POINT &&
318                     sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
319                     sdata->vif.type != NL80211_IFTYPE_AP &&
320 +                   sdata->vif.type != NL80211_IFTYPE_WDS &&
321                     sdata->vif.type != NL80211_IFTYPE_ADHOC)
322                         break;
323  
324 @@ -2720,14 +2727,15 @@ ieee80211_rx_h_mgmt(struct ieee80211_rx_
325  
326         if (!ieee80211_vif_is_mesh(&sdata->vif) &&
327             sdata->vif.type != NL80211_IFTYPE_ADHOC &&
328 -           sdata->vif.type != NL80211_IFTYPE_STATION)
329 +           sdata->vif.type != NL80211_IFTYPE_STATION &&
330 +           sdata->vif.type != NL80211_IFTYPE_WDS)
331                 return RX_DROP_MONITOR;
332  
333         switch (stype) {
334         case cpu_to_le16(IEEE80211_STYPE_AUTH):
335         case cpu_to_le16(IEEE80211_STYPE_BEACON):
336         case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
337 -               /* process for all: mesh, mlme, ibss */
338 +               /* process for all: mesh, mlme, ibss, wds */
339                 break;
340         case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
341         case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
342 @@ -3059,10 +3067,16 @@ static int prepare_for_handlers(struct i
343                 }
344                 break;
345         case NL80211_IFTYPE_WDS:
346 -               if (bssid || !ieee80211_is_data(hdr->frame_control))
347 -                       return 0;
348                 if (!ether_addr_equal(sdata->u.wds.remote_addr, hdr->addr2))
349                         return 0;
350 +
351 +               if (ieee80211_is_data(hdr->frame_control) ||
352 +                   ieee80211_is_action(hdr->frame_control)) {
353 +                       if (compare_ether_addr(sdata->vif.addr, hdr->addr1))
354 +                               return 0;
355 +               } else if (!ieee80211_is_beacon(hdr->frame_control))
356 +                       return 0;
357 +
358                 break;
359         case NL80211_IFTYPE_P2P_DEVICE:
360                 if (!ieee80211_is_public_action(hdr, skb->len) &&
361 --- a/net/mac80211/sta_info.h
362 +++ b/net/mac80211/sta_info.h
363 @@ -32,7 +32,6 @@
364   * @WLAN_STA_SHORT_PREAMBLE: Station is capable of receiving short-preamble
365   *     frames.
366   * @WLAN_STA_WME: Station is a QoS-STA.
367 - * @WLAN_STA_WDS: Station is one of our WDS peers.
368   * @WLAN_STA_CLEAR_PS_FILT: Clear PS filter in hardware (using the
369   *     IEEE80211_TX_CTL_CLEAR_PS_FILT control flag) when the next
370   *     frame to this station is transmitted.
371 @@ -66,7 +65,6 @@ enum ieee80211_sta_info_flags {
372         WLAN_STA_AUTHORIZED,
373         WLAN_STA_SHORT_PREAMBLE,
374         WLAN_STA_WME,
375 -       WLAN_STA_WDS,
376         WLAN_STA_CLEAR_PS_FILT,
377         WLAN_STA_MFP,
378         WLAN_STA_BLOCK_BA,
379 --- a/drivers/net/wireless/ath/ath9k/xmit.c
380 +++ b/drivers/net/wireless/ath/ath9k/xmit.c
381 @@ -135,6 +135,9 @@ static struct ath_frame_info *get_frame_
382  
383  static void ath_send_bar(struct ath_atx_tid *tid, u16 seqno)
384  {
385 +       if (!tid->an->sta)
386 +               return;
387 +
388         ieee80211_send_bar(tid->an->vif, tid->an->sta->addr, tid->tidno,
389                            seqno << IEEE80211_SEQ_SEQ_SHIFT);
390  }
391 @@ -146,6 +149,93 @@ static void ath_set_rates(struct ieee802
392                                ARRAY_SIZE(bf->rates));
393  }
394  
395 +static void ath_txq_skb_done(struct ath_softc *sc, struct ath_txq *txq,
396 +                            struct sk_buff *skb)
397 +{
398 +       int q;
399 +
400 +       q = skb_get_queue_mapping(skb);
401 +       if (txq == sc->tx.uapsdq)
402 +               txq = sc->tx.txq_map[q];
403 +
404 +       if (txq != sc->tx.txq_map[q])
405 +               return;
406 +
407 +       if (WARN_ON(--txq->pending_frames < 0))
408 +               txq->pending_frames = 0;
409 +
410 +       if (txq->stopped &&
411 +           txq->pending_frames < sc->tx.txq_max_pending[q]) {
412 +               ieee80211_wake_queue(sc->hw, q);
413 +               txq->stopped = false;
414 +       }
415 +}
416 +
417 +static struct ath_atx_tid *
418 +ath_get_skb_tid(struct ath_softc *sc, struct ath_node *an, struct sk_buff *skb)
419 +{
420 +       struct ieee80211_hdr *hdr;
421 +       u8 tidno = 0;
422 +
423 +       hdr = (struct ieee80211_hdr *) skb->data;
424 +       if (ieee80211_is_data_qos(hdr->frame_control))
425 +               tidno = ieee80211_get_qos_ctl(hdr)[0];
426 +
427 +       tidno &= IEEE80211_QOS_CTL_TID_MASK;
428 +       return ATH_AN_2_TID(an, tidno);
429 +}
430 +
431 +static bool ath_tid_has_buffered(struct ath_atx_tid *tid)
432 +{
433 +       return !skb_queue_empty(&tid->buf_q) || !skb_queue_empty(&tid->retry_q);
434 +}
435 +
436 +static struct sk_buff *ath_tid_dequeue(struct ath_atx_tid *tid)
437 +{
438 +       struct sk_buff *skb;
439 +
440 +       skb = __skb_dequeue(&tid->retry_q);
441 +       if (!skb)
442 +               skb = __skb_dequeue(&tid->buf_q);
443 +
444 +       return skb;
445 +}
446 +
447 +/*
448 + * ath_tx_tid_change_state:
449 + * - clears a-mpdu flag of previous session
450 + * - force sequence number allocation to fix next BlockAck Window
451 + */
452 +static void
453 +ath_tx_tid_change_state(struct ath_softc *sc, struct ath_atx_tid *tid)
454 +{
455 +       struct ath_txq *txq = tid->ac->txq;
456 +       struct ieee80211_tx_info *tx_info;
457 +       struct sk_buff *skb, *tskb;
458 +       struct ath_buf *bf;
459 +       struct ath_frame_info *fi;
460 +
461 +       skb_queue_walk_safe(&tid->buf_q, skb, tskb) {
462 +               fi = get_frame_info(skb);
463 +               bf = fi->bf;
464 +
465 +               tx_info = IEEE80211_SKB_CB(skb);
466 +               tx_info->flags &= ~IEEE80211_TX_CTL_AMPDU;
467 +
468 +               if (bf)
469 +                       continue;
470 +
471 +               bf = ath_tx_setup_buffer(sc, txq, tid, skb);
472 +               if (!bf) {
473 +                       __skb_unlink(skb, &tid->buf_q);
474 +                       ath_txq_skb_done(sc, txq, skb);
475 +                       ieee80211_free_txskb(sc->hw, skb);
476 +                       continue;
477 +               }
478 +       }
479 +
480 +}
481 +
482  static void ath_tx_flush_tid(struct ath_softc *sc, struct ath_atx_tid *tid)
483  {
484         struct ath_txq *txq = tid->ac->txq;
485 @@ -160,27 +250,22 @@ static void ath_tx_flush_tid(struct ath_
486  
487         memset(&ts, 0, sizeof(ts));
488  
489 -       while ((skb = __skb_dequeue(&tid->buf_q))) {
490 +       while ((skb = __skb_dequeue(&tid->retry_q))) {
491                 fi = get_frame_info(skb);
492                 bf = fi->bf;
493 -
494                 if (!bf) {
495 -                       bf = ath_tx_setup_buffer(sc, txq, tid, skb);
496 -                       if (!bf) {
497 -                               ieee80211_free_txskb(sc->hw, skb);
498 -                               continue;
499 -                       }
500 +                       ath_txq_skb_done(sc, txq, skb);
501 +                       ieee80211_free_txskb(sc->hw, skb);
502 +                       continue;
503                 }
504  
505 -               if (fi->retries) {
506 -                       list_add_tail(&bf->list, &bf_head);
507 +               if (fi->baw_tracked) {
508                         ath_tx_update_baw(sc, tid, bf->bf_state.seqno);
509 -                       ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, 0);
510                         sendbar = true;
511 -               } else {
512 -                       ath_set_rates(tid->an->vif, tid->an->sta, bf);
513 -                       ath_tx_send_normal(sc, txq, NULL, skb);
514                 }
515 +
516 +               list_add_tail(&bf->list, &bf_head);
517 +               ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, 0);
518         }
519  
520         if (sendbar) {
521 @@ -209,13 +294,16 @@ static void ath_tx_update_baw(struct ath
522  }
523  
524  static void ath_tx_addto_baw(struct ath_softc *sc, struct ath_atx_tid *tid,
525 -                            u16 seqno)
526 +                            struct ath_buf *bf)
527  {
528 +       struct ath_frame_info *fi = get_frame_info(bf->bf_mpdu);
529 +       u16 seqno = bf->bf_state.seqno;
530         int index, cindex;
531  
532         index  = ATH_BA_INDEX(tid->seq_start, seqno);
533         cindex = (tid->baw_head + index) & (ATH_TID_MAX_BUFS - 1);
534         __set_bit(cindex, tid->tx_buf);
535 +       fi->baw_tracked = 1;
536  
537         if (index >= ((tid->baw_tail - tid->baw_head) &
538                 (ATH_TID_MAX_BUFS - 1))) {
539 @@ -224,12 +312,6 @@ static void ath_tx_addto_baw(struct ath_
540         }
541  }
542  
543 -/*
544 - * TODO: For frame(s) that are in the retry state, we will reuse the
545 - * sequence number(s) without setting the retry bit. The
546 - * alternative is to give up on these and BAR the receiver's window
547 - * forward.
548 - */
549  static void ath_tid_drain(struct ath_softc *sc, struct ath_txq *txq,
550                           struct ath_atx_tid *tid)
551  
552 @@ -243,7 +325,7 @@ static void ath_tid_drain(struct ath_sof
553         memset(&ts, 0, sizeof(ts));
554         INIT_LIST_HEAD(&bf_head);
555  
556 -       while ((skb = __skb_dequeue(&tid->buf_q))) {
557 +       while ((skb = ath_tid_dequeue(tid))) {
558                 fi = get_frame_info(skb);
559                 bf = fi->bf;
560  
561 @@ -253,14 +335,8 @@ static void ath_tid_drain(struct ath_sof
562                 }
563  
564                 list_add_tail(&bf->list, &bf_head);
565 -
566 -               ath_tx_update_baw(sc, tid, bf->bf_state.seqno);
567                 ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, 0);
568         }
569 -
570 -       tid->seq_next = tid->seq_start;
571 -       tid->baw_tail = tid->baw_head;
572 -       tid->bar_index = -1;
573  }
574  
575  static void ath_tx_set_retry(struct ath_softc *sc, struct ath_txq *txq,
576 @@ -323,6 +399,7 @@ static struct ath_buf* ath_clone_txbuf(s
577         tbf->bf_buf_addr = bf->bf_buf_addr;
578         memcpy(tbf->bf_desc, bf->bf_desc, sc->sc_ah->caps.tx_desc_len);
579         tbf->bf_state = bf->bf_state;
580 +       tbf->bf_state.stale = false;
581  
582         return tbf;
583  }
584 @@ -380,7 +457,6 @@ static void ath_tx_complete_aggr(struct 
585         struct ieee80211_tx_rate rates[4];
586         struct ath_frame_info *fi;
587         int nframes;
588 -       u8 tidno;
589         bool flush = !!(ts->ts_status & ATH9K_TX_FLUSH);
590         int i, retries;
591         int bar_index = -1;
592 @@ -406,7 +482,7 @@ static void ath_tx_complete_aggr(struct 
593                 while (bf) {
594                         bf_next = bf->bf_next;
595  
596 -                       if (!bf->bf_stale || bf_next != NULL)
597 +                       if (!bf->bf_state.stale || bf_next != NULL)
598                                 list_move_tail(&bf->list, &bf_head);
599  
600                         ath_tx_complete_buf(sc, bf, txq, &bf_head, ts, 0);
601 @@ -417,8 +493,7 @@ static void ath_tx_complete_aggr(struct 
602         }
603  
604         an = (struct ath_node *)sta->drv_priv;
605 -       tidno = ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
606 -       tid = ATH_AN_2_TID(an, tidno);
607 +       tid = ath_get_skb_tid(sc, an, skb);
608         seq_first = tid->seq_start;
609         isba = ts->ts_flags & ATH9K_TX_BA;
610  
611 @@ -430,7 +505,7 @@ static void ath_tx_complete_aggr(struct 
612          * Only BlockAcks have a TID and therefore normal Acks cannot be
613          * checked
614          */
615 -       if (isba && tidno != ts->tid)
616 +       if (isba && tid->tidno != ts->tid)
617                 txok = false;
618  
619         isaggr = bf_isaggr(bf);
620 @@ -466,7 +541,8 @@ static void ath_tx_complete_aggr(struct 
621                 tx_info = IEEE80211_SKB_CB(skb);
622                 fi = get_frame_info(skb);
623  
624 -               if (!BAW_WITHIN(tid->seq_start, tid->baw_size, seqno)) {
625 +               if (!BAW_WITHIN(tid->seq_start, tid->baw_size, seqno) ||
626 +                   !tid->active) {
627                         /*
628                          * Outside of the current BlockAck window,
629                          * maybe part of a previous session
630 @@ -499,7 +575,7 @@ static void ath_tx_complete_aggr(struct 
631                  * not a holding desc.
632                  */
633                 INIT_LIST_HEAD(&bf_head);
634 -               if (bf_next != NULL || !bf_last->bf_stale)
635 +               if (bf_next != NULL || !bf_last->bf_state.stale)
636                         list_move_tail(&bf->list, &bf_head);
637  
638                 if (!txpending) {
639 @@ -523,7 +599,7 @@ static void ath_tx_complete_aggr(struct 
640                                 ieee80211_sta_eosp(sta);
641                         }
642                         /* retry the un-acked ones */
643 -                       if (bf->bf_next == NULL && bf_last->bf_stale) {
644 +                       if (bf->bf_next == NULL && bf_last->bf_state.stale) {
645                                 struct ath_buf *tbf;
646  
647                                 tbf = ath_clone_txbuf(sc, bf_last);
648 @@ -560,7 +636,7 @@ static void ath_tx_complete_aggr(struct 
649                 if (an->sleeping)
650                         ieee80211_sta_set_buffered(sta, tid->tidno, true);
651  
652 -               skb_queue_splice(&bf_pending, &tid->buf_q);
653 +               skb_queue_splice_tail(&bf_pending, &tid->retry_q);
654                 if (!an->sleeping) {
655                         ath_tx_queue_tid(txq, tid);
656  
657 @@ -618,7 +694,7 @@ static void ath_tx_process_buffer(struct
658         } else
659                 ath_tx_complete_aggr(sc, txq, bf, bf_head, ts, txok);
660  
661 -       if ((sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_HT) && !flush)
662 +       if (!flush)
663                 ath_txq_schedule(sc, txq);
664  }
665  
666 @@ -792,15 +868,20 @@ static int ath_compute_num_delims(struct
667  
668  static struct ath_buf *
669  ath_tx_get_tid_subframe(struct ath_softc *sc, struct ath_txq *txq,
670 -                       struct ath_atx_tid *tid)
671 +                       struct ath_atx_tid *tid, struct sk_buff_head **q)
672  {
673 +       struct ieee80211_tx_info *tx_info;
674         struct ath_frame_info *fi;
675         struct sk_buff *skb;
676         struct ath_buf *bf;
677         u16 seqno;
678  
679         while (1) {
680 -               skb = skb_peek(&tid->buf_q);
681 +               *q = &tid->retry_q;
682 +               if (skb_queue_empty(*q))
683 +                       *q = &tid->buf_q;
684 +
685 +               skb = skb_peek(*q);
686                 if (!skb)
687                         break;
688  
689 @@ -808,13 +889,26 @@ ath_tx_get_tid_subframe(struct ath_softc
690                 bf = fi->bf;
691                 if (!fi->bf)
692                         bf = ath_tx_setup_buffer(sc, txq, tid, skb);
693 +               else
694 +                       bf->bf_state.stale = false;
695  
696                 if (!bf) {
697 -                       __skb_unlink(skb, &tid->buf_q);
698 +                       __skb_unlink(skb, *q);
699 +                       ath_txq_skb_done(sc, txq, skb);
700                         ieee80211_free_txskb(sc->hw, skb);
701                         continue;
702                 }
703  
704 +               bf->bf_next = NULL;
705 +               bf->bf_lastbf = bf;
706 +
707 +               tx_info = IEEE80211_SKB_CB(skb);
708 +               tx_info->flags &= ~IEEE80211_TX_CTL_CLEAR_PS_FILT;
709 +               if (!(tx_info->flags & IEEE80211_TX_CTL_AMPDU)) {
710 +                       bf->bf_state.bf_type = 0;
711 +                       return bf;
712 +               }
713 +
714                 bf->bf_state.bf_type = BUF_AMPDU | BUF_AGGR;
715                 seqno = bf->bf_state.seqno;
716  
717 @@ -828,73 +922,52 @@ ath_tx_get_tid_subframe(struct ath_softc
718  
719                         INIT_LIST_HEAD(&bf_head);
720                         list_add(&bf->list, &bf_head);
721 -                       __skb_unlink(skb, &tid->buf_q);
722 +                       __skb_unlink(skb, *q);
723                         ath_tx_update_baw(sc, tid, seqno);
724                         ath_tx_complete_buf(sc, bf, txq, &bf_head, &ts, 0);
725                         continue;
726                 }
727  
728 -               bf->bf_next = NULL;
729 -               bf->bf_lastbf = bf;
730                 return bf;
731         }
732  
733         return NULL;
734  }
735  
736 -static enum ATH_AGGR_STATUS ath_tx_form_aggr(struct ath_softc *sc,
737 -                                            struct ath_txq *txq,
738 -                                            struct ath_atx_tid *tid,
739 -                                            struct list_head *bf_q,
740 -                                            int *aggr_len)
741 +static bool
742 +ath_tx_form_aggr(struct ath_softc *sc, struct ath_txq *txq,
743 +                struct ath_atx_tid *tid, struct list_head *bf_q,
744 +                struct ath_buf *bf_first, struct sk_buff_head *tid_q,
745 +                int *aggr_len)
746  {
747  #define PADBYTES(_len) ((4 - ((_len) % 4)) % 4)
748 -       struct ath_buf *bf, *bf_first = NULL, *bf_prev = NULL;
749 -       int rl = 0, nframes = 0, ndelim, prev_al = 0;
750 +       struct ath_buf *bf = bf_first, *bf_prev = NULL;
751 +       int nframes = 0, ndelim;
752         u16 aggr_limit = 0, al = 0, bpad = 0,
753 -               al_delta, h_baw = tid->baw_size / 2;
754 -       enum ATH_AGGR_STATUS status = ATH_AGGR_DONE;
755 +           al_delta, h_baw = tid->baw_size / 2;
756         struct ieee80211_tx_info *tx_info;
757         struct ath_frame_info *fi;
758         struct sk_buff *skb;
759 +       bool closed = false;
760  
761 -       do {
762 -               bf = ath_tx_get_tid_subframe(sc, txq, tid);
763 -               if (!bf) {
764 -                       status = ATH_AGGR_BAW_CLOSED;
765 -                       break;
766 -               }
767 +       bf = bf_first;
768 +       aggr_limit = ath_lookup_rate(sc, bf, tid);
769  
770 +       do {
771                 skb = bf->bf_mpdu;
772                 fi = get_frame_info(skb);
773  
774 -               if (!bf_first)
775 -                       bf_first = bf;
776 -
777 -               if (!rl) {
778 -                       ath_set_rates(tid->an->vif, tid->an->sta, bf);
779 -                       aggr_limit = ath_lookup_rate(sc, bf, tid);
780 -                       rl = 1;
781 -               }
782 -
783                 /* do not exceed aggregation limit */
784                 al_delta = ATH_AGGR_DELIM_SZ + fi->framelen;
785 +               if (nframes) {
786 +                       if (aggr_limit < al + bpad + al_delta ||
787 +                           ath_lookup_legacy(bf) || nframes >= h_baw)
788 +                               break;
789  
790 -               if (nframes &&
791 -                   ((aggr_limit < (al + bpad + al_delta + prev_al)) ||
792 -                    ath_lookup_legacy(bf))) {
793 -                       status = ATH_AGGR_LIMITED;
794 -                       break;
795 -               }
796 -
797 -               tx_info = IEEE80211_SKB_CB(bf->bf_mpdu);
798 -               if (nframes && (tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE))
799 -                       break;
800 -
801 -               /* do not exceed subframe limit */
802 -               if (nframes >= min((int)h_baw, ATH_AMPDU_SUBFRAME_DEFAULT)) {
803 -                       status = ATH_AGGR_LIMITED;
804 -                       break;
805 +                       tx_info = IEEE80211_SKB_CB(bf->bf_mpdu);
806 +                       if ((tx_info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) ||
807 +                           !(tx_info->flags & IEEE80211_TX_CTL_AMPDU))
808 +                               break;
809                 }
810  
811                 /* add padding for previous frame to aggregation length */
812 @@ -912,22 +985,37 @@ static enum ATH_AGGR_STATUS ath_tx_form_
813                 bf->bf_next = NULL;
814  
815                 /* link buffers of this frame to the aggregate */
816 -               if (!fi->retries)
817 -                       ath_tx_addto_baw(sc, tid, bf->bf_state.seqno);
818 +               if (!fi->baw_tracked)
819 +                       ath_tx_addto_baw(sc, tid, bf);
820                 bf->bf_state.ndelim = ndelim;
821  
822 -               __skb_unlink(skb, &tid->buf_q);
823 +               __skb_unlink(skb, tid_q);
824                 list_add_tail(&bf->list, bf_q);
825                 if (bf_prev)
826                         bf_prev->bf_next = bf;
827  
828                 bf_prev = bf;
829  
830 -       } while (!skb_queue_empty(&tid->buf_q));
831 +               bf = ath_tx_get_tid_subframe(sc, txq, tid, &tid_q);
832 +               if (!bf) {
833 +                       closed = true;
834 +                       break;
835 +               }
836 +       } while (ath_tid_has_buffered(tid));
837 +
838 +       bf = bf_first;
839 +       bf->bf_lastbf = bf_prev;
840 +
841 +       if (bf == bf_prev) {
842 +               al = get_frame_info(bf->bf_mpdu)->framelen;
843 +               bf->bf_state.bf_type = BUF_AMPDU;
844 +       } else {
845 +               TX_STAT_INC(txq->axq_qnum, a_aggr);
846 +       }
847  
848         *aggr_len = al;
849  
850 -       return status;
851 +       return closed;
852  #undef PADBYTES
853  }
854  
855 @@ -999,7 +1087,7 @@ void ath_update_max_aggr_framelen(struct
856  }
857  
858  static void ath_buf_set_rate(struct ath_softc *sc, struct ath_buf *bf,
859 -                            struct ath_tx_info *info, int len)
860 +                            struct ath_tx_info *info, int len, bool rts)
861  {
862         struct ath_hw *ah = sc->sc_ah;
863         struct sk_buff *skb;
864 @@ -1008,6 +1096,7 @@ static void ath_buf_set_rate(struct ath_
865         const struct ieee80211_rate *rate;
866         struct ieee80211_hdr *hdr;
867         struct ath_frame_info *fi = get_frame_info(bf->bf_mpdu);
868 +       u32 rts_thresh = sc->hw->wiphy->rts_threshold;
869         int i;
870         u8 rix = 0;
871  
872 @@ -1030,7 +1119,17 @@ static void ath_buf_set_rate(struct ath_
873                 rix = rates[i].idx;
874                 info->rates[i].Tries = rates[i].count;
875  
876 -                   if (rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) {
877 +               /*
878 +                * Handle RTS threshold for unaggregated HT frames.
879 +                */
880 +               if (bf_isampdu(bf) && !bf_isaggr(bf) &&
881 +                   (rates[i].flags & IEEE80211_TX_RC_MCS) &&
882 +                   unlikely(rts_thresh != (u32) -1)) {
883 +                       if (!rts_thresh || (len > rts_thresh))
884 +                               rts = true;
885 +               }
886 +
887 +               if (rts || rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) {
888                         info->rates[i].RateFlags |= ATH9K_RATESERIES_RTS_CTS;
889                         info->flags |= ATH9K_TXDESC_RTSENA;
890                 } else if (rates[i].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
891 @@ -1123,6 +1222,8 @@ static void ath_tx_fill_desc(struct ath_
892         struct ath_hw *ah = sc->sc_ah;
893         struct ath_buf *bf_first = NULL;
894         struct ath_tx_info info;
895 +       u32 rts_thresh = sc->hw->wiphy->rts_threshold;
896 +       bool rts = false;
897  
898         memset(&info, 0, sizeof(info));
899         info.is_first = true;
900 @@ -1159,7 +1260,22 @@ static void ath_tx_fill_desc(struct ath_
901                                 info.flags |= (u32) bf->bf_state.bfs_paprd <<
902                                               ATH9K_TXDESC_PAPRD_S;
903  
904 -                       ath_buf_set_rate(sc, bf, &info, len);
905 +                       /*
906 +                        * mac80211 doesn't handle RTS threshold for HT because
907 +                        * the decision has to be taken based on AMPDU length
908 +                        * and aggregation is done entirely inside ath9k.
909 +                        * Set the RTS/CTS flag for the first subframe based
910 +                        * on the threshold.
911 +                        */
912 +                       if (aggr && (bf == bf_first) &&
913 +                           unlikely(rts_thresh != (u32) -1)) {
914 +                               /*
915 +                                * "len" is the size of the entire AMPDU.
916 +                                */
917 +                               if (!rts_thresh || (len > rts_thresh))
918 +                                       rts = true;
919 +                       }
920 +                       ath_buf_set_rate(sc, bf, &info, len, rts);
921                 }
922  
923                 info.buf_addr[0] = bf->bf_buf_addr;
924 @@ -1188,53 +1304,86 @@ static void ath_tx_fill_desc(struct ath_
925         }
926  }
927  
928 -static void ath_tx_sched_aggr(struct ath_softc *sc, struct ath_txq *txq,
929 -                             struct ath_atx_tid *tid)
930 +static void
931 +ath_tx_form_burst(struct ath_softc *sc, struct ath_txq *txq,
932 +                 struct ath_atx_tid *tid, struct list_head *bf_q,
933 +                 struct ath_buf *bf_first, struct sk_buff_head *tid_q)
934  {
935 -       struct ath_buf *bf;
936 -       enum ATH_AGGR_STATUS status;
937 -       struct ieee80211_tx_info *tx_info;
938 -       struct list_head bf_q;
939 -       int aggr_len;
940 +       struct ath_buf *bf = bf_first, *bf_prev = NULL;
941 +       struct sk_buff *skb;
942 +       int nframes = 0;
943  
944         do {
945 -               if (skb_queue_empty(&tid->buf_q))
946 -                       return;
947 +               struct ieee80211_tx_info *tx_info;
948 +               skb = bf->bf_mpdu;
949  
950 -               INIT_LIST_HEAD(&bf_q);
951 +               nframes++;
952 +               __skb_unlink(skb, tid_q);
953 +               list_add_tail(&bf->list, bf_q);
954 +               if (bf_prev)
955 +                       bf_prev->bf_next = bf;
956 +               bf_prev = bf;
957  
958 -               status = ath_tx_form_aggr(sc, txq, tid, &bf_q, &aggr_len);
959 +               if (nframes >= 2)
960 +                       break;
961  
962 -               /*
963 -                * no frames picked up to be aggregated;
964 -                * block-ack window is not open.
965 -                */
966 -               if (list_empty(&bf_q))
967 +               bf = ath_tx_get_tid_subframe(sc, txq, tid, &tid_q);
968 +               if (!bf)
969                         break;
970  
971 -               bf = list_first_entry(&bf_q, struct ath_buf, list);
972 -               bf->bf_lastbf = list_entry(bf_q.prev, struct ath_buf, list);
973                 tx_info = IEEE80211_SKB_CB(bf->bf_mpdu);
974 +               if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
975 +                       break;
976  
977 -               if (tid->ac->clear_ps_filter) {
978 -                       tid->ac->clear_ps_filter = false;
979 -                       tx_info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
980 -               } else {
981 -                       tx_info->flags &= ~IEEE80211_TX_CTL_CLEAR_PS_FILT;
982 -               }
983 +               ath_set_rates(tid->an->vif, tid->an->sta, bf);
984 +       } while (1);
985 +}
986  
987 -               /* if only one frame, send as non-aggregate */
988 -               if (bf == bf->bf_lastbf) {
989 -                       aggr_len = get_frame_info(bf->bf_mpdu)->framelen;
990 -                       bf->bf_state.bf_type = BUF_AMPDU;
991 -               } else {
992 -                       TX_STAT_INC(txq->axq_qnum, a_aggr);
993 -               }
994 +static bool ath_tx_sched_aggr(struct ath_softc *sc, struct ath_txq *txq,
995 +                             struct ath_atx_tid *tid, bool *stop)
996 +{
997 +       struct ath_buf *bf;
998 +       struct ieee80211_tx_info *tx_info;
999 +       struct sk_buff_head *tid_q;
1000 +       struct list_head bf_q;
1001 +       int aggr_len = 0;
1002 +       bool aggr, last = true;
1003 +
1004 +       if (!ath_tid_has_buffered(tid))
1005 +               return false;
1006 +
1007 +       INIT_LIST_HEAD(&bf_q);
1008 +
1009 +       bf = ath_tx_get_tid_subframe(sc, txq, tid, &tid_q);
1010 +       if (!bf)
1011 +               return false;
1012 +
1013 +       tx_info = IEEE80211_SKB_CB(bf->bf_mpdu);
1014 +       aggr = !!(tx_info->flags & IEEE80211_TX_CTL_AMPDU);
1015 +       if ((aggr && txq->axq_ampdu_depth >= ATH_AGGR_MIN_QDEPTH) ||
1016 +               (!aggr && txq->axq_depth >= ATH_NON_AGGR_MIN_QDEPTH)) {
1017 +               *stop = true;
1018 +               return false;
1019 +       }
1020  
1021 -               ath_tx_fill_desc(sc, bf, txq, aggr_len);
1022 -               ath_tx_txqaddbuf(sc, txq, &bf_q, false);
1023 -       } while (txq->axq_ampdu_depth < ATH_AGGR_MIN_QDEPTH &&
1024 -                status != ATH_AGGR_BAW_CLOSED);
1025 +       ath_set_rates(tid->an->vif, tid->an->sta, bf);
1026 +       if (aggr)
1027 +               last = ath_tx_form_aggr(sc, txq, tid, &bf_q, bf,
1028 +                                       tid_q, &aggr_len);
1029 +       else
1030 +               ath_tx_form_burst(sc, txq, tid, &bf_q, bf, tid_q);
1031 +
1032 +       if (list_empty(&bf_q))
1033 +               return false;
1034 +
1035 +       if (tid->ac->clear_ps_filter || tid->an->no_ps_filter) {
1036 +               tid->ac->clear_ps_filter = false;
1037 +               tx_info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1038 +       }
1039 +
1040 +       ath_tx_fill_desc(sc, bf, txq, aggr_len);
1041 +       ath_tx_txqaddbuf(sc, txq, &bf_q, false);
1042 +       return true;
1043  }
1044  
1045  int ath_tx_aggr_start(struct ath_softc *sc, struct ieee80211_sta *sta,
1046 @@ -1258,6 +1407,9 @@ int ath_tx_aggr_start(struct ath_softc *
1047                 an->mpdudensity = density;
1048         }
1049  
1050 +       /* force sequence number allocation for pending frames */
1051 +       ath_tx_tid_change_state(sc, txtid);
1052 +
1053         txtid->active = true;
1054         txtid->paused = true;
1055         *ssn = txtid->seq_start = txtid->seq_next;
1056 @@ -1277,8 +1429,9 @@ void ath_tx_aggr_stop(struct ath_softc *
1057  
1058         ath_txq_lock(sc, txq);
1059         txtid->active = false;
1060 -       txtid->paused = true;
1061 +       txtid->paused = false;
1062         ath_tx_flush_tid(sc, txtid);
1063 +       ath_tx_tid_change_state(sc, txtid);
1064         ath_txq_unlock_complete(sc, txq);
1065  }
1066  
1067 @@ -1302,7 +1455,7 @@ void ath_tx_aggr_sleep(struct ieee80211_
1068  
1069                 ath_txq_lock(sc, txq);
1070  
1071 -               buffered = !skb_queue_empty(&tid->buf_q);
1072 +               buffered = ath_tid_has_buffered(tid);
1073  
1074                 tid->sched = false;
1075                 list_del(&tid->list);
1076 @@ -1334,7 +1487,7 @@ void ath_tx_aggr_wakeup(struct ath_softc
1077                 ath_txq_lock(sc, txq);
1078                 ac->clear_ps_filter = true;
1079  
1080 -               if (!skb_queue_empty(&tid->buf_q) && !tid->paused) {
1081 +               if (!tid->paused && ath_tid_has_buffered(tid)) {
1082                         ath_tx_queue_tid(txq, tid);
1083                         ath_txq_schedule(sc, txq);
1084                 }
1085 @@ -1359,7 +1512,7 @@ void ath_tx_aggr_resume(struct ath_softc
1086         tid->baw_size = IEEE80211_MIN_AMPDU_BUF << sta->ht_cap.ampdu_factor;
1087         tid->paused = false;
1088  
1089 -       if (!skb_queue_empty(&tid->buf_q)) {
1090 +       if (ath_tid_has_buffered(tid)) {
1091                 ath_tx_queue_tid(txq, tid);
1092                 ath_txq_schedule(sc, txq);
1093         }
1094 @@ -1379,6 +1532,7 @@ void ath9k_release_buffered_frames(struc
1095         struct ieee80211_tx_info *info;
1096         struct list_head bf_q;
1097         struct ath_buf *bf_tail = NULL, *bf;
1098 +       struct sk_buff_head *tid_q;
1099         int sent = 0;
1100         int i;
1101  
1102 @@ -1394,15 +1548,15 @@ void ath9k_release_buffered_frames(struc
1103                         continue;
1104  
1105                 ath_txq_lock(sc, tid->ac->txq);
1106 -               while (!skb_queue_empty(&tid->buf_q) && nframes > 0) {
1107 -                       bf = ath_tx_get_tid_subframe(sc, sc->tx.uapsdq, tid);
1108 +               while (nframes > 0) {
1109 +                       bf = ath_tx_get_tid_subframe(sc, sc->tx.uapsdq, tid, &tid_q);
1110                         if (!bf)
1111                                 break;
1112  
1113 -                       __skb_unlink(bf->bf_mpdu, &tid->buf_q);
1114 +                       __skb_unlink(bf->bf_mpdu, tid_q);
1115                         list_add_tail(&bf->list, &bf_q);
1116                         ath_set_rates(tid->an->vif, tid->an->sta, bf);
1117 -                       ath_tx_addto_baw(sc, tid, bf->bf_state.seqno);
1118 +                       ath_tx_addto_baw(sc, tid, bf);
1119                         bf->bf_state.bf_type &= ~BUF_AGGR;
1120                         if (bf_tail)
1121                                 bf_tail->bf_next = bf;
1122 @@ -1412,7 +1566,7 @@ void ath9k_release_buffered_frames(struc
1123                         sent++;
1124                         TX_STAT_INC(txq->axq_qnum, a_queued_hw);
1125  
1126 -                       if (skb_queue_empty(&tid->buf_q))
1127 +                       if (an->sta && !ath_tid_has_buffered(tid))
1128                                 ieee80211_sta_set_buffered(an->sta, i, false);
1129                 }
1130                 ath_txq_unlock_complete(sc, tid->ac->txq);
1131 @@ -1571,7 +1725,7 @@ static void ath_drain_txq_list(struct at
1132         while (!list_empty(list)) {
1133                 bf = list_first_entry(list, struct ath_buf, list);
1134  
1135 -               if (bf->bf_stale) {
1136 +               if (bf->bf_state.stale) {
1137                         list_del(&bf->list);
1138  
1139                         ath_tx_return_buffer(sc, bf);
1140 @@ -1665,25 +1819,27 @@ void ath_tx_cleanupq(struct ath_softc *s
1141   */
1142  void ath_txq_schedule(struct ath_softc *sc, struct ath_txq *txq)
1143  {
1144 -       struct ath_atx_ac *ac, *ac_tmp, *last_ac;
1145 +       struct ath_atx_ac *ac, *last_ac;
1146         struct ath_atx_tid *tid, *last_tid;
1147 +       bool sent = false;
1148  
1149         if (test_bit(SC_OP_HW_RESET, &sc->sc_flags) ||
1150 -           list_empty(&txq->axq_acq) ||
1151 -           txq->axq_ampdu_depth >= ATH_AGGR_MIN_QDEPTH)
1152 +           list_empty(&txq->axq_acq))
1153                 return;
1154  
1155         rcu_read_lock();
1156  
1157 -       ac = list_first_entry(&txq->axq_acq, struct ath_atx_ac, list);
1158         last_ac = list_entry(txq->axq_acq.prev, struct ath_atx_ac, list);
1159 +       while (!list_empty(&txq->axq_acq)) {
1160 +               bool stop = false;
1161  
1162 -       list_for_each_entry_safe(ac, ac_tmp, &txq->axq_acq, list) {
1163 +               ac = list_first_entry(&txq->axq_acq, struct ath_atx_ac, list);
1164                 last_tid = list_entry(ac->tid_q.prev, struct ath_atx_tid, list);
1165                 list_del(&ac->list);
1166                 ac->sched = false;
1167  
1168                 while (!list_empty(&ac->tid_q)) {
1169 +
1170                         tid = list_first_entry(&ac->tid_q, struct ath_atx_tid,
1171                                                list);
1172                         list_del(&tid->list);
1173 @@ -1692,17 +1848,17 @@ void ath_txq_schedule(struct ath_softc *
1174                         if (tid->paused)
1175                                 continue;
1176  
1177 -                       ath_tx_sched_aggr(sc, txq, tid);
1178 +                       if (ath_tx_sched_aggr(sc, txq, tid, &stop))
1179 +                               sent = true;
1180  
1181                         /*
1182                          * add tid to round-robin queue if more frames
1183                          * are pending for the tid
1184                          */
1185 -                       if (!skb_queue_empty(&tid->buf_q))
1186 +                       if (ath_tid_has_buffered(tid))
1187                                 ath_tx_queue_tid(txq, tid);
1188  
1189 -                       if (tid == last_tid ||
1190 -                           txq->axq_ampdu_depth >= ATH_AGGR_MIN_QDEPTH)
1191 +                       if (stop || tid == last_tid)
1192                                 break;
1193                 }
1194  
1195 @@ -1711,9 +1867,17 @@ void ath_txq_schedule(struct ath_softc *
1196                         list_add_tail(&ac->list, &txq->axq_acq);
1197                 }
1198  
1199 -               if (ac == last_ac ||
1200 -                   txq->axq_ampdu_depth >= ATH_AGGR_MIN_QDEPTH)
1201 +               if (stop)
1202                         break;
1203 +
1204 +               if (ac == last_ac) {
1205 +                       if (!sent)
1206 +                               break;
1207 +
1208 +                       sent = false;
1209 +                       last_ac = list_entry(txq->axq_acq.prev,
1210 +                                            struct ath_atx_ac, list);
1211 +               }
1212         }
1213  
1214         rcu_read_unlock();
1215 @@ -1792,57 +1956,6 @@ static void ath_tx_txqaddbuf(struct ath_
1216         }
1217  }
1218  
1219 -static void ath_tx_send_ampdu(struct ath_softc *sc, struct ath_txq *txq,
1220 -                             struct ath_atx_tid *tid, struct sk_buff *skb,
1221 -                             struct ath_tx_control *txctl)
1222 -{
1223 -       struct ath_frame_info *fi = get_frame_info(skb);
1224 -       struct list_head bf_head;
1225 -       struct ath_buf *bf;
1226 -
1227 -       /*
1228 -        * Do not queue to h/w when any of the following conditions is true:
1229 -        * - there are pending frames in software queue
1230 -        * - the TID is currently paused for ADDBA/BAR request
1231 -        * - seqno is not within block-ack window
1232 -        * - h/w queue depth exceeds low water mark
1233 -        */
1234 -       if ((!skb_queue_empty(&tid->buf_q) || tid->paused ||
1235 -            !BAW_WITHIN(tid->seq_start, tid->baw_size, tid->seq_next) ||
1236 -            txq->axq_ampdu_depth >= ATH_AGGR_MIN_QDEPTH) &&
1237 -           txq != sc->tx.uapsdq) {
1238 -               /*
1239 -                * Add this frame to software queue for scheduling later
1240 -                * for aggregation.
1241 -                */
1242 -               TX_STAT_INC(txq->axq_qnum, a_queued_sw);
1243 -               __skb_queue_tail(&tid->buf_q, skb);
1244 -               if (!txctl->an || !txctl->an->sleeping)
1245 -                       ath_tx_queue_tid(txq, tid);
1246 -               return;
1247 -       }
1248 -
1249 -       bf = ath_tx_setup_buffer(sc, txq, tid, skb);
1250 -       if (!bf) {
1251 -               ieee80211_free_txskb(sc->hw, skb);
1252 -               return;
1253 -       }
1254 -
1255 -       ath_set_rates(tid->an->vif, tid->an->sta, bf);
1256 -       bf->bf_state.bf_type = BUF_AMPDU;
1257 -       INIT_LIST_HEAD(&bf_head);
1258 -       list_add(&bf->list, &bf_head);
1259 -
1260 -       /* Add sub-frame to BAW */
1261 -       ath_tx_addto_baw(sc, tid, bf->bf_state.seqno);
1262 -
1263 -       /* Queue to h/w without aggregation */
1264 -       TX_STAT_INC(txq->axq_qnum, a_queued_hw);
1265 -       bf->bf_lastbf = bf;
1266 -       ath_tx_fill_desc(sc, bf, txq, fi->framelen);
1267 -       ath_tx_txqaddbuf(sc, txq, &bf_head, false);
1268 -}
1269 -
1270  static void ath_tx_send_normal(struct ath_softc *sc, struct ath_txq *txq,
1271                                struct ath_atx_tid *tid, struct sk_buff *skb)
1272  {
1273 @@ -1985,6 +2098,7 @@ static int ath_tx_prepare(struct ieee802
1274         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1275         struct ieee80211_sta *sta = txctl->sta;
1276         struct ieee80211_vif *vif = info->control.vif;
1277 +       struct ath_vif *avp;
1278         struct ath_softc *sc = hw->priv;
1279         int frmlen = skb->len + FCS_LEN;
1280         int padpos, padsize;
1281 @@ -1992,6 +2106,10 @@ static int ath_tx_prepare(struct ieee802
1282         /* NOTE:  sta can be NULL according to net/mac80211.h */
1283         if (sta)
1284                 txctl->an = (struct ath_node *)sta->drv_priv;
1285 +       else if (vif && ieee80211_is_data(hdr->frame_control)) {
1286 +               avp = (void *)vif->drv_priv;
1287 +               txctl->an = &avp->mcast_node;
1288 +       }
1289  
1290         if (info->control.hw_key)
1291                 frmlen += info->control.hw_key->icv_len;
1292 @@ -2041,7 +2159,6 @@ int ath_tx_start(struct ieee80211_hw *hw
1293         struct ath_txq *txq = txctl->txq;
1294         struct ath_atx_tid *tid = NULL;
1295         struct ath_buf *bf;
1296 -       u8 tidno;
1297         int q;
1298         int ret;
1299  
1300 @@ -2069,27 +2186,31 @@ int ath_tx_start(struct ieee80211_hw *hw
1301                 ath_txq_unlock(sc, txq);
1302                 txq = sc->tx.uapsdq;
1303                 ath_txq_lock(sc, txq);
1304 -       }
1305 -
1306 -       if (txctl->an && ieee80211_is_data_qos(hdr->frame_control)) {
1307 -               tidno = ieee80211_get_qos_ctl(hdr)[0] &
1308 -                       IEEE80211_QOS_CTL_TID_MASK;
1309 -               tid = ATH_AN_2_TID(txctl->an, tidno);
1310 +       } else if (txctl->an &&
1311 +                  ieee80211_is_data_present(hdr->frame_control)) {
1312 +               tid = ath_get_skb_tid(sc, txctl->an, skb);
1313  
1314                 WARN_ON(tid->ac->txq != txctl->txq);
1315 -       }
1316  
1317 -       if ((info->flags & IEEE80211_TX_CTL_AMPDU) && tid) {
1318 +               if (info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT)
1319 +                       tid->ac->clear_ps_filter = true;
1320 +
1321                 /*
1322 -                * Try aggregation if it's a unicast data frame
1323 -                * and the destination is HT capable.
1324 +                * Add this frame to software queue for scheduling later
1325 +                * for aggregation.
1326                  */
1327 -               ath_tx_send_ampdu(sc, txq, tid, skb, txctl);
1328 +               TX_STAT_INC(txq->axq_qnum, a_queued_sw);
1329 +               __skb_queue_tail(&tid->buf_q, skb);
1330 +               if (!txctl->an->sleeping)
1331 +                       ath_tx_queue_tid(txq, tid);
1332 +
1333 +               ath_txq_schedule(sc, txq);
1334                 goto out;
1335         }
1336  
1337         bf = ath_tx_setup_buffer(sc, txq, tid, skb);
1338         if (!bf) {
1339 +               ath_txq_skb_done(sc, txq, skb);
1340                 if (txctl->paprd)
1341                         dev_kfree_skb_any(skb);
1342                 else
1343 @@ -2142,7 +2263,7 @@ void ath_tx_cabq(struct ieee80211_hw *hw
1344  
1345                 bf->bf_lastbf = bf;
1346                 ath_set_rates(vif, NULL, bf);
1347 -               ath_buf_set_rate(sc, bf, &info, fi->framelen);
1348 +               ath_buf_set_rate(sc, bf, &info, fi->framelen, false);
1349                 duration += info.rates[0].PktDuration;
1350                 if (bf_tail)
1351                         bf_tail->bf_next = bf;
1352 @@ -2189,7 +2310,7 @@ static void ath_tx_complete(struct ath_s
1353         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1354         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1355         struct ieee80211_hdr * hdr = (struct ieee80211_hdr *)skb->data;
1356 -       int q, padpos, padsize;
1357 +       int padpos, padsize;
1358         unsigned long flags;
1359  
1360         ath_dbg(common, XMIT, "TX complete: skb: %p\n", skb);
1361 @@ -2225,21 +2346,7 @@ static void ath_tx_complete(struct ath_s
1362         spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1363  
1364         __skb_queue_tail(&txq->complete_q, skb);
1365 -
1366 -       q = skb_get_queue_mapping(skb);
1367 -       if (txq == sc->tx.uapsdq)
1368 -               txq = sc->tx.txq_map[q];
1369 -
1370 -       if (txq == sc->tx.txq_map[q]) {
1371 -               if (WARN_ON(--txq->pending_frames < 0))
1372 -                       txq->pending_frames = 0;
1373 -
1374 -               if (txq->stopped &&
1375 -                   txq->pending_frames < sc->tx.txq_max_pending[q]) {
1376 -                       ieee80211_wake_queue(sc->hw, q);
1377 -                       txq->stopped = false;
1378 -               }
1379 -       }
1380 +       ath_txq_skb_done(sc, txq, skb);
1381  }
1382  
1383  static void ath_tx_complete_buf(struct ath_softc *sc, struct ath_buf *bf,
1384 @@ -2360,8 +2467,7 @@ static void ath_tx_processq(struct ath_s
1385  
1386                 if (list_empty(&txq->axq_q)) {
1387                         txq->axq_link = NULL;
1388 -                       if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_HT)
1389 -                               ath_txq_schedule(sc, txq);
1390 +                       ath_txq_schedule(sc, txq);
1391                         break;
1392                 }
1393                 bf = list_first_entry(&txq->axq_q, struct ath_buf, list);
1394 @@ -2375,7 +2481,7 @@ static void ath_tx_processq(struct ath_s
1395                  * it with the STALE flag.
1396                  */
1397                 bf_held = NULL;
1398 -               if (bf->bf_stale) {
1399 +               if (bf->bf_state.stale) {
1400                         bf_held = bf;
1401                         if (list_is_last(&bf_held->list, &txq->axq_q))
1402                                 break;
1403 @@ -2399,7 +2505,7 @@ static void ath_tx_processq(struct ath_s
1404                  * however leave the last descriptor back as the holding
1405                  * descriptor for hw.
1406                  */
1407 -               lastbf->bf_stale = true;
1408 +               lastbf->bf_state.stale = true;
1409                 INIT_LIST_HEAD(&bf_head);
1410                 if (!list_is_singular(&lastbf->list))
1411                         list_cut_position(&bf_head,
1412 @@ -2470,7 +2576,7 @@ void ath_tx_edma_tasklet(struct ath_soft
1413                 }
1414  
1415                 bf = list_first_entry(fifo_list, struct ath_buf, list);
1416 -               if (bf->bf_stale) {
1417 +               if (bf->bf_state.stale) {
1418                         list_del(&bf->list);
1419                         ath_tx_return_buffer(sc, bf);
1420                         bf = list_first_entry(fifo_list, struct ath_buf, list);
1421 @@ -2492,7 +2598,7 @@ void ath_tx_edma_tasklet(struct ath_soft
1422                                 ath_tx_txqaddbuf(sc, txq, &bf_q, true);
1423                         }
1424                 } else {
1425 -                       lastbf->bf_stale = true;
1426 +                       lastbf->bf_state.stale = true;
1427                         if (bf != lastbf)
1428                                 list_cut_position(&bf_head, fifo_list,
1429                                                   lastbf->list.prev);
1430 @@ -2583,6 +2689,7 @@ void ath_tx_node_init(struct ath_softc *
1431                 tid->paused    = false;
1432                 tid->active        = false;
1433                 __skb_queue_head_init(&tid->buf_q);
1434 +               __skb_queue_head_init(&tid->retry_q);
1435                 acno = TID_TO_WME_AC(tidno);
1436                 tid->ac = &an->ac[acno];
1437         }
1438 @@ -2590,6 +2697,7 @@ void ath_tx_node_init(struct ath_softc *
1439         for (acno = 0, ac = &an->ac[acno];
1440              acno < IEEE80211_NUM_ACS; acno++, ac++) {
1441                 ac->sched    = false;
1442 +               ac->clear_ps_filter = true;
1443                 ac->txq = sc->tx.txq_map[acno];
1444                 INIT_LIST_HEAD(&ac->tid_q);
1445         }
1446 --- a/drivers/net/wireless/ath/ath9k/main.c
1447 +++ b/drivers/net/wireless/ath/ath9k/main.c
1448 @@ -173,8 +173,7 @@ static void ath_restart_work(struct ath_
1449  {
1450         ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
1451  
1452 -       if (AR_SREV_9340(sc->sc_ah) || AR_SREV_9485(sc->sc_ah) ||
1453 -           AR_SREV_9550(sc->sc_ah))
1454 +       if (AR_SREV_9340(sc->sc_ah) || AR_SREV_9330(sc->sc_ah))
1455                 ieee80211_queue_delayed_work(sc->hw, &sc->hw_pll_work,
1456                                      msecs_to_jiffies(ATH_PLL_WORK_INTERVAL));
1457  
1458 @@ -238,9 +237,6 @@ static bool ath_complete_reset(struct at
1459                 ath_restart_work(sc);
1460         }
1461  
1462 -       if ((ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) && sc->ant_rx != 3)
1463 -               ath_ant_comb_update(sc);
1464 -
1465         ieee80211_wake_queues(sc->hw);
1466  
1467         return true;
1468 @@ -966,6 +962,8 @@ static int ath9k_add_interface(struct ie
1469         struct ath_softc *sc = hw->priv;
1470         struct ath_hw *ah = sc->sc_ah;
1471         struct ath_common *common = ath9k_hw_common(ah);
1472 +       struct ath_vif *avp = (void *)vif->drv_priv;
1473 +       struct ath_node *an = &avp->mcast_node;
1474  
1475         mutex_lock(&sc->mutex);
1476  
1477 @@ -979,6 +977,12 @@ static int ath9k_add_interface(struct ie
1478         if (ath9k_uses_beacons(vif->type))
1479                 ath9k_beacon_assign_slot(sc, vif);
1480  
1481 +       an->sc = sc;
1482 +       an->sta = NULL;
1483 +       an->vif = vif;
1484 +       an->no_ps_filter = true;
1485 +       ath_tx_node_init(sc, an);
1486 +
1487         mutex_unlock(&sc->mutex);
1488         return 0;
1489  }
1490 @@ -1016,6 +1020,7 @@ static void ath9k_remove_interface(struc
1491  {
1492         struct ath_softc *sc = hw->priv;
1493         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1494 +       struct ath_vif *avp = (void *)vif->drv_priv;
1495  
1496         ath_dbg(common, CONFIG, "Detach Interface\n");
1497  
1498 @@ -1030,6 +1035,8 @@ static void ath9k_remove_interface(struc
1499         ath9k_calculate_summary_state(hw, NULL);
1500         ath9k_ps_restore(sc);
1501  
1502 +       ath_tx_node_cleanup(sc, &avp->mcast_node);
1503 +
1504         mutex_unlock(&sc->mutex);
1505  }
1506  
1507 @@ -1193,8 +1200,6 @@ static int ath9k_config(struct ieee80211
1508  
1509         if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) || reset_channel) {
1510                 struct ieee80211_channel *curchan = hw->conf.chandef.chan;
1511 -               enum nl80211_channel_type channel_type =
1512 -                       cfg80211_get_chandef_type(&conf->chandef);
1513                 int pos = curchan->hw_value;
1514                 int old_pos = -1;
1515                 unsigned long flags;
1516 @@ -1202,8 +1207,8 @@ static int ath9k_config(struct ieee80211
1517                 if (ah->curchan)
1518                         old_pos = ah->curchan - &ah->channels[0];
1519  
1520 -               ath_dbg(common, CONFIG, "Set channel: %d MHz type: %d\n",
1521 -                       curchan->center_freq, channel_type);
1522 +               ath_dbg(common, CONFIG, "Set channel: %d MHz width: %d\n",
1523 +                       curchan->center_freq, hw->conf.chandef.width);
1524  
1525                 /* update survey stats for the old channel before switching */
1526                 spin_lock_irqsave(&common->cc_lock, flags);
1527 @@ -1211,7 +1216,7 @@ static int ath9k_config(struct ieee80211
1528                 spin_unlock_irqrestore(&common->cc_lock, flags);
1529  
1530                 ath9k_cmn_update_ichannel(&sc->sc_ah->channels[pos],
1531 -                                         curchan, channel_type);
1532 +                                         &conf->chandef);
1533  
1534                 /*
1535                  * If the operating channel changes, change the survey in-use flags
1536 @@ -1374,9 +1379,6 @@ static void ath9k_sta_notify(struct ieee
1537         struct ath_softc *sc = hw->priv;
1538         struct ath_node *an = (struct ath_node *) sta->drv_priv;
1539  
1540 -       if (!sta->ht_cap.ht_supported)
1541 -               return;
1542 -
1543         switch (cmd) {
1544         case STA_NOTIFY_SLEEP:
1545                 an->sleeping = true;
1546 @@ -2094,7 +2096,7 @@ static void ath9k_wow_add_pattern(struct
1547  {
1548         struct ath_hw *ah = sc->sc_ah;
1549         struct ath9k_wow_pattern *wow_pattern = NULL;
1550 -       struct cfg80211_wowlan_trig_pkt_pattern *patterns = wowlan->patterns;
1551 +       struct cfg80211_pkt_pattern *patterns = wowlan->patterns;
1552         int mask_len;
1553         s8 i = 0;
1554  
1555 --- a/drivers/net/wireless/mwifiex/cfg80211.c
1556 +++ b/drivers/net/wireless/mwifiex/cfg80211.c
1557 @@ -2298,8 +2298,7 @@ EXPORT_SYMBOL_GPL(mwifiex_del_virtual_in
1558  
1559  #ifdef CONFIG_PM
1560  static bool
1561 -mwifiex_is_pattern_supported(struct cfg80211_wowlan_trig_pkt_pattern *pat,
1562 -                            s8 *byte_seq)
1563 +mwifiex_is_pattern_supported(struct cfg80211_pkt_pattern *pat, s8 *byte_seq)
1564  {
1565         int j, k, valid_byte_cnt = 0;
1566         bool dont_care_byte = false;
1567 --- a/drivers/net/wireless/ti/wlcore/main.c
1568 +++ b/drivers/net/wireless/ti/wlcore/main.c
1569 @@ -1315,7 +1315,7 @@ static struct sk_buff *wl12xx_alloc_dumm
1570  
1571  #ifdef CONFIG_PM
1572  static int
1573 -wl1271_validate_wowlan_pattern(struct cfg80211_wowlan_trig_pkt_pattern *p)
1574 +wl1271_validate_wowlan_pattern(struct cfg80211_pkt_pattern *p)
1575  {
1576         int num_fields = 0, in_field = 0, fields_size = 0;
1577         int i, pattern_len = 0;
1578 @@ -1458,9 +1458,9 @@ void wl1271_rx_filter_flatten_fields(str
1579   * Allocates an RX filter returned through f
1580   * which needs to be freed using rx_filter_free()
1581   */
1582 -static int wl1271_convert_wowlan_pattern_to_rx_filter(
1583 -       struct cfg80211_wowlan_trig_pkt_pattern *p,
1584 -       struct wl12xx_rx_filter **f)
1585 +static int
1586 +wl1271_convert_wowlan_pattern_to_rx_filter(struct cfg80211_pkt_pattern *p,
1587 +                                          struct wl12xx_rx_filter **f)
1588  {
1589         int i, j, ret = 0;
1590         struct wl12xx_rx_filter *filter;
1591 @@ -1562,7 +1562,7 @@ static int wl1271_configure_wowlan(struc
1592  
1593         /* Translate WoWLAN patterns into filters */
1594         for (i = 0; i < wow->n_patterns; i++) {
1595 -               struct cfg80211_wowlan_trig_pkt_pattern *p;
1596 +               struct cfg80211_pkt_pattern *p;
1597                 struct wl12xx_rx_filter *filter = NULL;
1598  
1599                 p = &wow->patterns[i];
1600 --- a/include/net/cfg80211.h
1601 +++ b/include/net/cfg80211.h
1602 @@ -639,6 +639,30 @@ struct cfg80211_ap_settings {
1603  };
1604  
1605  /**
1606 + * struct cfg80211_csa_settings - channel switch settings
1607 + *
1608 + * Used for channel switch
1609 + *
1610 + * @chandef: defines the channel to use after the switch
1611 + * @beacon_csa: beacon data while performing the switch
1612 + * @counter_offset_beacon: offset for the counter within the beacon (tail)
1613 + * @counter_offset_presp: offset for the counter within the probe response
1614 + * @beacon_after: beacon data to be used on the new channel
1615 + * @radar_required: whether radar detection is required on the new channel
1616 + * @block_tx: whether transmissions should be blocked while changing
1617 + * @count: number of beacons until switch
1618 + */
1619 +struct cfg80211_csa_settings {
1620 +       struct cfg80211_chan_def chandef;
1621 +       struct cfg80211_beacon_data beacon_csa;
1622 +       u16 counter_offset_beacon, counter_offset_presp;
1623 +       struct cfg80211_beacon_data beacon_after;
1624 +       bool radar_required;
1625 +       bool block_tx;
1626 +       u8 count;
1627 +};
1628 +
1629 +/**
1630   * enum station_parameters_apply_mask - station parameter values to apply
1631   * @STATION_PARAM_APPLY_UAPSD: apply new uAPSD parameters (uapsd_queues, max_sp)
1632   * @STATION_PARAM_APPLY_CAPABILITY: apply new capability
1633 @@ -1698,7 +1722,7 @@ struct cfg80211_pmksa {
1634  };
1635  
1636  /**
1637 - * struct cfg80211_wowlan_trig_pkt_pattern - packet pattern
1638 + * struct cfg80211_pkt_pattern - packet pattern
1639   * @mask: bitmask where to match pattern and where to ignore bytes,
1640   *     one bit per byte, in same format as nl80211
1641   * @pattern: bytes to match where bitmask is 1
1642 @@ -1708,7 +1732,7 @@ struct cfg80211_pmksa {
1643   * Internal note: @mask and @pattern are allocated in one chunk of
1644   * memory, free @mask only!
1645   */
1646 -struct cfg80211_wowlan_trig_pkt_pattern {
1647 +struct cfg80211_pkt_pattern {
1648         u8 *mask, *pattern;
1649         int pattern_len;
1650         int pkt_offset;
1651 @@ -1770,7 +1794,7 @@ struct cfg80211_wowlan {
1652         bool any, disconnect, magic_pkt, gtk_rekey_failure,
1653              eap_identity_req, four_way_handshake,
1654              rfkill_release;
1655 -       struct cfg80211_wowlan_trig_pkt_pattern *patterns;
1656 +       struct cfg80211_pkt_pattern *patterns;
1657         struct cfg80211_wowlan_tcp *tcp;
1658         int n_patterns;
1659  };
1660 @@ -2071,6 +2095,8 @@ struct cfg80211_update_ft_ies_params {
1661   *     driver can take the most appropriate actions.
1662   * @crit_proto_stop: Indicates critical protocol no longer needs increased link
1663   *     reliability. This operation can not fail.
1664 + *
1665 + * @channel_switch: initiate channel-switch procedure (with CSA)
1666   */
1667  struct cfg80211_ops {
1668         int     (*suspend)(struct wiphy *wiphy, struct cfg80211_wowlan *wow);
1669 @@ -2306,6 +2332,10 @@ struct cfg80211_ops {
1670                                     u16 duration);
1671         void    (*crit_proto_stop)(struct wiphy *wiphy,
1672                                    struct wireless_dev *wdev);
1673 +
1674 +       int     (*channel_switch)(struct wiphy *wiphy,
1675 +                                 struct net_device *dev,
1676 +                                 struct cfg80211_csa_settings *params);
1677  };
1678  
1679  /*
1680 @@ -2371,6 +2401,8 @@ struct cfg80211_ops {
1681   * @WIPHY_FLAG_OFFCHAN_TX: Device supports direct off-channel TX.
1682   * @WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL: Device supports remain-on-channel call.
1683   * @WIPHY_FLAG_SUPPORTS_5_10_MHZ: Device supports 5 MHz and 10 MHz channels.
1684 + * @WIPHY_FLAG_HAS_CHANNEL_SWITCH: Device supports channel switch in
1685 + *     beaconing mode (AP, IBSS, Mesh, ...).
1686   */
1687  enum wiphy_flags {
1688         WIPHY_FLAG_CUSTOM_REGULATORY            = BIT(0),
1689 @@ -2395,6 +2427,7 @@ enum wiphy_flags {
1690         WIPHY_FLAG_OFFCHAN_TX                   = BIT(20),
1691         WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL        = BIT(21),
1692         WIPHY_FLAG_SUPPORTS_5_10_MHZ            = BIT(22),
1693 +       WIPHY_FLAG_HAS_CHANNEL_SWITCH           = BIT(23),
1694  };
1695  
1696  /**
1697 --- a/include/uapi/linux/nl80211.h
1698 +++ b/include/uapi/linux/nl80211.h
1699 @@ -648,6 +648,16 @@
1700   * @NL80211_CMD_CRIT_PROTOCOL_STOP: Indicates the connection reliability can
1701   *     return back to normal.
1702   *
1703 + * @NL80211_CMD_CHANNEL_SWITCH: Perform a channel switch by announcing the
1704 + *     the new channel information (Channel Switch Announcement - CSA)
1705 + *     in the beacon for some time (as defined in the
1706 + *     %NL80211_ATTR_CH_SWITCH_COUNT parameter) and then change to the
1707 + *     new channel. Userspace provides the new channel information (using
1708 + *     %NL80211_ATTR_WIPHY_FREQ and the attributes determining channel
1709 + *     width). %NL80211_ATTR_CH_SWITCH_BLOCK_TX may be supplied to inform
1710 + *     other station that transmission must be blocked until the channel
1711 + *     switch is complete.
1712 + *
1713   * @NL80211_CMD_MAX: highest used command number
1714   * @__NL80211_CMD_AFTER_LAST: internal use
1715   */
1716 @@ -810,6 +820,8 @@ enum nl80211_commands {
1717         NL80211_CMD_CRIT_PROTOCOL_START,
1718         NL80211_CMD_CRIT_PROTOCOL_STOP,
1719  
1720 +       NL80211_CMD_CHANNEL_SWITCH,
1721 +
1722         /* add new commands above here */
1723  
1724         /* used to define NL80211_CMD_MAX below */
1725 @@ -1436,6 +1448,18 @@ enum nl80211_commands {
1726   *     allowed to be used with the first @NL80211_CMD_SET_STATION command to
1727   *     update a TDLS peer STA entry.
1728   *
1729 + * @NL80211_ATTR_CH_SWITCH_COUNT: u32 attribute specifying the number of TBTT's
1730 + *     until the channel switch event.
1731 + * @NL80211_ATTR_CH_SWITCH_BLOCK_TX: flag attribute specifying that transmission
1732 + *     must be blocked on the current channel (before the channel switch
1733 + *     operation).
1734 + * @NL80211_ATTR_CSA_IES: Nested set of attributes containing the IE information
1735 + *     for the time while performing a channel switch.
1736 + * @NL80211_ATTR_CSA_C_OFF_BEACON: Offset of the channel switch counter
1737 + *     field in the beacons tail (%NL80211_ATTR_BEACON_TAIL).
1738 + * @NL80211_ATTR_CSA_C_OFF_PRESP: Offset of the channel switch counter
1739 + *     field in the probe response (%NL80211_ATTR_PROBE_RESP).
1740 + *
1741   * @NL80211_ATTR_MAX: highest attribute number currently defined
1742   * @__NL80211_ATTR_AFTER_LAST: internal use
1743   */
1744 @@ -1736,6 +1760,12 @@ enum nl80211_attrs {
1745  
1746         NL80211_ATTR_PEER_AID,
1747  
1748 +       NL80211_ATTR_CH_SWITCH_COUNT,
1749 +       NL80211_ATTR_CH_SWITCH_BLOCK_TX,
1750 +       NL80211_ATTR_CSA_IES,
1751 +       NL80211_ATTR_CSA_C_OFF_BEACON,
1752 +       NL80211_ATTR_CSA_C_OFF_PRESP,
1753 +
1754         /* add attributes here, update the policy in nl80211.c */
1755  
1756         __NL80211_ATTR_AFTER_LAST,
1757 @@ -3060,11 +3090,11 @@ enum nl80211_tx_power_setting {
1758  };
1759  
1760  /**
1761 - * enum nl80211_wowlan_packet_pattern_attr - WoWLAN packet pattern attribute
1762 - * @__NL80211_WOWLAN_PKTPAT_INVALID: invalid number for nested attribute
1763 - * @NL80211_WOWLAN_PKTPAT_PATTERN: the pattern, values where the mask has
1764 + * enum nl80211_packet_pattern_attr - packet pattern attribute
1765 + * @__NL80211_PKTPAT_INVALID: invalid number for nested attribute
1766 + * @NL80211_PKTPAT_PATTERN: the pattern, values where the mask has
1767   *     a zero bit are ignored
1768 - * @NL80211_WOWLAN_PKTPAT_MASK: pattern mask, must be long enough to have
1769 + * @NL80211_PKTPAT_MASK: pattern mask, must be long enough to have
1770   *     a bit for each byte in the pattern. The lowest-order bit corresponds
1771   *     to the first byte of the pattern, but the bytes of the pattern are
1772   *     in a little-endian-like format, i.e. the 9th byte of the pattern
1773 @@ -3075,23 +3105,23 @@ enum nl80211_tx_power_setting {
1774   *     Note that the pattern matching is done as though frames were not
1775   *     802.11 frames but 802.3 frames, i.e. the frame is fully unpacked
1776   *     first (including SNAP header unpacking) and then matched.
1777 - * @NL80211_WOWLAN_PKTPAT_OFFSET: packet offset, pattern is matched after
1778 + * @NL80211_PKTPAT_OFFSET: packet offset, pattern is matched after
1779   *     these fixed number of bytes of received packet
1780 - * @NUM_NL80211_WOWLAN_PKTPAT: number of attributes
1781 - * @MAX_NL80211_WOWLAN_PKTPAT: max attribute number
1782 + * @NUM_NL80211_PKTPAT: number of attributes
1783 + * @MAX_NL80211_PKTPAT: max attribute number
1784   */
1785 -enum nl80211_wowlan_packet_pattern_attr {
1786 -       __NL80211_WOWLAN_PKTPAT_INVALID,
1787 -       NL80211_WOWLAN_PKTPAT_MASK,
1788 -       NL80211_WOWLAN_PKTPAT_PATTERN,
1789 -       NL80211_WOWLAN_PKTPAT_OFFSET,
1790 +enum nl80211_packet_pattern_attr {
1791 +       __NL80211_PKTPAT_INVALID,
1792 +       NL80211_PKTPAT_MASK,
1793 +       NL80211_PKTPAT_PATTERN,
1794 +       NL80211_PKTPAT_OFFSET,
1795  
1796 -       NUM_NL80211_WOWLAN_PKTPAT,
1797 -       MAX_NL80211_WOWLAN_PKTPAT = NUM_NL80211_WOWLAN_PKTPAT - 1,
1798 +       NUM_NL80211_PKTPAT,
1799 +       MAX_NL80211_PKTPAT = NUM_NL80211_PKTPAT - 1,
1800  };
1801  
1802  /**
1803 - * struct nl80211_wowlan_pattern_support - pattern support information
1804 + * struct nl80211_pattern_support - packet pattern support information
1805   * @max_patterns: maximum number of patterns supported
1806   * @min_pattern_len: minimum length of each pattern
1807   * @max_pattern_len: maximum length of each pattern
1808 @@ -3101,13 +3131,22 @@ enum nl80211_wowlan_packet_pattern_attr 
1809   * that is part of %NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED in the
1810   * capability information given by the kernel to userspace.
1811   */
1812 -struct nl80211_wowlan_pattern_support {
1813 +struct nl80211_pattern_support {
1814         __u32 max_patterns;
1815         __u32 min_pattern_len;
1816         __u32 max_pattern_len;
1817         __u32 max_pkt_offset;
1818  } __attribute__((packed));
1819  
1820 +/* only for backward compatibility */
1821 +#define __NL80211_WOWLAN_PKTPAT_INVALID __NL80211_PKTPAT_INVALID
1822 +#define NL80211_WOWLAN_PKTPAT_MASK NL80211_PKTPAT_MASK
1823 +#define NL80211_WOWLAN_PKTPAT_PATTERN NL80211_PKTPAT_PATTERN
1824 +#define NL80211_WOWLAN_PKTPAT_OFFSET NL80211_PKTPAT_OFFSET
1825 +#define NUM_NL80211_WOWLAN_PKTPAT NUM_NL80211_PKTPAT
1826 +#define MAX_NL80211_WOWLAN_PKTPAT MAX_NL80211_PKTPAT
1827 +#define nl80211_wowlan_pattern_support nl80211_pattern_support
1828 +
1829  /**
1830   * enum nl80211_wowlan_triggers - WoWLAN trigger definitions
1831   * @__NL80211_WOWLAN_TRIG_INVALID: invalid number for nested attributes
1832 @@ -3127,7 +3166,7 @@ struct nl80211_wowlan_pattern_support {
1833   *     pattern matching is done after the packet is converted to the MSDU.
1834   *
1835   *     In %NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED, it is a binary attribute
1836 - *     carrying a &struct nl80211_wowlan_pattern_support.
1837 + *     carrying a &struct nl80211_pattern_support.
1838   *
1839   *     When reporting wakeup. it is a u32 attribute containing the 0-based
1840   *     index of the pattern that caused the wakeup, in the patterns passed
1841 @@ -3284,7 +3323,7 @@ struct nl80211_wowlan_tcp_data_token_fea
1842   * @NL80211_WOWLAN_TCP_WAKE_PAYLOAD: wake packet payload, for advertising a
1843   *     u32 attribute holding the maximum length
1844   * @NL80211_WOWLAN_TCP_WAKE_MASK: Wake packet payload mask, not used for
1845 - *     feature advertising. The mask works like @NL80211_WOWLAN_PKTPAT_MASK
1846 + *     feature advertising. The mask works like @NL80211_PKTPAT_MASK
1847   *     but on the TCP payload only.
1848   * @NUM_NL80211_WOWLAN_TCP: number of TCP attributes
1849   * @MAX_NL80211_WOWLAN_TCP: highest attribute number
1850 --- a/net/mac80211/mesh_ps.c
1851 +++ b/net/mac80211/mesh_ps.c
1852 @@ -229,6 +229,10 @@ void ieee80211_mps_sta_status_update(str
1853         enum nl80211_mesh_power_mode pm;
1854         bool do_buffer;
1855  
1856 +       /* For non-assoc STA, prevent buffering or frame transmission */
1857 +       if (sta->sta_state < IEEE80211_STA_ASSOC)
1858 +               return;
1859 +
1860         /*
1861          * use peer-specific power mode if peering is established and the
1862          * peer's power mode is known
1863 --- a/net/wireless/nl80211.c
1864 +++ b/net/wireless/nl80211.c
1865 @@ -349,6 +349,11 @@ static const struct nla_policy nl80211_p
1866         [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
1867                                   .len = IEEE80211_MAX_DATA_LEN },
1868         [NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
1869 +       [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
1870 +       [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
1871 +       [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
1872 +       [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_U16 },
1873 +       [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_U16 },
1874  };
1875  
1876  /* policy for the key attributes */
1877 @@ -441,10 +446,12 @@ static int nl80211_prepare_wdev_dump(str
1878                         goto out_unlock;
1879                 }
1880                 *rdev = wiphy_to_dev((*wdev)->wiphy);
1881 -               cb->args[0] = (*rdev)->wiphy_idx;
1882 +               /* 0 is the first index - add 1 to parse only once */
1883 +               cb->args[0] = (*rdev)->wiphy_idx + 1;
1884                 cb->args[1] = (*wdev)->identifier;
1885         } else {
1886 -               struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0]);
1887 +               /* subtract the 1 again here */
1888 +               struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
1889                 struct wireless_dev *tmp;
1890  
1891                 if (!wiphy) {
1892 @@ -974,7 +981,7 @@ static int nl80211_send_wowlan(struct sk
1893                 return -ENOBUFS;
1894  
1895         if (dev->wiphy.wowlan->n_patterns) {
1896 -               struct nl80211_wowlan_pattern_support pat = {
1897 +               struct nl80211_pattern_support pat = {
1898                         .max_patterns = dev->wiphy.wowlan->n_patterns,
1899                         .min_pattern_len = dev->wiphy.wowlan->pattern_min_len,
1900                         .max_pattern_len = dev->wiphy.wowlan->pattern_max_len,
1901 @@ -1393,6 +1400,8 @@ static int nl80211_send_wiphy(struct cfg
1902                 if (state->split) {
1903                         CMD(crit_proto_start, CRIT_PROTOCOL_START);
1904                         CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
1905 +                       if (dev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
1906 +                               CMD(channel_switch, CHANNEL_SWITCH);
1907                 }
1908  
1909  #ifdef CPTCFG_NL80211_TESTMODE
1910 @@ -1568,8 +1577,10 @@ static int nl80211_dump_wiphy(struct sk_
1911         rtnl_lock();
1912         if (!state) {
1913                 state = kzalloc(sizeof(*state), GFP_KERNEL);
1914 -               if (!state)
1915 +               if (!state) {
1916 +                       rtnl_unlock();
1917                         return -ENOMEM;
1918 +               }
1919                 state->filter_wiphy = -1;
1920                 ret = nl80211_dump_wiphy_parse(skb, cb, state);
1921                 if (ret) {
1922 @@ -2620,8 +2631,8 @@ static int nl80211_get_key(struct sk_buf
1923  
1924         hdr = nl80211hdr_put(msg, genl_info_snd_portid(info), info->snd_seq, 0,
1925                              NL80211_CMD_NEW_KEY);
1926 -       if (IS_ERR(hdr))
1927 -               return PTR_ERR(hdr);
1928 +       if (!hdr)
1929 +               return -ENOBUFS;
1930  
1931         cookie.msg = msg;
1932         cookie.idx = key_idx;
1933 @@ -4770,9 +4781,9 @@ do {                                                                          \
1934         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
1935                                   mask, NL80211_MESHCONF_FORWARDING,
1936                                   nla_get_u8);
1937 -       FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
1938 +       FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
1939                                   mask, NL80211_MESHCONF_RSSI_THRESHOLD,
1940 -                                 nla_get_u32);
1941 +                                 nla_get_s32);
1942         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
1943                                   mask, NL80211_MESHCONF_HT_OPMODE,
1944                                   nla_get_u16);
1945 @@ -5578,6 +5589,111 @@ static int nl80211_start_radar_detection
1946         return err;
1947  }
1948  
1949 +static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
1950 +{
1951 +       struct cfg80211_registered_device *rdev = info->user_ptr[0];
1952 +       struct net_device *dev = info->user_ptr[1];
1953 +       struct wireless_dev *wdev = dev->ieee80211_ptr;
1954 +       struct cfg80211_csa_settings params;
1955 +       /* csa_attrs is defined static to avoid waste of stack size - this
1956 +        * function is called under RTNL lock, so this should not be a problem.
1957 +        */
1958 +       static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
1959 +       u8 radar_detect_width = 0;
1960 +       int err;
1961 +
1962 +       if (!rdev->ops->channel_switch ||
1963 +           !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
1964 +               return -EOPNOTSUPP;
1965 +
1966 +       /* may add IBSS support later */
1967 +       if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1968 +           dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1969 +               return -EOPNOTSUPP;
1970 +
1971 +       memset(&params, 0, sizeof(params));
1972 +
1973 +       if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
1974 +           !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
1975 +               return -EINVAL;
1976 +
1977 +       /* only important for AP, IBSS and mesh create IEs internally */
1978 +       if (!info->attrs[NL80211_ATTR_CSA_IES])
1979 +               return -EINVAL;
1980 +
1981 +       /* useless if AP is not running */
1982 +       if (!wdev->beacon_interval)
1983 +               return -EINVAL;
1984 +
1985 +       params.count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
1986 +
1987 +       err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
1988 +       if (err)
1989 +               return err;
1990 +
1991 +       err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
1992 +                              info->attrs[NL80211_ATTR_CSA_IES],
1993 +                              nl80211_policy);
1994 +       if (err)
1995 +               return err;
1996 +
1997 +       err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
1998 +       if (err)
1999 +               return err;
2000 +
2001 +       if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
2002 +               return -EINVAL;
2003 +
2004 +       params.counter_offset_beacon =
2005 +               nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
2006 +       if (params.counter_offset_beacon >= params.beacon_csa.tail_len)
2007 +               return -EINVAL;
2008 +
2009 +       /* sanity check - counters should be the same */
2010 +       if (params.beacon_csa.tail[params.counter_offset_beacon] !=
2011 +           params.count)
2012 +               return -EINVAL;
2013 +
2014 +       if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
2015 +               params.counter_offset_presp =
2016 +                       nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
2017 +               if (params.counter_offset_presp >=
2018 +                   params.beacon_csa.probe_resp_len)
2019 +                       return -EINVAL;
2020 +
2021 +               if (params.beacon_csa.probe_resp[params.counter_offset_presp] !=
2022 +                   params.count)
2023 +                       return -EINVAL;
2024 +       }
2025 +
2026 +       err = nl80211_parse_chandef(rdev, info, &params.chandef);
2027 +       if (err)
2028 +               return err;
2029 +
2030 +       if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
2031 +               return -EINVAL;
2032 +
2033 +       err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
2034 +       if (err < 0) {
2035 +               return err;
2036 +       } else if (err) {
2037 +               radar_detect_width = BIT(params.chandef.width);
2038 +               params.radar_required = true;
2039 +       }
2040 +
2041 +       err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
2042 +                                          params.chandef.chan,
2043 +                                          CHAN_MODE_SHARED,
2044 +                                          radar_detect_width);
2045 +       if (err)
2046 +               return err;
2047 +
2048 +       if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
2049 +               params.block_tx = true;
2050 +
2051 +       return rdev_channel_switch(rdev, dev, &params);
2052 +}
2053 +
2054  static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
2055                             u32 seq, int flags,
2056                             struct cfg80211_registered_device *rdev,
2057 @@ -6507,6 +6623,9 @@ static int nl80211_testmode_dump(struct 
2058                                            NL80211_CMD_TESTMODE);
2059                 struct nlattr *tmdata;
2060  
2061 +               if (!hdr)
2062 +                       break;
2063 +
2064                 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
2065                         genlmsg_cancel(skb, hdr);
2066                         break;
2067 @@ -6615,12 +6734,14 @@ EXPORT_SYMBOL(cfg80211_testmode_alloc_ev
2068  
2069  void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
2070  {
2071 +       struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
2072         void *hdr = ((void **)skb->cb)[1];
2073         struct nlattr *data = ((void **)skb->cb)[2];
2074  
2075         nla_nest_end(skb, data);
2076         genlmsg_end(skb, hdr);
2077 -       genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
2078 +       genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), skb, 0,
2079 +                               nl80211_testmode_mcgrp.id, gfp);
2080  }
2081  EXPORT_SYMBOL(cfg80211_testmode_event);
2082  #endif
2083 @@ -6949,9 +7070,8 @@ static int nl80211_remain_on_channel(str
2084  
2085         hdr = nl80211hdr_put(msg, genl_info_snd_portid(info), info->snd_seq, 0,
2086                              NL80211_CMD_REMAIN_ON_CHANNEL);
2087 -
2088 -       if (IS_ERR(hdr)) {
2089 -               err = PTR_ERR(hdr);
2090 +       if (!hdr) {
2091 +               err = -ENOBUFS;
2092                 goto free_msg;
2093         }
2094  
2095 @@ -7249,9 +7369,8 @@ static int nl80211_tx_mgmt(struct sk_buf
2096  
2097                 hdr = nl80211hdr_put(msg, genl_info_snd_portid(info), info->snd_seq, 0,
2098                                      NL80211_CMD_FRAME);
2099 -
2100 -               if (IS_ERR(hdr)) {
2101 -                       err = PTR_ERR(hdr);
2102 +               if (!hdr) {
2103 +                       err = -ENOBUFS;
2104                         goto free_msg;
2105                 }
2106         }
2107 @@ -7593,12 +7712,11 @@ static int nl80211_send_wowlan_patterns(
2108                 if (!nl_pat)
2109                         return -ENOBUFS;
2110                 pat_len = wowlan->patterns[i].pattern_len;
2111 -               if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
2112 -                           DIV_ROUND_UP(pat_len, 8),
2113 +               if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
2114                             wowlan->patterns[i].mask) ||
2115 -                   nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
2116 -                           pat_len, wowlan->patterns[i].pattern) ||
2117 -                   nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
2118 +                   nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
2119 +                           wowlan->patterns[i].pattern) ||
2120 +                   nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
2121                                 wowlan->patterns[i].pkt_offset))
2122                         return -ENOBUFS;
2123                 nla_nest_end(msg, nl_pat);
2124 @@ -7939,7 +8057,7 @@ static int nl80211_set_wowlan(struct sk_
2125                 struct nlattr *pat;
2126                 int n_patterns = 0;
2127                 int rem, pat_len, mask_len, pkt_offset;
2128 -               struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
2129 +               struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
2130  
2131                 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
2132                                     rem)
2133 @@ -7958,26 +8076,25 @@ static int nl80211_set_wowlan(struct sk_
2134  
2135                 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
2136                                     rem) {
2137 -                       nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
2138 -                                 nla_data(pat), nla_len(pat), NULL);
2139 +                       nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
2140 +                                 nla_len(pat), NULL);
2141                         err = -EINVAL;
2142 -                       if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
2143 -                           !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
2144 +                       if (!pat_tb[NL80211_PKTPAT_MASK] ||
2145 +                           !pat_tb[NL80211_PKTPAT_PATTERN])
2146                                 goto error;
2147 -                       pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
2148 +                       pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
2149                         mask_len = DIV_ROUND_UP(pat_len, 8);
2150 -                       if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
2151 -                           mask_len)
2152 +                       if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
2153                                 goto error;
2154                         if (pat_len > wowlan->pattern_max_len ||
2155                             pat_len < wowlan->pattern_min_len)
2156                                 goto error;
2157  
2158 -                       if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
2159 +                       if (!pat_tb[NL80211_PKTPAT_OFFSET])
2160                                 pkt_offset = 0;
2161                         else
2162                                 pkt_offset = nla_get_u32(
2163 -                                       pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
2164 +                                       pat_tb[NL80211_PKTPAT_OFFSET]);
2165                         if (pkt_offset > wowlan->max_pkt_offset)
2166                                 goto error;
2167                         new_triggers.patterns[i].pkt_offset = pkt_offset;
2168 @@ -7991,11 +8108,11 @@ static int nl80211_set_wowlan(struct sk_
2169                         new_triggers.patterns[i].pattern =
2170                                 new_triggers.patterns[i].mask + mask_len;
2171                         memcpy(new_triggers.patterns[i].mask,
2172 -                              nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
2173 +                              nla_data(pat_tb[NL80211_PKTPAT_MASK]),
2174                                mask_len);
2175                         new_triggers.patterns[i].pattern_len = pat_len;
2176                         memcpy(new_triggers.patterns[i].pattern,
2177 -                              nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
2178 +                              nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
2179                                pat_len);
2180                         i++;
2181                 }
2182 @@ -8130,9 +8247,8 @@ static int nl80211_probe_client(struct s
2183  
2184         hdr = nl80211hdr_put(msg, genl_info_snd_portid(info), info->snd_seq, 0,
2185                              NL80211_CMD_PROBE_CLIENT);
2186 -
2187 -       if (IS_ERR(hdr)) {
2188 -               err = PTR_ERR(hdr);
2189 +       if (!hdr) {
2190 +               err = -ENOBUFS;
2191                 goto free_msg;
2192         }
2193  
2194 @@ -9041,7 +9157,15 @@ static struct genl_ops nl80211_ops[] = {
2195                 .flags = GENL_ADMIN_PERM,
2196                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
2197                                   NL80211_FLAG_NEED_RTNL,
2198 -       }
2199 +       },
2200 +       {
2201 +               .cmd = NL80211_CMD_CHANNEL_SWITCH,
2202 +               .doit = nl80211_channel_switch,
2203 +               .policy = nl80211_policy,
2204 +               .flags = GENL_ADMIN_PERM,
2205 +               .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
2206 +                                 NL80211_FLAG_NEED_RTNL,
2207 +       },
2208  };
2209  
2210  static struct genl_multicast_group nl80211_mlme_mcgrp = {
2211 @@ -10066,7 +10190,8 @@ void cfg80211_mgmt_tx_status(struct wire
2212  
2213         genlmsg_end(msg, hdr);
2214  
2215 -       genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
2216 +       genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
2217 +                               nl80211_mlme_mcgrp.id, gfp);
2218         return;
2219  
2220   nla_put_failure:
2221 --- a/net/wireless/reg.c
2222 +++ b/net/wireless/reg.c
2223 @@ -2247,10 +2247,13 @@ int reg_device_uevent(struct device *dev
2224  
2225  void wiphy_regulatory_register(struct wiphy *wiphy)
2226  {
2227 +       struct regulatory_request *lr;
2228 +
2229         if (!reg_dev_ignore_cell_hint(wiphy))
2230                 reg_num_devs_support_basehint++;
2231  
2232 -       wiphy_update_regulatory(wiphy, NL80211_REGDOM_SET_BY_CORE);
2233 +       lr = get_last_request();
2234 +       wiphy_update_regulatory(wiphy, lr->initiator);
2235  }
2236  
2237  void wiphy_regulatory_deregister(struct wiphy *wiphy)
2238 @@ -2279,7 +2282,9 @@ void wiphy_regulatory_deregister(struct 
2239  static void reg_timeout_work(struct work_struct *work)
2240  {
2241         REG_DBG_PRINT("Timeout while waiting for CRDA to reply, restoring regulatory settings\n");
2242 +       rtnl_lock();
2243         restore_regulatory_settings(true);
2244 +       rtnl_unlock();
2245  }
2246  
2247  int __init regulatory_init(void)
2248 --- a/net/wireless/sme.c
2249 +++ b/net/wireless/sme.c
2250 @@ -34,8 +34,10 @@ struct cfg80211_conn {
2251                 CFG80211_CONN_SCAN_AGAIN,
2252                 CFG80211_CONN_AUTHENTICATE_NEXT,
2253                 CFG80211_CONN_AUTHENTICATING,
2254 +               CFG80211_CONN_AUTH_FAILED,
2255                 CFG80211_CONN_ASSOCIATE_NEXT,
2256                 CFG80211_CONN_ASSOCIATING,
2257 +               CFG80211_CONN_ASSOC_FAILED,
2258                 CFG80211_CONN_DEAUTH,
2259                 CFG80211_CONN_CONNECTED,
2260         } state;
2261 @@ -164,6 +166,8 @@ static int cfg80211_conn_do_work(struct 
2262                                           NULL, 0,
2263                                           params->key, params->key_len,
2264                                           params->key_idx, NULL, 0);
2265 +       case CFG80211_CONN_AUTH_FAILED:
2266 +               return -ENOTCONN;
2267         case CFG80211_CONN_ASSOCIATE_NEXT:
2268                 BUG_ON(!rdev->ops->assoc);
2269                 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
2270 @@ -188,10 +192,17 @@ static int cfg80211_conn_do_work(struct 
2271                                              WLAN_REASON_DEAUTH_LEAVING,
2272                                              false);
2273                 return err;
2274 +       case CFG80211_CONN_ASSOC_FAILED:
2275 +               cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
2276 +                                    NULL, 0,
2277 +                                    WLAN_REASON_DEAUTH_LEAVING, false);
2278 +               return -ENOTCONN;
2279         case CFG80211_CONN_DEAUTH:
2280                 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
2281                                      NULL, 0,
2282                                      WLAN_REASON_DEAUTH_LEAVING, false);
2283 +               /* free directly, disconnected event already sent */
2284 +               cfg80211_sme_free(wdev);
2285                 return 0;
2286         default:
2287                 return 0;
2288 @@ -371,7 +382,7 @@ bool cfg80211_sme_rx_assoc_resp(struct w
2289                 return true;
2290         }
2291  
2292 -       wdev->conn->state = CFG80211_CONN_DEAUTH;
2293 +       wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
2294         schedule_work(&rdev->conn_work);
2295         return false;
2296  }
2297 @@ -383,7 +394,13 @@ void cfg80211_sme_deauth(struct wireless
2298  
2299  void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
2300  {
2301 -       cfg80211_sme_free(wdev);
2302 +       struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
2303 +
2304 +       if (!wdev->conn)
2305 +               return;
2306 +
2307 +       wdev->conn->state = CFG80211_CONN_AUTH_FAILED;
2308 +       schedule_work(&rdev->conn_work);
2309  }
2310  
2311  void cfg80211_sme_disassoc(struct wireless_dev *wdev)
2312 @@ -399,7 +416,13 @@ void cfg80211_sme_disassoc(struct wirele
2313  
2314  void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
2315  {
2316 -       cfg80211_sme_disassoc(wdev);
2317 +       struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
2318 +
2319 +       if (!wdev->conn)
2320 +               return;
2321 +
2322 +       wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
2323 +       schedule_work(&rdev->conn_work);
2324  }
2325  
2326  static int cfg80211_sme_connect(struct wireless_dev *wdev,
2327 @@ -953,21 +976,19 @@ int cfg80211_disconnect(struct cfg80211_
2328                         struct net_device *dev, u16 reason, bool wextev)
2329  {
2330         struct wireless_dev *wdev = dev->ieee80211_ptr;
2331 -       int err;
2332 +       int err = 0;
2333  
2334         ASSERT_WDEV_LOCK(wdev);
2335  
2336         kfree(wdev->connect_keys);
2337         wdev->connect_keys = NULL;
2338  
2339 -       if (wdev->conn) {
2340 +       if (wdev->conn)
2341                 err = cfg80211_sme_disconnect(wdev, reason);
2342 -       } else if (!rdev->ops->disconnect) {
2343 +       else if (!rdev->ops->disconnect)
2344                 cfg80211_mlme_down(rdev, dev);
2345 -               err = 0;
2346 -       } else {
2347 +       else if (wdev->current_bss)
2348                 err = rdev_disconnect(rdev, dev, reason);
2349 -       }
2350  
2351         return err;
2352  }
2353 --- a/net/mac80211/rc80211_minstrel.c
2354 +++ b/net/mac80211/rc80211_minstrel.c
2355 @@ -203,6 +203,15 @@ minstrel_update_stats(struct minstrel_pr
2356         memcpy(mi->max_tp_rate, tmp_tp_rate, sizeof(mi->max_tp_rate));
2357         mi->max_prob_rate = tmp_prob_rate;
2358  
2359 +#ifdef CPTCFG_MAC80211_DEBUGFS
2360 +       /* use fixed index if set */
2361 +       if (mp->fixed_rate_idx != -1) {
2362 +               mi->max_tp_rate[0] = mp->fixed_rate_idx;
2363 +               mi->max_tp_rate[1] = mp->fixed_rate_idx;
2364 +               mi->max_prob_rate = mp->fixed_rate_idx;
2365 +       }
2366 +#endif
2367 +
2368         /* Reset update timer */
2369         mi->stats_update = jiffies;
2370  
2371 @@ -290,7 +299,7 @@ minstrel_get_rate(void *priv, struct iee
2372         struct minstrel_rate *msr, *mr;
2373         unsigned int ndx;
2374         bool mrr_capable;
2375 -       bool prev_sample = mi->prev_sample;
2376 +       bool prev_sample;
2377         int delta;
2378         int sampling_ratio;
2379  
2380 @@ -310,10 +319,16 @@ minstrel_get_rate(void *priv, struct iee
2381         /* increase sum packet counter */
2382         mi->packet_count++;
2383  
2384 +#ifdef CPTCFG_MAC80211_DEBUGFS
2385 +       if (mp->fixed_rate_idx != -1)
2386 +               return;
2387 +#endif
2388 +
2389         delta = (mi->packet_count * sampling_ratio / 100) -
2390                         (mi->sample_count + mi->sample_deferred / 2);
2391  
2392         /* delta < 0: no sampling required */
2393 +       prev_sample = mi->prev_sample;
2394         mi->prev_sample = false;
2395         if (delta < 0 || (!mrr_capable && prev_sample))
2396                 return;
2397 --- a/drivers/net/wireless/rt2x00/rt2x00queue.c
2398 +++ b/drivers/net/wireless/rt2x00/rt2x00queue.c
2399 @@ -936,13 +936,8 @@ void rt2x00queue_index_inc(struct queue_
2400         spin_unlock_irqrestore(&queue->index_lock, irqflags);
2401  }
2402  
2403 -void rt2x00queue_pause_queue(struct data_queue *queue)
2404 +void rt2x00queue_pause_queue_nocheck(struct data_queue *queue)
2405  {
2406 -       if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
2407 -           !test_bit(QUEUE_STARTED, &queue->flags) ||
2408 -           test_and_set_bit(QUEUE_PAUSED, &queue->flags))
2409 -               return;
2410 -
2411         switch (queue->qid) {
2412         case QID_AC_VO:
2413         case QID_AC_VI:
2414 @@ -958,6 +953,15 @@ void rt2x00queue_pause_queue(struct data
2415                 break;
2416         }
2417  }
2418 +void rt2x00queue_pause_queue(struct data_queue *queue)
2419 +{
2420 +       if (!test_bit(DEVICE_STATE_PRESENT, &queue->rt2x00dev->flags) ||
2421 +           !test_bit(QUEUE_STARTED, &queue->flags) ||
2422 +           test_and_set_bit(QUEUE_PAUSED, &queue->flags))
2423 +               return;
2424 +
2425 +       rt2x00queue_pause_queue_nocheck(queue);
2426 +}
2427  EXPORT_SYMBOL_GPL(rt2x00queue_pause_queue);
2428  
2429  void rt2x00queue_unpause_queue(struct data_queue *queue)
2430 @@ -1019,7 +1023,7 @@ void rt2x00queue_stop_queue(struct data_
2431                 return;
2432         }
2433  
2434 -       rt2x00queue_pause_queue(queue);
2435 +       rt2x00queue_pause_queue_nocheck(queue);
2436  
2437         queue->rt2x00dev->ops->lib->stop_queue(queue);
2438  
2439 --- a/net/mac80211/mlme.c
2440 +++ b/net/mac80211/mlme.c
2441 @@ -31,10 +31,12 @@
2442  #include "led.h"
2443  
2444  #define IEEE80211_AUTH_TIMEOUT         (HZ / 5)
2445 +#define IEEE80211_AUTH_TIMEOUT_LONG    (HZ / 2)
2446  #define IEEE80211_AUTH_TIMEOUT_SHORT   (HZ / 10)
2447  #define IEEE80211_AUTH_MAX_TRIES       3
2448  #define IEEE80211_AUTH_WAIT_ASSOC      (HZ * 5)
2449  #define IEEE80211_ASSOC_TIMEOUT                (HZ / 5)
2450 +#define IEEE80211_ASSOC_TIMEOUT_LONG   (HZ / 2)
2451  #define IEEE80211_ASSOC_TIMEOUT_SHORT  (HZ / 10)
2452  #define IEEE80211_ASSOC_MAX_TRIES      3
2453  
2454 @@ -209,8 +211,9 @@ ieee80211_determine_chantype(struct ieee
2455                              struct ieee80211_channel *channel,
2456                              const struct ieee80211_ht_operation *ht_oper,
2457                              const struct ieee80211_vht_operation *vht_oper,
2458 -                            struct cfg80211_chan_def *chandef, bool verbose)
2459 +                            struct cfg80211_chan_def *chandef, bool tracking)
2460  {
2461 +       struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2462         struct cfg80211_chan_def vht_chandef;
2463         u32 ht_cfreq, ret;
2464  
2465 @@ -229,7 +232,7 @@ ieee80211_determine_chantype(struct ieee
2466         ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
2467                                                   channel->band);
2468         /* check that channel matches the right operating channel */
2469 -       if (channel->center_freq != ht_cfreq) {
2470 +       if (!tracking && channel->center_freq != ht_cfreq) {
2471                 /*
2472                  * It's possible that some APs are confused here;
2473                  * Netgear WNDR3700 sometimes reports 4 higher than
2474 @@ -237,11 +240,10 @@ ieee80211_determine_chantype(struct ieee
2475                  * since we look at probe response/beacon data here
2476                  * it should be OK.
2477                  */
2478 -               if (verbose)
2479 -                       sdata_info(sdata,
2480 -                                  "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
2481 -                                  channel->center_freq, ht_cfreq,
2482 -                                  ht_oper->primary_chan, channel->band);
2483 +               sdata_info(sdata,
2484 +                          "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
2485 +                          channel->center_freq, ht_cfreq,
2486 +                          ht_oper->primary_chan, channel->band);
2487                 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2488                 goto out;
2489         }
2490 @@ -295,7 +297,7 @@ ieee80211_determine_chantype(struct ieee
2491                                 channel->band);
2492                 break;
2493         default:
2494 -               if (verbose)
2495 +               if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2496                         sdata_info(sdata,
2497                                    "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
2498                                    vht_oper->chan_width);
2499 @@ -304,7 +306,7 @@ ieee80211_determine_chantype(struct ieee
2500         }
2501  
2502         if (!cfg80211_chandef_valid(&vht_chandef)) {
2503 -               if (verbose)
2504 +               if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2505                         sdata_info(sdata,
2506                                    "AP VHT information is invalid, disable VHT\n");
2507                 ret = IEEE80211_STA_DISABLE_VHT;
2508 @@ -317,7 +319,7 @@ ieee80211_determine_chantype(struct ieee
2509         }
2510  
2511         if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
2512 -               if (verbose)
2513 +               if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
2514                         sdata_info(sdata,
2515                                    "AP VHT information doesn't match HT, disable VHT\n");
2516                 ret = IEEE80211_STA_DISABLE_VHT;
2517 @@ -333,18 +335,27 @@ out:
2518         if (ret & IEEE80211_STA_DISABLE_VHT)
2519                 vht_chandef = *chandef;
2520  
2521 +       /*
2522 +        * Ignore the DISABLED flag when we're already connected and only
2523 +        * tracking the APs beacon for bandwidth changes - otherwise we
2524 +        * might get disconnected here if we connect to an AP, update our
2525 +        * regulatory information based on the AP's country IE and the
2526 +        * information we have is wrong/outdated and disables the channel
2527 +        * that we're actually using for the connection to the AP.
2528 +        */
2529         while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
2530 -                                       IEEE80211_CHAN_DISABLED)) {
2531 +                                       tracking ? 0 :
2532 +                                                  IEEE80211_CHAN_DISABLED)) {
2533                 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
2534                         ret = IEEE80211_STA_DISABLE_HT |
2535                               IEEE80211_STA_DISABLE_VHT;
2536 -                       goto out;
2537 +                       break;
2538                 }
2539  
2540                 ret |= chandef_downgrade(chandef);
2541         }
2542  
2543 -       if (chandef->width != vht_chandef.width && verbose)
2544 +       if (chandef->width != vht_chandef.width && !tracking)
2545                 sdata_info(sdata,
2546                            "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
2547  
2548 @@ -384,7 +395,7 @@ static int ieee80211_config_bw(struct ie
2549  
2550         /* calculate new channel (type) based on HT/VHT operation IEs */
2551         flags = ieee80211_determine_chantype(sdata, sband, chan, ht_oper,
2552 -                                            vht_oper, &chandef, false);
2553 +                                            vht_oper, &chandef, true);
2554  
2555         /*
2556          * Downgrade the new channel if we associated with restricted
2557 @@ -1043,6 +1054,13 @@ ieee80211_sta_process_chanswitch(struct 
2558                 if (!ieee80211_operating_class_to_band(
2559                                 elems->ext_chansw_ie->new_operating_class,
2560                                 &new_band)) {
2561 +                       /*
2562 +                        * Some APs send invalid ECSA IEs in probe response
2563 +                        * frames, so check for these and ignore them.
2564 +                        */
2565 +                       if (beacon && elems->ext_chansw_ie->new_ch_num == 0 &&
2566 +                           elems->ext_chansw_ie->new_operating_class == 0)
2567 +                               return;
2568                         sdata_info(sdata,
2569                                    "cannot understand ECSA IE operating class %d, disconnecting\n",
2570                                    elems->ext_chansw_ie->new_operating_class);
2571 @@ -1110,6 +1128,15 @@ ieee80211_sta_process_chanswitch(struct 
2572         case -1:
2573                 cfg80211_chandef_create(&new_chandef, new_chan,
2574                                         NL80211_CHAN_NO_HT);
2575 +               /* keep width for 5/10 MHz channels */
2576 +               switch (sdata->vif.bss_conf.chandef.width) {
2577 +               case NL80211_CHAN_WIDTH_5:
2578 +               case NL80211_CHAN_WIDTH_10:
2579 +                       new_chandef.width = sdata->vif.bss_conf.chandef.width;
2580 +                       break;
2581 +               default:
2582 +                       break;
2583 +               }
2584                 break;
2585         }
2586  
2587 @@ -3394,10 +3421,13 @@ static int ieee80211_probe_auth(struct i
2588  
2589         if (tx_flags == 0) {
2590                 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
2591 -               ifmgd->auth_data->timeout_started = true;
2592 +               auth_data->timeout_started = true;
2593                 run_again(sdata, auth_data->timeout);
2594         } else {
2595 -               auth_data->timeout_started = false;
2596 +               auth_data->timeout =
2597 +                       round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
2598 +               auth_data->timeout_started = true;
2599 +               run_again(sdata, auth_data->timeout);
2600         }
2601  
2602         return 0;
2603 @@ -3434,7 +3464,11 @@ static int ieee80211_do_assoc(struct iee
2604                 assoc_data->timeout_started = true;
2605                 run_again(sdata, assoc_data->timeout);
2606         } else {
2607 -               assoc_data->timeout_started = false;
2608 +               assoc_data->timeout =
2609 +                       round_jiffies_up(jiffies +
2610 +                                        IEEE80211_ASSOC_TIMEOUT_LONG);
2611 +               assoc_data->timeout_started = true;
2612 +               run_again(sdata, assoc_data->timeout);
2613         }
2614  
2615         return 0;
2616 @@ -3829,7 +3863,7 @@ static int ieee80211_prep_channel(struct
2617         ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
2618                                                      cbss->channel,
2619                                                      ht_oper, vht_oper,
2620 -                                                    &chandef, true);
2621 +                                                    &chandef, false);
2622  
2623         sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
2624                                       local->rx_chains);
2625 --- a/net/wireless/core.c
2626 +++ b/net/wireless/core.c
2627 @@ -772,6 +772,7 @@ void cfg80211_leave(struct cfg80211_regi
2628                 cfg80211_leave_mesh(rdev, dev);
2629                 break;
2630         case NL80211_IFTYPE_AP:
2631 +       case NL80211_IFTYPE_P2P_GO:
2632                 cfg80211_stop_ap(rdev, dev);
2633                 break;
2634         default:
2635 --- a/drivers/net/wireless/rtlwifi/Kconfig
2636 +++ b/drivers/net/wireless/rtlwifi/Kconfig
2637 @@ -1,29 +1,22 @@
2638 -config RTLWIFI
2639 -       tristate "Realtek wireless card support"
2640 +menuconfig RTL_CARDS
2641 +       tristate "Realtek rtlwifi family of devices"
2642         depends on m
2643 -       depends on MAC80211
2644 -       select BACKPORT_FW_LOADER
2645 -       ---help---
2646 -         This is common code for RTL8192CE/RTL8192CU/RTL8192SE/RTL8723AE
2647 -         drivers.  This module does nothing by itself - the various front-end
2648 -         drivers need to be enabled to support any desired devices.
2649 -
2650 -         If you choose to build as a module, it'll be called rtlwifi.
2651 -
2652 -config RTLWIFI_DEBUG
2653 -       bool "Debugging output for rtlwifi driver family"
2654 -       depends on RTLWIFI
2655 +       depends on MAC80211 && (PCI || USB)
2656         default y
2657         ---help---
2658 -       To use the module option that sets the dynamic-debugging level for,
2659 -       the front-end driver, this parameter must be "Y". For memory-limited
2660 -       systems, choose "N". If in doubt, choose "Y".
2661 +         This option will enable support for the Realtek mac80211-based
2662 +         wireless drivers. Drivers rtl8192ce, rtl8192cu, rtl8192se, rtl8192de,
2663 +         rtl8723eu, and rtl8188eu share some common code.
2664 +
2665 +if RTL_CARDS
2666  
2667  config RTL8192CE
2668         tristate "Realtek RTL8192CE/RTL8188CE Wireless Network Adapter"
2669         depends on m
2670 -       depends on RTLWIFI && PCI
2671 +       depends on PCI
2672         select RTL8192C_COMMON
2673 +       select RTLWIFI
2674 +       select RTLWIFI_PCI
2675         ---help---
2676         This is the driver for Realtek RTL8192CE/RTL8188CE 802.11n PCIe
2677         wireless network adapters.
2678 @@ -33,7 +26,9 @@ config RTL8192CE
2679  config RTL8192SE
2680         tristate "Realtek RTL8192SE/RTL8191SE PCIe Wireless Network Adapter"
2681         depends on m
2682 -       depends on RTLWIFI && PCI
2683 +       depends on PCI
2684 +       select RTLWIFI
2685 +       select RTLWIFI_PCI
2686         ---help---
2687         This is the driver for Realtek RTL8192SE/RTL8191SE 802.11n PCIe
2688         wireless network adapters.
2689 @@ -43,7 +38,9 @@ config RTL8192SE
2690  config RTL8192DE
2691         tristate "Realtek RTL8192DE/RTL8188DE PCIe Wireless Network Adapter"
2692         depends on m
2693 -       depends on RTLWIFI && PCI
2694 +       depends on PCI
2695 +       select RTLWIFI
2696 +       select RTLWIFI_PCI
2697         ---help---
2698         This is the driver for Realtek RTL8192DE/RTL8188DE 802.11n PCIe
2699         wireless network adapters.
2700 @@ -53,7 +50,9 @@ config RTL8192DE
2701  config RTL8723AE
2702         tristate "Realtek RTL8723AE PCIe Wireless Network Adapter"
2703         depends on m
2704 -       depends on RTLWIFI && PCI
2705 +       depends on PCI
2706 +       select RTLWIFI
2707 +       select RTLWIFI_PCI
2708         ---help---
2709         This is the driver for Realtek RTL8723AE 802.11n PCIe
2710         wireless network adapters.
2711 @@ -63,7 +62,9 @@ config RTL8723AE
2712  config RTL8188EE
2713         tristate "Realtek RTL8188EE Wireless Network Adapter"
2714         depends on m
2715 -       depends on RTLWIFI && PCI
2716 +       depends on PCI
2717 +       select RTLWIFI
2718 +       select RTLWIFI_PCI
2719         ---help---
2720         This is the driver for Realtek RTL8188EE 802.11n PCIe
2721         wireless network adapters.
2722 @@ -73,7 +74,9 @@ config RTL8188EE
2723  config RTL8192CU
2724         tristate "Realtek RTL8192CU/RTL8188CU USB Wireless Network Adapter"
2725         depends on m
2726 -       depends on RTLWIFI && USB
2727 +       depends on USB
2728 +       select RTLWIFI
2729 +       select RTLWIFI_USB
2730         select RTL8192C_COMMON
2731         ---help---
2732         This is the driver for Realtek RTL8192CU/RTL8188CU 802.11n USB
2733 @@ -81,8 +84,32 @@ config RTL8192CU
2734  
2735         If you choose to build it as a module, it will be called rtl8192cu
2736  
2737 +config RTLWIFI
2738 +       tristate
2739 +       depends on m
2740 +       select BACKPORT_FW_LOADER
2741 +
2742 +config RTLWIFI_PCI
2743 +       tristate
2744 +       depends on m
2745 +
2746 +config RTLWIFI_USB
2747 +       tristate
2748 +       depends on m
2749 +
2750 +config RTLWIFI_DEBUG
2751 +       bool "Debugging output for rtlwifi driver family"
2752 +       depends on RTLWIFI
2753 +       default y
2754 +       ---help---
2755 +       To use the module option that sets the dynamic-debugging level for,
2756 +       the front-end driver, this parameter must be "Y". For memory-limited
2757 +       systems, choose "N". If in doubt, choose "Y".
2758 +
2759  config RTL8192C_COMMON
2760         tristate
2761         depends on m
2762         depends on RTL8192CE || RTL8192CU
2763 -       default m
2764 +       default y
2765 +
2766 +endif
2767 --- a/drivers/net/wireless/rtlwifi/Makefile
2768 +++ b/drivers/net/wireless/rtlwifi/Makefile
2769 @@ -12,13 +12,11 @@ rtlwifi-objs        :=              \
2770  
2771  rtl8192c_common-objs +=                \
2772  
2773 -ifneq ($(CONFIG_PCI),)
2774 -rtlwifi-objs   += pci.o
2775 -endif
2776 +obj-$(CPTCFG_RTLWIFI_PCI)      += rtl_pci.o
2777 +rtl_pci-objs   :=              pci.o
2778  
2779 -ifneq ($(CONFIG_USB),)
2780 -rtlwifi-objs   += usb.o
2781 -endif
2782 +obj-$(CPTCFG_RTLWIFI_USB)      += rtl_usb.o
2783 +rtl_usb-objs   :=              usb.o
2784  
2785  obj-$(CPTCFG_RTL8192C_COMMON)  += rtl8192c/
2786  obj-$(CPTCFG_RTL8192CE)                += rtl8192ce/
2787 --- a/drivers/net/wireless/rtlwifi/ps.h
2788 +++ b/drivers/net/wireless/rtlwifi/ps.h
2789 @@ -49,5 +49,6 @@ void rtl_swlps_rf_awake(struct ieee80211
2790  void rtl_swlps_rf_sleep(struct ieee80211_hw *hw);
2791  void rtl_p2p_ps_cmd(struct ieee80211_hw *hw, u8 p2p_ps_state);
2792  void rtl_p2p_info(struct ieee80211_hw *hw, void *data, unsigned int len);
2793 +void rtl_lps_change_work_callback(struct work_struct *work);
2794  
2795  #endif
2796 --- a/drivers/net/wireless/rtlwifi/base.c
2797 +++ b/drivers/net/wireless/rtlwifi/base.c
2798 @@ -173,6 +173,7 @@ u8 rtl_tid_to_ac(u8 tid)
2799  {
2800         return tid_to_ac[tid];
2801  }
2802 +EXPORT_SYMBOL_GPL(rtl_tid_to_ac);
2803  
2804  static void _rtl_init_hw_ht_capab(struct ieee80211_hw *hw,
2805                                   struct ieee80211_sta_ht_cap *ht_cap)
2806 @@ -407,6 +408,7 @@ void rtl_deinit_deferred_work(struct iee
2807         cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
2808         cancel_delayed_work(&rtlpriv->works.fwevt_wq);
2809  }
2810 +EXPORT_SYMBOL_GPL(rtl_deinit_deferred_work);
2811  
2812  void rtl_init_rfkill(struct ieee80211_hw *hw)
2813  {
2814 @@ -440,6 +442,7 @@ void rtl_deinit_rfkill(struct ieee80211_
2815  {
2816         wiphy_rfkill_stop_polling(hw->wiphy);
2817  }
2818 +EXPORT_SYMBOL_GPL(rtl_deinit_rfkill);
2819  
2820  int rtl_init_core(struct ieee80211_hw *hw)
2821  {
2822 @@ -490,10 +493,12 @@ int rtl_init_core(struct ieee80211_hw *h
2823  
2824         return 0;
2825  }
2826 +EXPORT_SYMBOL_GPL(rtl_init_core);
2827  
2828  void rtl_deinit_core(struct ieee80211_hw *hw)
2829  {
2830  }
2831 +EXPORT_SYMBOL_GPL(rtl_deinit_core);
2832  
2833  void rtl_init_rx_config(struct ieee80211_hw *hw)
2834  {
2835 @@ -502,6 +507,7 @@ void rtl_init_rx_config(struct ieee80211
2836  
2837         rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
2838  }
2839 +EXPORT_SYMBOL_GPL(rtl_init_rx_config);
2840  
2841  /*********************************************************
2842   *
2843 @@ -880,6 +886,7 @@ bool rtl_tx_mgmt_proc(struct ieee80211_h
2844  
2845         return true;
2846  }
2847 +EXPORT_SYMBOL_GPL(rtl_tx_mgmt_proc);
2848  
2849  void rtl_get_tcb_desc(struct ieee80211_hw *hw,
2850                       struct ieee80211_tx_info *info,
2851 @@ -1053,6 +1060,7 @@ bool rtl_action_proc(struct ieee80211_hw
2852  
2853         return true;
2854  }
2855 +EXPORT_SYMBOL_GPL(rtl_action_proc);
2856  
2857  /*should call before software enc*/
2858  u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
2859 @@ -1126,6 +1134,7 @@ u8 rtl_is_special_data(struct ieee80211_
2860  
2861         return false;
2862  }
2863 +EXPORT_SYMBOL_GPL(rtl_is_special_data);
2864  
2865  /*********************************************************
2866   *
2867 @@ -1301,6 +1310,7 @@ void rtl_beacon_statistic(struct ieee802
2868  
2869         rtlpriv->link_info.bcn_rx_inperiod++;
2870  }
2871 +EXPORT_SYMBOL_GPL(rtl_beacon_statistic);
2872  
2873  void rtl_watchdog_wq_callback(void *data)
2874  {
2875 @@ -1794,6 +1804,7 @@ void rtl_recognize_peer(struct ieee80211
2876  
2877         mac->vendor = vendor;
2878  }
2879 +EXPORT_SYMBOL_GPL(rtl_recognize_peer);
2880  
2881  /*********************************************************
2882   *
2883 @@ -1850,6 +1861,7 @@ struct attribute_group rtl_attribute_gro
2884         .name = "rtlsysfs",
2885         .attrs = rtl_sysfs_entries,
2886  };
2887 +EXPORT_SYMBOL_GPL(rtl_attribute_group);
2888  
2889  MODULE_AUTHOR("lizhaoming      <chaoming_li@realsil.com.cn>");
2890  MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>");
2891 @@ -1857,7 +1869,8 @@ MODULE_AUTHOR("Larry Finger       <Larry.FInge
2892  MODULE_LICENSE("GPL");
2893  MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
2894  
2895 -struct rtl_global_var global_var = {};
2896 +struct rtl_global_var rtl_global_var = {};
2897 +EXPORT_SYMBOL_GPL(rtl_global_var);
2898  
2899  static int __init rtl_core_module_init(void)
2900  {
2901 @@ -1865,8 +1878,8 @@ static int __init rtl_core_module_init(v
2902                 pr_err("Unable to register rtl_rc, use default RC !!\n");
2903  
2904         /* init some global vars */
2905 -       INIT_LIST_HEAD(&global_var.glb_priv_list);
2906 -       spin_lock_init(&global_var.glb_list_lock);
2907 +       INIT_LIST_HEAD(&rtl_global_var.glb_priv_list);
2908 +       spin_lock_init(&rtl_global_var.glb_list_lock);
2909  
2910         return 0;
2911  }
2912 --- a/drivers/net/wireless/rtlwifi/base.h
2913 +++ b/drivers/net/wireless/rtlwifi/base.h
2914 @@ -147,7 +147,7 @@ void rtl_recognize_peer(struct ieee80211
2915  u8 rtl_tid_to_ac(u8 tid);
2916  extern struct attribute_group rtl_attribute_group;
2917  void rtl_easy_concurrent_retrytimer_callback(unsigned long data);
2918 -extern struct rtl_global_var global_var;
2919 +extern struct rtl_global_var rtl_global_var;
2920  int rtlwifi_rate_mapping(struct ieee80211_hw *hw,
2921                          bool isht, u8 desc_rate, bool first_ampdu);
2922  bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb);
2923 --- a/drivers/net/wireless/rtlwifi/core.c
2924 +++ b/drivers/net/wireless/rtlwifi/core.c
2925 @@ -1330,3 +1330,4 @@ const struct ieee80211_ops rtl_ops = {
2926         .rfkill_poll = rtl_op_rfkill_poll,
2927         .flush = rtl_op_flush,
2928  };
2929 +EXPORT_SYMBOL_GPL(rtl_ops);
2930 --- a/drivers/net/wireless/rtlwifi/debug.c
2931 +++ b/drivers/net/wireless/rtlwifi/debug.c
2932 @@ -51,3 +51,4 @@ void rtl_dbgp_flag_init(struct ieee80211
2933  
2934         /*Init Debug flag enable condition */
2935  }
2936 +EXPORT_SYMBOL_GPL(rtl_dbgp_flag_init);
2937 --- a/drivers/net/wireless/rtlwifi/efuse.c
2938 +++ b/drivers/net/wireless/rtlwifi/efuse.c
2939 @@ -229,6 +229,7 @@ void read_efuse_byte(struct ieee80211_hw
2940  
2941         *pbuf = (u8) (value32 & 0xff);
2942  }
2943 +EXPORT_SYMBOL_GPL(read_efuse_byte);
2944  
2945  void read_efuse(struct ieee80211_hw *hw, u16 _offset, u16 _size_byte, u8 *pbuf)
2946  {
2947 --- a/drivers/net/wireless/rtlwifi/pci.c
2948 +++ b/drivers/net/wireless/rtlwifi/pci.c
2949 @@ -35,6 +35,13 @@
2950  #include "efuse.h"
2951  #include <linux/export.h>
2952  #include <linux/kmemleak.h>
2953 +#include <linux/module.h>
2954 +
2955 +MODULE_AUTHOR("lizhaoming      <chaoming_li@realsil.com.cn>");
2956 +MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>");
2957 +MODULE_AUTHOR("Larry Finger    <Larry.FInger@lwfinger.net>");
2958 +MODULE_LICENSE("GPL");
2959 +MODULE_DESCRIPTION("PCI basic driver for rtlwifi");
2960  
2961  static const u16 pcibridge_vendors[PCI_BRIDGE_VENDOR_MAX] = {
2962         PCI_VENDOR_ID_INTEL,
2963 @@ -1008,19 +1015,6 @@ static void _rtl_pci_prepare_bcn_tasklet
2964         return;
2965  }
2966  
2967 -static void rtl_lps_change_work_callback(struct work_struct *work)
2968 -{
2969 -       struct rtl_works *rtlworks =
2970 -           container_of(work, struct rtl_works, lps_change_work);
2971 -       struct ieee80211_hw *hw = rtlworks->hw;
2972 -       struct rtl_priv *rtlpriv = rtl_priv(hw);
2973 -
2974 -       if (rtlpriv->enter_ps)
2975 -               rtl_lps_enter(hw);
2976 -       else
2977 -               rtl_lps_leave(hw);
2978 -}
2979 -
2980  static void _rtl_pci_init_trx_var(struct ieee80211_hw *hw)
2981  {
2982         struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
2983 @@ -1899,7 +1893,7 @@ int rtl_pci_probe(struct pci_dev *pdev,
2984         rtlpriv->rtlhal.interface = INTF_PCI;
2985         rtlpriv->cfg = (struct rtl_hal_cfg *)(id->driver_data);
2986         rtlpriv->intf_ops = &rtl_pci_ops;
2987 -       rtlpriv->glb_var = &global_var;
2988 +       rtlpriv->glb_var = &rtl_global_var;
2989  
2990         /*
2991          *init dbgp flags before all
2992 --- a/drivers/net/wireless/rtlwifi/ps.c
2993 +++ b/drivers/net/wireless/rtlwifi/ps.c
2994 @@ -269,6 +269,7 @@ void rtl_ips_nic_on(struct ieee80211_hw 
2995  
2996         spin_unlock_irqrestore(&rtlpriv->locks.ips_lock, flags);
2997  }
2998 +EXPORT_SYMBOL_GPL(rtl_ips_nic_on);
2999  
3000  /*for FW LPS*/
3001  
3002 @@ -518,6 +519,7 @@ void rtl_swlps_beacon(struct ieee80211_h
3003                          "u_bufferd: %x, m_buffered: %x\n", u_buffed, m_buffed);
3004         }
3005  }
3006 +EXPORT_SYMBOL_GPL(rtl_swlps_beacon);
3007  
3008  void rtl_swlps_rf_awake(struct ieee80211_hw *hw)
3009  {
3010 @@ -611,6 +613,19 @@ void rtl_swlps_rf_sleep(struct ieee80211
3011                         MSECS(sleep_intv * mac->vif->bss_conf.beacon_int - 40));
3012  }
3013  
3014 +void rtl_lps_change_work_callback(struct work_struct *work)
3015 +{
3016 +       struct rtl_works *rtlworks =
3017 +           container_of(work, struct rtl_works, lps_change_work);
3018 +       struct ieee80211_hw *hw = rtlworks->hw;
3019 +       struct rtl_priv *rtlpriv = rtl_priv(hw);
3020 +
3021 +       if (rtlpriv->enter_ps)
3022 +               rtl_lps_enter(hw);
3023 +       else
3024 +               rtl_lps_leave(hw);
3025 +}
3026 +EXPORT_SYMBOL_GPL(rtl_lps_change_work_callback);
3027  
3028  void rtl_swlps_wq_callback(void *data)
3029  {
3030 @@ -922,3 +937,4 @@ void rtl_p2p_info(struct ieee80211_hw *h
3031         else
3032                 rtl_p2p_noa_ie(hw, data, len - FCS_LEN);
3033  }
3034 +EXPORT_SYMBOL_GPL(rtl_p2p_info);
3035 --- a/drivers/net/wireless/rtlwifi/usb.c
3036 +++ b/drivers/net/wireless/rtlwifi/usb.c
3037 @@ -32,6 +32,13 @@
3038  #include "ps.h"
3039  #include "rtl8192c/fw_common.h"
3040  #include <linux/export.h>
3041 +#include <linux/module.h>
3042 +
3043 +MODULE_AUTHOR("lizhaoming      <chaoming_li@realsil.com.cn>");
3044 +MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>");
3045 +MODULE_AUTHOR("Larry Finger    <Larry.FInger@lwfinger.net>");
3046 +MODULE_LICENSE("GPL");
3047 +MODULE_DESCRIPTION("USB basic driver for rtlwifi");
3048  
3049  #define        REALTEK_USB_VENQT_READ                  0xC0
3050  #define        REALTEK_USB_VENQT_WRITE                 0x40
3051 @@ -1070,6 +1077,8 @@ int rtl_usb_probe(struct usb_interface *
3052         spin_lock_init(&rtlpriv->locks.usb_lock);
3053         INIT_WORK(&rtlpriv->works.fill_h2c_cmd,
3054                   rtl_fill_h2c_cmd_work_callback);
3055 +       INIT_WORK(&rtlpriv->works.lps_change_work,
3056 +                 rtl_lps_change_work_callback);
3057  
3058         rtlpriv->usb_data_index = 0;
3059         init_completion(&rtlpriv->firmware_loading_complete);
3060 --- a/drivers/net/wireless/ath/ath9k/ath9k.h
3061 +++ b/drivers/net/wireless/ath/ath9k/ath9k.h
3062 @@ -72,17 +72,12 @@ struct ath_config {
3063  /*************************/
3064  
3065  #define ATH_TXBUF_RESET(_bf) do {                              \
3066 -               (_bf)->bf_stale = false;                        \
3067                 (_bf)->bf_lastbf = NULL;                        \
3068                 (_bf)->bf_next = NULL;                          \
3069                 memset(&((_bf)->bf_state), 0,                   \
3070                        sizeof(struct ath_buf_state));           \
3071         } while (0)
3072  
3073 -#define ATH_RXBUF_RESET(_bf) do {              \
3074 -               (_bf)->bf_stale = false;        \
3075 -       } while (0)
3076 -
3077  /**
3078   * enum buffer_type - Buffer type flags
3079   *
3080 @@ -137,7 +132,8 @@ int ath_descdma_setup(struct ath_softc *
3081  #define ATH_AGGR_ENCRYPTDELIM      10
3082  /* minimum h/w qdepth to be sustained to maximize aggregation */
3083  #define ATH_AGGR_MIN_QDEPTH        2
3084 -#define ATH_AMPDU_SUBFRAME_DEFAULT 32
3085 +/* minimum h/w qdepth for non-aggregated traffic */
3086 +#define ATH_NON_AGGR_MIN_QDEPTH    8
3087  
3088  #define IEEE80211_SEQ_SEQ_SHIFT    4
3089  #define IEEE80211_SEQ_MAX          4096
3090 @@ -174,12 +170,6 @@ int ath_descdma_setup(struct ath_softc *
3091  
3092  #define ATH_TX_COMPLETE_POLL_INT       1000
3093  
3094 -enum ATH_AGGR_STATUS {
3095 -       ATH_AGGR_DONE,
3096 -       ATH_AGGR_BAW_CLOSED,
3097 -       ATH_AGGR_LIMITED,
3098 -};
3099 -
3100  #define ATH_TXFIFO_DEPTH 8
3101  struct ath_txq {
3102         int mac80211_qnum; /* mac80211 queue number, -1 means not mac80211 Q */
3103 @@ -201,10 +191,10 @@ struct ath_txq {
3104  
3105  struct ath_atx_ac {
3106         struct ath_txq *txq;
3107 -       int sched;
3108         struct list_head list;
3109         struct list_head tid_q;
3110         bool clear_ps_filter;
3111 +       bool sched;
3112  };
3113  
3114  struct ath_frame_info {
3115 @@ -212,14 +202,16 @@ struct ath_frame_info {
3116         int framelen;
3117         enum ath9k_key_type keytype;
3118         u8 keyix;
3119 -       u8 retries;
3120         u8 rtscts_rate;
3121 +       u8 retries : 7;
3122 +       u8 baw_tracked : 1;
3123  };
3124  
3125  struct ath_buf_state {
3126         u8 bf_type;
3127         u8 bfs_paprd;
3128         u8 ndelim;
3129 +       bool stale;
3130         u16 seqno;
3131         unsigned long bfs_paprd_timestamp;
3132  };
3133 @@ -233,7 +225,6 @@ struct ath_buf {
3134         void *bf_desc;                  /* virtual addr of desc */
3135         dma_addr_t bf_daddr;            /* physical addr of desc */
3136         dma_addr_t bf_buf_addr; /* physical addr of data buffer, for DMA */
3137 -       bool bf_stale;
3138         struct ieee80211_tx_rate rates[4];
3139         struct ath_buf_state bf_state;
3140  };
3141 @@ -241,16 +232,18 @@ struct ath_buf {
3142  struct ath_atx_tid {
3143         struct list_head list;
3144         struct sk_buff_head buf_q;
3145 +       struct sk_buff_head retry_q;
3146         struct ath_node *an;
3147         struct ath_atx_ac *ac;
3148         unsigned long tx_buf[BITS_TO_LONGS(ATH_TID_MAX_BUFS)];
3149 -       int bar_index;
3150         u16 seq_start;
3151         u16 seq_next;
3152         u16 baw_size;
3153 -       int tidno;
3154 +       u8 tidno;
3155         int baw_head;   /* first un-acked tx buffer */
3156         int baw_tail;   /* next unused tx buffer slot */
3157 +
3158 +       s8 bar_index;
3159         bool sched;
3160         bool paused;
3161         bool active;
3162 @@ -262,12 +255,13 @@ struct ath_node {
3163         struct ieee80211_vif *vif; /* interface with which we're associated */
3164         struct ath_atx_tid tid[IEEE80211_NUM_TIDS];
3165         struct ath_atx_ac ac[IEEE80211_NUM_ACS];
3166 -       int ps_key;
3167  
3168         u16 maxampdu;
3169         u8 mpdudensity;
3170 +       s8 ps_key;
3171  
3172         bool sleeping;
3173 +       bool no_ps_filter;
3174  
3175  #if defined(CPTCFG_MAC80211_DEBUGFS) && defined(CPTCFG_ATH9K_DEBUGFS)
3176         struct dentry *node_stat;
3177 @@ -317,6 +311,7 @@ struct ath_rx {
3178         struct ath_descdma rxdma;
3179         struct ath_rx_edma rx_edma[ATH9K_RX_QUEUE_MAX];
3180  
3181 +       struct ath_buf *buf_hold;
3182         struct sk_buff *frag;
3183  
3184         u32 ampdu_ref;
3185 @@ -367,6 +362,7 @@ void ath9k_release_buffered_frames(struc
3186  /********/
3187  
3188  struct ath_vif {
3189 +       struct ath_node mcast_node;
3190         int av_bslot;
3191         bool primary_sta_vif;
3192         __le64 tsf_adjust; /* TSF adjustment for staggered beacons */
3193 @@ -585,19 +581,14 @@ static inline void ath_fill_led_pin(stru
3194  #define ATH_ANT_DIV_COMB_MAX_COUNT 100
3195  #define ATH_ANT_DIV_COMB_ALT_ANT_RATIO 30
3196  #define ATH_ANT_DIV_COMB_ALT_ANT_RATIO2 20
3197 +#define ATH_ANT_DIV_COMB_ALT_ANT_RATIO_LOW_RSSI 50
3198 +#define ATH_ANT_DIV_COMB_ALT_ANT_RATIO2_LOW_RSSI 50
3199  
3200  #define ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA -1
3201  #define ATH_ANT_DIV_COMB_LNA1_DELTA_HI -4
3202  #define ATH_ANT_DIV_COMB_LNA1_DELTA_MID -2
3203  #define ATH_ANT_DIV_COMB_LNA1_DELTA_LOW 2
3204  
3205 -enum ath9k_ant_div_comb_lna_conf {
3206 -       ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2,
3207 -       ATH_ANT_DIV_COMB_LNA2,
3208 -       ATH_ANT_DIV_COMB_LNA1,
3209 -       ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2,
3210 -};
3211 -
3212  struct ath_ant_comb {
3213         u16 count;
3214         u16 total_pkt_count;
3215 @@ -614,27 +605,36 @@ struct ath_ant_comb {
3216         int rssi_first;
3217         int rssi_second;
3218         int rssi_third;
3219 +       int ant_ratio;
3220 +       int ant_ratio2;
3221         bool alt_good;
3222         int quick_scan_cnt;
3223 -       int main_conf;
3224 +       enum ath9k_ant_div_comb_lna_conf main_conf;
3225         enum ath9k_ant_div_comb_lna_conf first_quick_scan_conf;
3226         enum ath9k_ant_div_comb_lna_conf second_quick_scan_conf;
3227         bool first_ratio;
3228         bool second_ratio;
3229         unsigned long scan_start_time;
3230 +
3231 +       /*
3232 +        * Card-specific config values.
3233 +        */
3234 +       int low_rssi_thresh;
3235 +       int fast_div_bias;
3236  };
3237  
3238  void ath_ant_comb_scan(struct ath_softc *sc, struct ath_rx_status *rs);
3239 -void ath_ant_comb_update(struct ath_softc *sc);
3240  
3241  /********************/
3242  /* Main driver core */
3243  /********************/
3244  
3245 -#define ATH9K_PCI_CUS198 0x0001
3246 -#define ATH9K_PCI_CUS230 0x0002
3247 -#define ATH9K_PCI_CUS217 0x0004
3248 -#define ATH9K_PCI_WOW    0x0008
3249 +#define ATH9K_PCI_CUS198     0x0001
3250 +#define ATH9K_PCI_CUS230     0x0002
3251 +#define ATH9K_PCI_CUS217     0x0004
3252 +#define ATH9K_PCI_WOW        0x0008
3253 +#define ATH9K_PCI_BT_ANT_DIV 0x0010
3254 +#define ATH9K_PCI_D3_L1_WAR  0x0020
3255  
3256  /*
3257   * Default cache line size, in bytes.
3258 --- a/drivers/net/wireless/ath/ath9k/debug.c
3259 +++ b/drivers/net/wireless/ath/ath9k/debug.c
3260 @@ -270,25 +270,29 @@ static const struct file_operations fops
3261         .llseek = default_llseek,
3262  };
3263  
3264 -static ssize_t read_file_ant_diversity(struct file *file, char __user *user_buf,
3265 -                                      size_t count, loff_t *ppos)
3266 +#ifdef CPTCFG_ATH9K_BTCOEX_SUPPORT
3267 +
3268 +static ssize_t read_file_bt_ant_diversity(struct file *file,
3269 +                                         char __user *user_buf,
3270 +                                         size_t count, loff_t *ppos)
3271  {
3272         struct ath_softc *sc = file->private_data;
3273         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
3274         char buf[32];
3275         unsigned int len;
3276  
3277 -       len = sprintf(buf, "%d\n", common->antenna_diversity);
3278 +       len = sprintf(buf, "%d\n", common->bt_ant_diversity);
3279         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
3280  }
3281  
3282 -static ssize_t write_file_ant_diversity(struct file *file,
3283 -                                       const char __user *user_buf,
3284 -                                       size_t count, loff_t *ppos)
3285 +static ssize_t write_file_bt_ant_diversity(struct file *file,
3286 +                                          const char __user *user_buf,
3287 +                                          size_t count, loff_t *ppos)
3288  {
3289         struct ath_softc *sc = file->private_data;
3290         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
3291 -       unsigned long antenna_diversity;
3292 +       struct ath9k_hw_capabilities *pCap = &sc->sc_ah->caps;
3293 +       unsigned long bt_ant_diversity;
3294         char buf[32];
3295         ssize_t len;
3296  
3297 @@ -296,26 +300,147 @@ static ssize_t write_file_ant_diversity(
3298         if (copy_from_user(buf, user_buf, len))
3299                 return -EFAULT;
3300  
3301 -       if (!AR_SREV_9565(sc->sc_ah))
3302 +       if (!(pCap->hw_caps & ATH9K_HW_CAP_BT_ANT_DIV))
3303                 goto exit;
3304  
3305         buf[len] = '\0';
3306 -       if (strict_strtoul(buf, 0, &antenna_diversity))
3307 +       if (kstrtoul(buf, 0, &bt_ant_diversity))
3308                 return -EINVAL;
3309  
3310 -       common->antenna_diversity = !!antenna_diversity;
3311 +       common->bt_ant_diversity = !!bt_ant_diversity;
3312         ath9k_ps_wakeup(sc);
3313 -       ath_ant_comb_update(sc);
3314 -       ath_dbg(common, CONFIG, "Antenna diversity: %d\n",
3315 -               common->antenna_diversity);
3316 +       ath9k_hw_set_bt_ant_diversity(sc->sc_ah, common->bt_ant_diversity);
3317 +       ath_dbg(common, CONFIG, "Enable WLAN/BT RX Antenna diversity: %d\n",
3318 +               common->bt_ant_diversity);
3319         ath9k_ps_restore(sc);
3320  exit:
3321         return count;
3322  }
3323  
3324 -static const struct file_operations fops_ant_diversity = {
3325 -       .read = read_file_ant_diversity,
3326 -       .write = write_file_ant_diversity,
3327 +static const struct file_operations fops_bt_ant_diversity = {
3328 +       .read = read_file_bt_ant_diversity,
3329 +       .write = write_file_bt_ant_diversity,
3330 +       .open = simple_open,
3331 +       .owner = THIS_MODULE,
3332 +       .llseek = default_llseek,
3333 +};
3334 +
3335 +#endif
3336 +
3337 +void ath9k_debug_stat_ant(struct ath_softc *sc,
3338 +                         struct ath_hw_antcomb_conf *div_ant_conf,
3339 +                         int main_rssi_avg, int alt_rssi_avg)
3340 +{
3341 +       struct ath_antenna_stats *as_main = &sc->debug.stats.ant_stats[ANT_MAIN];
3342 +       struct ath_antenna_stats *as_alt = &sc->debug.stats.ant_stats[ANT_ALT];
3343 +
3344 +       as_main->lna_attempt_cnt[div_ant_conf->main_lna_conf]++;
3345 +       as_alt->lna_attempt_cnt[div_ant_conf->alt_lna_conf]++;
3346 +
3347 +       as_main->rssi_avg = main_rssi_avg;
3348 +       as_alt->rssi_avg = alt_rssi_avg;
3349 +}
3350 +
3351 +static ssize_t read_file_antenna_diversity(struct file *file,
3352 +                                          char __user *user_buf,
3353 +                                          size_t count, loff_t *ppos)
3354 +{
3355 +       struct ath_softc *sc = file->private_data;
3356 +       struct ath_hw *ah = sc->sc_ah;
3357 +       struct ath9k_hw_capabilities *pCap = &ah->caps;
3358 +       struct ath_antenna_stats *as_main = &sc->debug.stats.ant_stats[ANT_MAIN];
3359 +       struct ath_antenna_stats *as_alt = &sc->debug.stats.ant_stats[ANT_ALT];
3360 +       struct ath_hw_antcomb_conf div_ant_conf;
3361 +       unsigned int len = 0, size = 1024;
3362 +       ssize_t retval = 0;
3363 +       char *buf;
3364 +       char *lna_conf_str[4] = {"LNA1_MINUS_LNA2",
3365 +                                "LNA2",
3366 +                                "LNA1",
3367 +                                "LNA1_PLUS_LNA2"};
3368 +
3369 +       buf = kzalloc(size, GFP_KERNEL);
3370 +       if (buf == NULL)
3371 +               return -ENOMEM;
3372 +
3373 +       if (!(pCap->hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB)) {
3374 +               len += snprintf(buf + len, size - len, "%s\n",
3375 +                               "Antenna Diversity Combining is disabled");
3376 +               goto exit;
3377 +       }
3378 +
3379 +       ath9k_ps_wakeup(sc);
3380 +       ath9k_hw_antdiv_comb_conf_get(ah, &div_ant_conf);
3381 +       len += snprintf(buf + len, size - len, "Current MAIN config : %s\n",
3382 +                       lna_conf_str[div_ant_conf.main_lna_conf]);
3383 +       len += snprintf(buf + len, size - len, "Current ALT config  : %s\n",
3384 +                       lna_conf_str[div_ant_conf.alt_lna_conf]);
3385 +       len += snprintf(buf + len, size - len, "Average MAIN RSSI   : %d\n",
3386 +                       as_main->rssi_avg);
3387 +       len += snprintf(buf + len, size - len, "Average ALT RSSI    : %d\n\n",
3388 +                       as_alt->rssi_avg);
3389 +       ath9k_ps_restore(sc);
3390 +
3391 +       len += snprintf(buf + len, size - len, "Packet Receive Cnt:\n");
3392 +       len += snprintf(buf + len, size - len, "-------------------\n");
3393 +
3394 +       len += snprintf(buf + len, size - len, "%30s%15s\n",
3395 +                       "MAIN", "ALT");
3396 +       len += snprintf(buf + len, size - len, "%-14s:%15d%15d\n",
3397 +                       "TOTAL COUNT",
3398 +                       as_main->recv_cnt,
3399 +                       as_alt->recv_cnt);
3400 +       len += snprintf(buf + len, size - len, "%-14s:%15d%15d\n",
3401 +                       "LNA1",
3402 +                       as_main->lna_recv_cnt[ATH_ANT_DIV_COMB_LNA1],
3403 +                       as_alt->lna_recv_cnt[ATH_ANT_DIV_COMB_LNA1]);
3404 +       len += snprintf(buf + len, size - len, "%-14s:%15d%15d\n",
3405 +                       "LNA2",
3406 +                       as_main->lna_recv_cnt[ATH_ANT_DIV_COMB_LNA2],
3407 +                       as_alt->lna_recv_cnt[ATH_ANT_DIV_COMB_LNA2]);
3408 +       len += snprintf(buf + len, size - len, "%-14s:%15d%15d\n",
3409 +                       "LNA1 + LNA2",
3410 +                       as_main->lna_recv_cnt[ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2],
3411 +                       as_alt->lna_recv_cnt[ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2]);
3412 +       len += snprintf(buf + len, size - len, "%-14s:%15d%15d\n",
3413 +                       "LNA1 - LNA2",
3414 +                       as_main->lna_recv_cnt[ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2],
3415 +                       as_alt->lna_recv_cnt[ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2]);
3416 +
3417 +       len += snprintf(buf + len, size - len, "\nLNA Config Attempts:\n");
3418 +       len += snprintf(buf + len, size - len, "--------------------\n");
3419 +
3420 +       len += snprintf(buf + len, size - len, "%30s%15s\n",
3421 +                       "MAIN", "ALT");
3422 +       len += snprintf(buf + len, size - len, "%-14s:%15d%15d\n",
3423 +                       "LNA1",
3424 +                       as_main->lna_attempt_cnt[ATH_ANT_DIV_COMB_LNA1],
3425 +                       as_alt->lna_attempt_cnt[ATH_ANT_DIV_COMB_LNA1]);
3426 +       len += snprintf(buf + len, size - len, "%-14s:%15d%15d\n",
3427 +                       "LNA2",
3428 +                       as_main->lna_attempt_cnt[ATH_ANT_DIV_COMB_LNA2],
3429 +                       as_alt->lna_attempt_cnt[ATH_ANT_DIV_COMB_LNA2]);
3430 +       len += snprintf(buf + len, size - len, "%-14s:%15d%15d\n",
3431 +                       "LNA1 + LNA2",
3432 +                       as_main->lna_attempt_cnt[ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2],
3433 +                       as_alt->lna_attempt_cnt[ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2]);
3434 +       len += snprintf(buf + len, size - len, "%-14s:%15d%15d\n",
3435 +                       "LNA1 - LNA2",
3436 +                       as_main->lna_attempt_cnt[ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2],
3437 +                       as_alt->lna_attempt_cnt[ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2]);
3438 +
3439 +exit:
3440 +       if (len > size)
3441 +               len = size;
3442 +
3443 +       retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
3444 +       kfree(buf);
3445 +
3446 +       return retval;
3447 +}
3448 +
3449 +static const struct file_operations fops_antenna_diversity = {
3450 +       .read = read_file_antenna_diversity,
3451         .open = simple_open,
3452         .owner = THIS_MODULE,
3453         .llseek = default_llseek,
3454 @@ -607,6 +732,28 @@ static ssize_t read_file_xmit(struct fil
3455         return retval;
3456  }
3457  
3458 +static ssize_t print_queue(struct ath_softc *sc, struct ath_txq *txq,
3459 +                          char *buf, ssize_t size)
3460 +{
3461 +       ssize_t len = 0;
3462 +
3463 +       ath_txq_lock(sc, txq);
3464 +
3465 +       len += snprintf(buf + len, size - len, "%s: %d ",
3466 +                       "qnum", txq->axq_qnum);
3467 +       len += snprintf(buf + len, size - len, "%s: %2d ",
3468 +                       "qdepth", txq->axq_depth);
3469 +       len += snprintf(buf + len, size - len, "%s: %2d ",
3470 +                       "ampdu-depth", txq->axq_ampdu_depth);
3471 +       len += snprintf(buf + len, size - len, "%s: %3d ",
3472 +                       "pending", txq->pending_frames);
3473 +       len += snprintf(buf + len, size - len, "%s: %d\n",
3474 +                       "stopped", txq->stopped);
3475 +
3476 +       ath_txq_unlock(sc, txq);
3477 +       return len;
3478 +}
3479 +
3480  static ssize_t read_file_queues(struct file *file, char __user *user_buf,
3481                                 size_t count, loff_t *ppos)
3482  {
3483 @@ -624,24 +771,13 @@ static ssize_t read_file_queues(struct f
3484  
3485         for (i = 0; i < IEEE80211_NUM_ACS; i++) {
3486                 txq = sc->tx.txq_map[i];
3487 -               len += snprintf(buf + len, size - len, "(%s): ", qname[i]);
3488 -
3489 -               ath_txq_lock(sc, txq);
3490 -
3491 -               len += snprintf(buf + len, size - len, "%s: %d ",
3492 -                               "qnum", txq->axq_qnum);
3493 -               len += snprintf(buf + len, size - len, "%s: %2d ",
3494 -                               "qdepth", txq->axq_depth);
3495 -               len += snprintf(buf + len, size - len, "%s: %2d ",
3496 -                               "ampdu-depth", txq->axq_ampdu_depth);
3497 -               len += snprintf(buf + len, size - len, "%s: %3d ",
3498 -                               "pending", txq->pending_frames);
3499 -               len += snprintf(buf + len, size - len, "%s: %d\n",
3500 -                               "stopped", txq->stopped);
3501 -
3502 -               ath_txq_unlock(sc, txq);
3503 +               len += snprintf(buf + len, size - len, "(%s):  ", qname[i]);
3504 +               len += print_queue(sc, txq, buf + len, size - len);
3505         }
3506  
3507 +       len += snprintf(buf + len, size - len, "(CAB): ");
3508 +       len += print_queue(sc, sc->beacon.cabq, buf + len, size - len);
3509 +
3510         if (len > size)
3511                 len = size;
3512  
3513 @@ -1818,9 +1954,11 @@ int ath9k_init_debug(struct ath_hw *ah)
3514                            sc->debug.debugfs_phy, &sc->sc_ah->gpio_mask);
3515         debugfs_create_u32("gpio_val", S_IRUSR | S_IWUSR,
3516                            sc->debug.debugfs_phy, &sc->sc_ah->gpio_val);
3517 -       debugfs_create_file("diversity", S_IRUSR | S_IWUSR,
3518 -                           sc->debug.debugfs_phy, sc, &fops_ant_diversity);
3519 +       debugfs_create_file("antenna_diversity", S_IRUSR,
3520 +                           sc->debug.debugfs_phy, sc, &fops_antenna_diversity);
3521  #ifdef CPTCFG_ATH9K_BTCOEX_SUPPORT
3522 +       debugfs_create_file("bt_ant_diversity", S_IRUSR | S_IWUSR,
3523 +                           sc->debug.debugfs_phy, sc, &fops_bt_ant_diversity);
3524         debugfs_create_file("btcoex", S_IRUSR, sc->debug.debugfs_phy, sc,
3525                             &fops_btcoex);
3526  #endif
3527 --- a/net/mac80211/ibss.c
3528 +++ b/net/mac80211/ibss.c
3529 @@ -30,13 +30,14 @@
3530  
3531  #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
3532  #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
3533 +#define IEEE80211_IBSS_RSN_INACTIVITY_LIMIT (10 * HZ)
3534  
3535  #define IEEE80211_IBSS_MAX_STA_ENTRIES 128
3536  
3537  
3538  static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
3539                                       const u8 *bssid, const int beacon_int,
3540 -                                     struct ieee80211_channel *chan,
3541 +                                     struct cfg80211_chan_def *req_chandef,
3542                                       const u32 basic_rates,
3543                                       const u16 capability, u64 tsf,
3544                                       bool creator)
3545 @@ -51,6 +52,7 @@ static void __ieee80211_sta_join_ibss(st
3546         u32 bss_change;
3547         u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
3548         struct cfg80211_chan_def chandef;
3549 +       struct ieee80211_channel *chan;
3550         struct beacon_data *presp;
3551         int frame_len;
3552  
3553 @@ -81,7 +83,9 @@ static void __ieee80211_sta_join_ibss(st
3554  
3555         sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
3556  
3557 -       chandef = ifibss->chandef;
3558 +       /* make a copy of the chandef, it could be modified below. */
3559 +       chandef = *req_chandef;
3560 +       chan = chandef.chan;
3561         if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chandef)) {
3562                 chandef.width = NL80211_CHAN_WIDTH_20;
3563                 chandef.center_freq1 = chan->center_freq;
3564 @@ -259,10 +263,12 @@ static void ieee80211_sta_join_ibss(stru
3565         struct cfg80211_bss *cbss =
3566                 container_of((void *)bss, struct cfg80211_bss, priv);
3567         struct ieee80211_supported_band *sband;
3568 +       struct cfg80211_chan_def chandef;
3569         u32 basic_rates;
3570         int i, j;
3571         u16 beacon_int = cbss->beacon_interval;
3572         const struct cfg80211_bss_ies *ies;
3573 +       enum nl80211_channel_type chan_type;
3574         u64 tsf;
3575  
3576         sdata_assert_lock(sdata);
3577 @@ -270,6 +276,26 @@ static void ieee80211_sta_join_ibss(stru
3578         if (beacon_int < 10)
3579                 beacon_int = 10;
3580  
3581 +       switch (sdata->u.ibss.chandef.width) {
3582 +       case NL80211_CHAN_WIDTH_20_NOHT:
3583 +       case NL80211_CHAN_WIDTH_20:
3584 +       case NL80211_CHAN_WIDTH_40:
3585 +               chan_type = cfg80211_get_chandef_type(&sdata->u.ibss.chandef);
3586 +               cfg80211_chandef_create(&chandef, cbss->channel, chan_type);
3587 +               break;
3588 +       case NL80211_CHAN_WIDTH_5:
3589 +       case NL80211_CHAN_WIDTH_10:
3590 +               cfg80211_chandef_create(&chandef, cbss->channel,
3591 +                                       NL80211_CHAN_WIDTH_20_NOHT);
3592 +               chandef.width = sdata->u.ibss.chandef.width;
3593 +               break;
3594 +       default:
3595 +               /* fall back to 20 MHz for unsupported modes */
3596 +               cfg80211_chandef_create(&chandef, cbss->channel,
3597 +                                       NL80211_CHAN_WIDTH_20_NOHT);
3598 +               break;
3599 +       }
3600 +
3601         sband = sdata->local->hw.wiphy->bands[cbss->channel->band];
3602  
3603         basic_rates = 0;
3604 @@ -294,7 +320,7 @@ static void ieee80211_sta_join_ibss(stru
3605  
3606         __ieee80211_sta_join_ibss(sdata, cbss->bssid,
3607                                   beacon_int,
3608 -                                 cbss->channel,
3609 +                                 &chandef,
3610                                   basic_rates,
3611                                   cbss->capability,
3612                                   tsf, false);
3613 @@ -672,6 +698,33 @@ static int ieee80211_sta_active_ibss(str
3614         return active;
3615  }
3616  
3617 +static void ieee80211_ibss_sta_expire(struct ieee80211_sub_if_data *sdata)
3618 +{
3619 +       struct ieee80211_local *local = sdata->local;
3620 +       struct sta_info *sta, *tmp;
3621 +       unsigned long exp_time = IEEE80211_IBSS_INACTIVITY_LIMIT;
3622 +       unsigned long exp_rsn_time = IEEE80211_IBSS_RSN_INACTIVITY_LIMIT;
3623 +
3624 +       mutex_lock(&local->sta_mtx);
3625 +
3626 +       list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
3627 +               if (sdata != sta->sdata)
3628 +                       continue;
3629 +
3630 +               if (time_after(jiffies, sta->last_rx + exp_time) ||
3631 +                   (time_after(jiffies, sta->last_rx + exp_rsn_time) &&
3632 +                    sta->sta_state != IEEE80211_STA_AUTHORIZED)) {
3633 +                       sta_dbg(sta->sdata, "expiring inactive %sSTA %pM\n",
3634 +                               sta->sta_state != IEEE80211_STA_AUTHORIZED ?
3635 +                               "not authorized " : "", sta->sta.addr);
3636 +
3637 +                       WARN_ON(__sta_info_destroy(sta));
3638 +               }
3639 +       }
3640 +
3641 +       mutex_unlock(&local->sta_mtx);
3642 +}
3643 +
3644  /*
3645   * This function is called with state == IEEE80211_IBSS_MLME_JOINED
3646   */
3647 @@ -685,7 +738,7 @@ static void ieee80211_sta_merge_ibss(str
3648         mod_timer(&ifibss->timer,
3649                   round_jiffies(jiffies + IEEE80211_IBSS_MERGE_INTERVAL));
3650  
3651 -       ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
3652 +       ieee80211_ibss_sta_expire(sdata);
3653  
3654         if (time_before(jiffies, ifibss->last_scan_completed +
3655                        IEEE80211_IBSS_MERGE_INTERVAL))
3656 @@ -736,7 +789,7 @@ static void ieee80211_sta_create_ibss(st
3657                 sdata->drop_unencrypted = 0;
3658  
3659         __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
3660 -                                 ifibss->chandef.chan, ifibss->basic_rates,
3661 +                                 &ifibss->chandef, ifibss->basic_rates,
3662                                   capability, 0, true);
3663  }
3664  
3665 @@ -792,6 +845,17 @@ static void ieee80211_sta_find_ibss(stru
3666                 return;
3667         }
3668  
3669 +       /* if a fixed bssid and a fixed freq have been provided create the IBSS
3670 +        * directly and do not waste time scanning
3671 +        */
3672 +       if (ifibss->fixed_bssid && ifibss->fixed_channel) {
3673 +               sdata_info(sdata, "Created IBSS using preconfigured BSSID %pM\n",
3674 +                          bssid);
3675 +               ieee80211_sta_create_ibss(sdata);
3676 +               return;
3677 +       }
3678 +
3679 +
3680         ibss_dbg(sdata, "sta_find_ibss: did not try to join ibss\n");
3681  
3682         /* Selected IBSS not found in current scan results - try to scan */
3683 @@ -1138,6 +1202,7 @@ int ieee80211_ibss_leave(struct ieee8021
3684         clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
3685         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
3686                                                 BSS_CHANGED_IBSS);
3687 +       ieee80211_vif_release_channel(sdata);
3688         synchronize_rcu();
3689         kfree(presp);
3690  
3691 --- a/drivers/net/wireless/ath/ath9k/ar9003_phy.c
3692 +++ b/drivers/net/wireless/ath/ath9k/ar9003_phy.c
3693 @@ -632,6 +632,22 @@ static void ar9003_hw_override_ini(struc
3694  
3695         REG_SET_BIT(ah, AR_PHY_CCK_DETECT,
3696                     AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV);
3697 +
3698 +       if (AR_SREV_9462(ah) || AR_SREV_9565(ah)) {
3699 +               REG_WRITE(ah, AR_GLB_SWREG_DISCONT_MODE,
3700 +                         AR_GLB_SWREG_DISCONT_EN_BT_WLAN);
3701 +
3702 +               if (REG_READ_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_0,
3703 +                                  AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL))
3704 +                       ah->enabled_cals |= TX_IQ_CAL;
3705 +               else
3706 +                       ah->enabled_cals &= ~TX_IQ_CAL;
3707 +
3708 +               if (REG_READ(ah, AR_PHY_CL_CAL_CTL) & AR_PHY_CL_CAL_ENABLE)
3709 +                       ah->enabled_cals |= TX_CL_CAL;
3710 +               else
3711 +                       ah->enabled_cals &= ~TX_CL_CAL;
3712 +       }
3713  }
3714  
3715  static void ar9003_hw_prog_ini(struct ath_hw *ah,
3716 @@ -814,29 +830,12 @@ static int ar9003_hw_process_ini(struct 
3717         if (chan->channel == 2484)
3718                 ar9003_hw_prog_ini(ah, &ah->iniCckfirJapan2484, 1);
3719  
3720 -       if (AR_SREV_9462(ah) || AR_SREV_9565(ah))
3721 -               REG_WRITE(ah, AR_GLB_SWREG_DISCONT_MODE,
3722 -                         AR_GLB_SWREG_DISCONT_EN_BT_WLAN);
3723 -
3724         ah->modes_index = modesIndex;
3725         ar9003_hw_override_ini(ah);
3726         ar9003_hw_set_channel_regs(ah, chan);
3727         ar9003_hw_set_chain_masks(ah, ah->rxchainmask, ah->txchainmask);
3728         ath9k_hw_apply_txpower(ah, chan, false);
3729  
3730 -       if (AR_SREV_9462(ah) || AR_SREV_9565(ah)) {
3731 -               if (REG_READ_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_0,
3732 -                                  AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL))
3733 -                       ah->enabled_cals |= TX_IQ_CAL;
3734 -               else
3735 -                       ah->enabled_cals &= ~TX_IQ_CAL;
3736 -
3737 -               if (REG_READ(ah, AR_PHY_CL_CAL_CTL) & AR_PHY_CL_CAL_ENABLE)
3738 -                       ah->enabled_cals |= TX_CL_CAL;
3739 -               else
3740 -                       ah->enabled_cals &= ~TX_CL_CAL;
3741 -       }
3742 -
3743         return 0;
3744  }
3745  
3746 @@ -1173,6 +1172,10 @@ skip_ws_det:
3747                  * is_on == 0 means MRC CCK is OFF (more noise imm)
3748                  */
3749                 bool is_on = param ? 1 : 0;
3750 +
3751 +               if (ah->caps.rx_chainmask == 1)
3752 +                       break;
3753 +
3754                 REG_RMW_FIELD(ah, AR_PHY_MRC_CCK_CTRL,
3755                               AR_PHY_MRC_CCK_ENABLE, is_on);
3756                 REG_RMW_FIELD(ah, AR_PHY_MRC_CCK_CTRL,
3757 @@ -1413,65 +1416,111 @@ static void ar9003_hw_antdiv_comb_conf_s
3758         REG_WRITE(ah, AR_PHY_MC_GAIN_CTRL, regval);
3759  }
3760  
3761 -static void ar9003_hw_antctrl_shared_chain_lnadiv(struct ath_hw *ah,
3762 -                                                 bool enable)
3763 +#ifdef CPTCFG_ATH9K_BTCOEX_SUPPORT
3764 +
3765 +static void ar9003_hw_set_bt_ant_diversity(struct ath_hw *ah, bool enable)
3766  {
3767 +       struct ath9k_hw_capabilities *pCap = &ah->caps;
3768         u8 ant_div_ctl1;
3769         u32 regval;
3770  
3771 -       if (!AR_SREV_9565(ah))
3772 +       if (!AR_SREV_9485(ah) && !AR_SREV_9565(ah))
3773                 return;
3774  
3775 -       ah->shared_chain_lnadiv = enable;
3776 +       if (AR_SREV_9485(ah)) {
3777 +               regval = ar9003_hw_ant_ctrl_common_2_get(ah,
3778 +                                                IS_CHAN_2GHZ(ah->curchan));
3779 +               if (enable) {
3780 +                       regval &= ~AR_SWITCH_TABLE_COM2_ALL;
3781 +                       regval |= ah->config.ant_ctrl_comm2g_switch_enable;
3782 +               }
3783 +               REG_RMW_FIELD(ah, AR_PHY_SWITCH_COM_2,
3784 +                             AR_SWITCH_TABLE_COM2_ALL, regval);
3785 +       }
3786 +
3787         ant_div_ctl1 = ah->eep_ops->get_eeprom(ah, EEP_ANT_DIV_CTL1);
3788  
3789 +       /*
3790 +        * Set MAIN/ALT LNA conf.
3791 +        * Set MAIN/ALT gain_tb.
3792 +        */
3793         regval = REG_READ(ah, AR_PHY_MC_GAIN_CTRL);
3794         regval &= (~AR_ANT_DIV_CTRL_ALL);
3795         regval |= (ant_div_ctl1 & 0x3f) << AR_ANT_DIV_CTRL_ALL_S;
3796 -       regval &= ~AR_PHY_ANT_DIV_LNADIV;
3797 -       regval |= ((ant_div_ctl1 >> 6) & 0x1) << AR_PHY_ANT_DIV_LNADIV_S;
3798 -
3799 -       if (enable)
3800 -               regval |= AR_ANT_DIV_ENABLE;
3801 -
3802         REG_WRITE(ah, AR_PHY_MC_GAIN_CTRL, regval);
3803  
3804 -       regval = REG_READ(ah, AR_PHY_CCK_DETECT);
3805 -       regval &= ~AR_FAST_DIV_ENABLE;
3806 -       regval |= ((ant_div_ctl1 >> 7) & 0x1) << AR_FAST_DIV_ENABLE_S;
3807 -
3808 -       if (enable)
3809 -               regval |= AR_FAST_DIV_ENABLE;
3810 -
3811 -       REG_WRITE(ah, AR_PHY_CCK_DETECT, regval);
3812 -
3813 -       if (enable) {
3814 -               REG_SET_BIT(ah, AR_PHY_MC_GAIN_CTRL,
3815 -                           (1 << AR_PHY_ANT_SW_RX_PROT_S));
3816 -               if (ah->curchan && IS_CHAN_2GHZ(ah->curchan))
3817 -                       REG_SET_BIT(ah, AR_PHY_RESTART,
3818 -                                   AR_PHY_RESTART_ENABLE_DIV_M2FLAG);
3819 -               REG_SET_BIT(ah, AR_BTCOEX_WL_LNADIV,
3820 -                           AR_BTCOEX_WL_LNADIV_FORCE_ON);
3821 -       } else {
3822 -               REG_CLR_BIT(ah, AR_PHY_MC_GAIN_CTRL, AR_ANT_DIV_ENABLE);
3823 -               REG_CLR_BIT(ah, AR_PHY_MC_GAIN_CTRL,
3824 -                           (1 << AR_PHY_ANT_SW_RX_PROT_S));
3825 -               REG_CLR_BIT(ah, AR_PHY_CCK_DETECT, AR_FAST_DIV_ENABLE);
3826 -               REG_CLR_BIT(ah, AR_BTCOEX_WL_LNADIV,
3827 -                           AR_BTCOEX_WL_LNADIV_FORCE_ON);
3828 -
3829 +       if (AR_SREV_9485_11_OR_LATER(ah)) {
3830 +               /*
3831 +                * Enable LNA diversity.
3832 +                */
3833                 regval = REG_READ(ah, AR_PHY_MC_GAIN_CTRL);
3834 -               regval &= ~(AR_PHY_ANT_DIV_MAIN_LNACONF |
3835 -                       AR_PHY_ANT_DIV_ALT_LNACONF |
3836 -                       AR_PHY_ANT_DIV_MAIN_GAINTB |
3837 -                       AR_PHY_ANT_DIV_ALT_GAINTB);
3838 -               regval |= (AR_PHY_ANT_DIV_LNA1 << AR_PHY_ANT_DIV_MAIN_LNACONF_S);
3839 -               regval |= (AR_PHY_ANT_DIV_LNA2 << AR_PHY_ANT_DIV_ALT_LNACONF_S);
3840 +               regval &= ~AR_PHY_ANT_DIV_LNADIV;
3841 +               regval |= ((ant_div_ctl1 >> 6) & 0x1) << AR_PHY_ANT_DIV_LNADIV_S;
3842 +               if (enable)
3843 +                       regval |= AR_ANT_DIV_ENABLE;
3844 +
3845                 REG_WRITE(ah, AR_PHY_MC_GAIN_CTRL, regval);
3846 +
3847 +               /*
3848 +                * Enable fast antenna diversity.
3849 +                */
3850 +               regval = REG_READ(ah, AR_PHY_CCK_DETECT);
3851 +               regval &= ~AR_FAST_DIV_ENABLE;
3852 +               regval |= ((ant_div_ctl1 >> 7) & 0x1) << AR_FAST_DIV_ENABLE_S;
3853 +               if (enable)
3854 +                       regval |= AR_FAST_DIV_ENABLE;
3855 +
3856 +               REG_WRITE(ah, AR_PHY_CCK_DETECT, regval);
3857 +
3858 +               if (pCap->hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) {
3859 +                       regval = REG_READ(ah, AR_PHY_MC_GAIN_CTRL);
3860 +                       regval &= (~(AR_PHY_ANT_DIV_MAIN_LNACONF |
3861 +                                    AR_PHY_ANT_DIV_ALT_LNACONF |
3862 +                                    AR_PHY_ANT_DIV_ALT_GAINTB |
3863 +                                    AR_PHY_ANT_DIV_MAIN_GAINTB));
3864 +                       /*
3865 +                        * Set MAIN to LNA1 and ALT to LNA2 at the
3866 +                        * beginning.
3867 +                        */
3868 +                       regval |= (ATH_ANT_DIV_COMB_LNA1 <<
3869 +                                  AR_PHY_ANT_DIV_MAIN_LNACONF_S);
3870 +                       regval |= (ATH_ANT_DIV_COMB_LNA2 <<
3871 +                                  AR_PHY_ANT_DIV_ALT_LNACONF_S);
3872 +                       REG_WRITE(ah, AR_PHY_MC_GAIN_CTRL, regval);
3873 +               }
3874 +       } else if (AR_SREV_9565(ah)) {
3875 +               if (enable) {
3876 +                       REG_SET_BIT(ah, AR_PHY_MC_GAIN_CTRL,
3877 +                                   (1 << AR_PHY_ANT_SW_RX_PROT_S));
3878 +                       if (ah->curchan && IS_CHAN_2GHZ(ah->curchan))
3879 +                               REG_SET_BIT(ah, AR_PHY_RESTART,
3880 +                                           AR_PHY_RESTART_ENABLE_DIV_M2FLAG);
3881 +                       REG_SET_BIT(ah, AR_BTCOEX_WL_LNADIV,
3882 +                                   AR_BTCOEX_WL_LNADIV_FORCE_ON);
3883 +               } else {
3884 +                       REG_CLR_BIT(ah, AR_PHY_MC_GAIN_CTRL, AR_ANT_DIV_ENABLE);
3885 +                       REG_CLR_BIT(ah, AR_PHY_MC_GAIN_CTRL,
3886 +                                   (1 << AR_PHY_ANT_SW_RX_PROT_S));
3887 +                       REG_CLR_BIT(ah, AR_PHY_CCK_DETECT, AR_FAST_DIV_ENABLE);
3888 +                       REG_CLR_BIT(ah, AR_BTCOEX_WL_LNADIV,
3889 +                                   AR_BTCOEX_WL_LNADIV_FORCE_ON);
3890 +
3891 +                       regval = REG_READ(ah, AR_PHY_MC_GAIN_CTRL);
3892 +                       regval &= ~(AR_PHY_ANT_DIV_MAIN_LNACONF |
3893 +                                   AR_PHY_ANT_DIV_ALT_LNACONF |
3894 +                                   AR_PHY_ANT_DIV_MAIN_GAINTB |
3895 +                                   AR_PHY_ANT_DIV_ALT_GAINTB);
3896 +                       regval |= (ATH_ANT_DIV_COMB_LNA1 <<
3897 +                                  AR_PHY_ANT_DIV_MAIN_LNACONF_S);
3898 +                       regval |= (ATH_ANT_DIV_COMB_LNA2 <<
3899 +                                  AR_PHY_ANT_DIV_ALT_LNACONF_S);
3900 +                       REG_WRITE(ah, AR_PHY_MC_GAIN_CTRL, regval);
3901 +               }
3902         }
3903  }
3904  
3905 +#endif
3906 +
3907  static int ar9003_hw_fast_chan_change(struct ath_hw *ah,
3908                                       struct ath9k_channel *chan,
3909                                       u8 *ini_reloaded)
3910 @@ -1518,6 +1567,18 @@ static int ar9003_hw_fast_chan_change(st
3911  
3912         REG_WRITE_ARRAY(&ah->iniModesTxGain, modesIndex, regWrites);
3913  
3914 +       if (AR_SREV_9462_20_OR_LATER(ah)) {
3915 +               /*
3916 +                * CUS217 mix LNA mode.
3917 +                */
3918 +               if (ar9003_hw_get_rx_gain_idx(ah) == 2) {
3919 +                       REG_WRITE_ARRAY(&ah->ini_modes_rxgain_bb_core,
3920 +                                       1, regWrites);
3921 +                       REG_WRITE_ARRAY(&ah->ini_modes_rxgain_bb_postamble,
3922 +                                       modesIndex, regWrites);
3923 +               }
3924 +       }
3925 +
3926         /*
3927          * For 5GHz channels requiring Fast Clock, apply
3928          * different modal values.
3929 @@ -1528,7 +1589,11 @@ static int ar9003_hw_fast_chan_change(st
3930         if (AR_SREV_9565(ah))
3931                 REG_WRITE_ARRAY(&ah->iniModesFastClock, 1, regWrites);
3932  
3933 -       REG_WRITE_ARRAY(&ah->iniAdditional, 1, regWrites);
3934 +       /*
3935 +        * JAPAN regulatory.
3936 +        */
3937 +       if (chan->channel == 2484)
3938 +               ar9003_hw_prog_ini(ah, &ah->iniCckfirJapan2484, 1);
3939  
3940         ah->modes_index = modesIndex;
3941         *ini_reloaded = true;
3942 @@ -1631,11 +1696,14 @@ void ar9003_hw_attach_phy_ops(struct ath
3943  
3944         ops->antdiv_comb_conf_get = ar9003_hw_antdiv_comb_conf_get;
3945         ops->antdiv_comb_conf_set = ar9003_hw_antdiv_comb_conf_set;
3946 -       ops->antctrl_shared_chain_lnadiv = ar9003_hw_antctrl_shared_chain_lnadiv;
3947         ops->spectral_scan_config = ar9003_hw_spectral_scan_config;
3948         ops->spectral_scan_trigger = ar9003_hw_spectral_scan_trigger;
3949         ops->spectral_scan_wait = ar9003_hw_spectral_scan_wait;
3950  
3951 +#ifdef CPTCFG_ATH9K_BTCOEX_SUPPORT
3952 +       ops->set_bt_ant_diversity = ar9003_hw_set_bt_ant_diversity;
3953 +#endif
3954 +
3955         ar9003_hw_set_nf_limits(ah);
3956         ar9003_hw_set_radar_conf(ah);
3957         memcpy(ah->nf_regs, ar9300_cca_regs, sizeof(ah->nf_regs));
3958 --- a/drivers/net/wireless/ath/ath9k/recv.c
3959 +++ b/drivers/net/wireless/ath/ath9k/recv.c
3960 @@ -42,8 +42,6 @@ static void ath_rx_buf_link(struct ath_s
3961         struct ath_desc *ds;
3962         struct sk_buff *skb;
3963  
3964 -       ATH_RXBUF_RESET(bf);
3965 -
3966         ds = bf->bf_desc;
3967         ds->ds_link = 0; /* link to null */
3968         ds->ds_data = bf->bf_buf_addr;
3969 @@ -70,6 +68,14 @@ static void ath_rx_buf_link(struct ath_s
3970         sc->rx.rxlink = &ds->ds_link;
3971  }
3972  
3973 +static void ath_rx_buf_relink(struct ath_softc *sc, struct ath_buf *bf)
3974 +{
3975 +       if (sc->rx.buf_hold)
3976 +               ath_rx_buf_link(sc, sc->rx.buf_hold);
3977 +
3978 +       sc->rx.buf_hold = bf;
3979 +}
3980 +
3981  static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
3982  {
3983         /* XXX block beacon interrupts */
3984 @@ -117,7 +123,6 @@ static bool ath_rx_edma_buf_link(struct 
3985  
3986         skb = bf->bf_mpdu;
3987  
3988 -       ATH_RXBUF_RESET(bf);
3989         memset(skb->data, 0, ah->caps.rx_status_len);
3990         dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
3991                                 ah->caps.rx_status_len, DMA_TO_DEVICE);
3992 @@ -185,7 +190,7 @@ static void ath_rx_edma_cleanup(struct a
3993  
3994  static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
3995  {
3996 -       skb_queue_head_init(&rx_edma->rx_fifo);
3997 +       __skb_queue_head_init(&rx_edma->rx_fifo);
3998         rx_edma->rx_fifo_hwsize = size;
3999  }
4000  
4001 @@ -432,6 +437,7 @@ int ath_startrecv(struct ath_softc *sc)
4002         if (list_empty(&sc->rx.rxbuf))
4003                 goto start_recv;
4004  
4005 +       sc->rx.buf_hold = NULL;
4006         sc->rx.rxlink = NULL;
4007         list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
4008                 ath_rx_buf_link(sc, bf);
4009 @@ -677,6 +683,9 @@ static struct ath_buf *ath_get_next_rx_b
4010         }
4011  
4012         bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
4013 +       if (bf == sc->rx.buf_hold)
4014 +               return NULL;
4015 +
4016         ds = bf->bf_desc;
4017  
4018         /*
4019 @@ -755,7 +764,6 @@ static bool ath9k_rx_accept(struct ath_c
4020         bool is_mc, is_valid_tkip, strip_mic, mic_error;
4021         struct ath_hw *ah = common->ah;
4022         __le16 fc;
4023 -       u8 rx_status_len = ah->caps.rx_status_len;
4024  
4025         fc = hdr->frame_control;
4026  
4027 @@ -777,25 +785,6 @@ static bool ath9k_rx_accept(struct ath_c
4028             !test_bit(rx_stats->rs_keyix, common->ccmp_keymap))
4029                 rx_stats->rs_status &= ~ATH9K_RXERR_KEYMISS;
4030  
4031 -       if (!rx_stats->rs_datalen) {
4032 -               RX_STAT_INC(rx_len_err);
4033 -               return false;
4034 -       }
4035 -
4036 -        /*
4037 -         * rs_status follows rs_datalen so if rs_datalen is too large
4038 -         * we can take a hint that hardware corrupted it, so ignore
4039 -         * those frames.
4040 -         */
4041 -       if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len)) {
4042 -               RX_STAT_INC(rx_len_err);
4043 -               return false;
4044 -       }
4045 -
4046 -       /* Only use error bits from the last fragment */
4047 -       if (rx_stats->rs_more)
4048 -               return true;
4049 -
4050         mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
4051                 !ieee80211_has_morefrags(fc) &&
4052                 !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
4053 @@ -814,8 +803,6 @@ static bool ath9k_rx_accept(struct ath_c
4054                         rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
4055                         mic_error = false;
4056                 }
4057 -               if (rx_stats->rs_status & ATH9K_RXERR_PHY)
4058 -                       return false;
4059  
4060                 if ((rx_stats->rs_status & ATH9K_RXERR_DECRYPT) ||
4061                     (!is_mc && (rx_stats->rs_status & ATH9K_RXERR_KEYMISS))) {
4062 @@ -898,129 +885,65 @@ static int ath9k_process_rate(struct ath
4063  
4064  static void ath9k_process_rssi(struct ath_common *common,
4065                                struct ieee80211_hw *hw,
4066 -                              struct ieee80211_hdr *hdr,
4067 -                              struct ath_rx_status *rx_stats)
4068 +                              struct ath_rx_status *rx_stats,
4069 +                              struct ieee80211_rx_status *rxs)
4070  {
4071         struct ath_softc *sc = hw->priv;
4072         struct ath_hw *ah = common->ah;
4073         int last_rssi;
4074         int rssi = rx_stats->rs_rssi;
4075  
4076 -       if (!rx_stats->is_mybeacon ||
4077 -           ((ah->opmode != NL80211_IFTYPE_STATION) &&
4078 -            (ah->opmode != NL80211_IFTYPE_ADHOC)))
4079 +       /*
4080 +        * RSSI is not available for subframes in an A-MPDU.
4081 +        */
4082 +       if (rx_stats->rs_moreaggr) {
4083 +               rxs->flag |= RX_FLAG_NO_SIGNAL_VAL;
4084                 return;
4085 -
4086 -       if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
4087 -               ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
4088 -
4089 -       last_rssi = sc->last_rssi;
4090 -       if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
4091 -               rssi = ATH_EP_RND(last_rssi, ATH_RSSI_EP_MULTIPLIER);
4092 -       if (rssi < 0)
4093 -               rssi = 0;
4094 -
4095 -       /* Update Beacon RSSI, this is used by ANI. */
4096 -       ah->stats.avgbrssi = rssi;
4097 -}
4098 -
4099 -/*
4100 - * For Decrypt or Demic errors, we only mark packet status here and always push
4101 - * up the frame up to let mac80211 handle the actual error case, be it no
4102 - * decryption key or real decryption error. This let us keep statistics there.
4103 - */
4104 -static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
4105 -                                  struct ieee80211_hdr *hdr,
4106 -                                  struct ath_rx_status *rx_stats,
4107 -                                  struct ieee80211_rx_status *rx_status,
4108 -                                  bool *decrypt_error)
4109 -{
4110 -       struct ieee80211_hw *hw = sc->hw;
4111 -       struct ath_hw *ah = sc->sc_ah;
4112 -       struct ath_common *common = ath9k_hw_common(ah);
4113 -       bool discard_current = sc->rx.discard_next;
4114 -
4115 -       sc->rx.discard_next = rx_stats->rs_more;
4116 -       if (discard_current)
4117 -               return -EINVAL;
4118 +       }
4119  
4120         /*
4121 -        * everything but the rate is checked here, the rate check is done
4122 -        * separately to avoid doing two lookups for a rate for each frame.
4123 +        * Check if the RSSI for the last subframe in an A-MPDU
4124 +        * or an unaggregated frame is valid.
4125          */
4126 -       if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
4127 -               return -EINVAL;
4128 -
4129 -       /* Only use status info from the last fragment */
4130 -       if (rx_stats->rs_more)
4131 -               return 0;
4132 +       if (rx_stats->rs_rssi == ATH9K_RSSI_BAD) {
4133 +               rxs->flag |= RX_FLAG_NO_SIGNAL_VAL;
4134 +               return;
4135 +       }
4136  
4137 -       if (ath9k_process_rate(common, hw, rx_stats, rx_status))
4138 -               return -EINVAL;
4139 +       /*
4140 +        * Update Beacon RSSI, this is used by ANI.
4141 +        */
4142 +       if (rx_stats->is_mybeacon &&
4143 +           ((ah->opmode == NL80211_IFTYPE_STATION) ||
4144 +            (ah->opmode == NL80211_IFTYPE_ADHOC))) {
4145 +               ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
4146 +               last_rssi = sc->last_rssi;
4147  
4148 -       ath9k_process_rssi(common, hw, hdr, rx_stats);
4149 +               if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
4150 +                       rssi = ATH_EP_RND(last_rssi, ATH_RSSI_EP_MULTIPLIER);
4151 +               if (rssi < 0)
4152 +                       rssi = 0;
4153  
4154 -       rx_status->band = hw->conf.chandef.chan->band;
4155 -       rx_status->freq = hw->conf.chandef.chan->center_freq;
4156 -       rx_status->signal = ah->noise + rx_stats->rs_rssi;
4157 -       rx_status->antenna = rx_stats->rs_antenna;
4158 -       rx_status->flag |= RX_FLAG_MACTIME_END;
4159 -       if (rx_stats->rs_moreaggr)
4160 -               rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
4161 +               ah->stats.avgbrssi = rssi;
4162 +       }
4163  
4164 -       sc->rx.discard_next = false;
4165 -       return 0;
4166 +       rxs->signal = ah->noise + rx_stats->rs_rssi;
4167  }
4168  
4169 -static void ath9k_rx_skb_postprocess(struct ath_common *common,
4170 -                                    struct sk_buff *skb,
4171 -                                    struct ath_rx_status *rx_stats,
4172 -                                    struct ieee80211_rx_status *rxs,
4173 -                                    bool decrypt_error)
4174 +static void ath9k_process_tsf(struct ath_rx_status *rs,
4175 +                             struct ieee80211_rx_status *rxs,
4176 +                             u64 tsf)
4177  {
4178 -       struct ath_hw *ah = common->ah;
4179 -       struct ieee80211_hdr *hdr;
4180 -       int hdrlen, padpos, padsize;
4181 -       u8 keyix;
4182 -       __le16 fc;
4183 +       u32 tsf_lower = tsf & 0xffffffff;
4184  
4185 -       /* see if any padding is done by the hw and remove it */
4186 -       hdr = (struct ieee80211_hdr *) skb->data;
4187 -       hdrlen = ieee80211_get_hdrlen_from_skb(skb);
4188 -       fc = hdr->frame_control;
4189 -       padpos = ieee80211_hdrlen(fc);
4190 +       rxs->mactime = (tsf & ~0xffffffffULL) | rs->rs_tstamp;
4191 +       if (rs->rs_tstamp > tsf_lower &&
4192 +           unlikely(rs->rs_tstamp - tsf_lower > 0x10000000))
4193 +               rxs->mactime -= 0x100000000ULL;
4194  
4195 -       /* The MAC header is padded to have 32-bit boundary if the
4196 -        * packet payload is non-zero. The general calculation for
4197 -        * padsize would take into account odd header lengths:
4198 -        * padsize = (4 - padpos % 4) % 4; However, since only
4199 -        * even-length headers are used, padding can only be 0 or 2
4200 -        * bytes and we can optimize this a bit. In addition, we must
4201 -        * not try to remove padding from short control frames that do
4202 -        * not have payload. */
4203 -       padsize = padpos & 3;
4204 -       if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
4205 -               memmove(skb->data + padsize, skb->data, padpos);
4206 -               skb_pull(skb, padsize);
4207 -       }
4208 -
4209 -       keyix = rx_stats->rs_keyix;
4210 -
4211 -       if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
4212 -           ieee80211_has_protected(fc)) {
4213 -               rxs->flag |= RX_FLAG_DECRYPTED;
4214 -       } else if (ieee80211_has_protected(fc)
4215 -                  && !decrypt_error && skb->len >= hdrlen + 4) {
4216 -               keyix = skb->data[hdrlen + 3] >> 6;
4217 -
4218 -               if (test_bit(keyix, common->keymap))
4219 -                       rxs->flag |= RX_FLAG_DECRYPTED;
4220 -       }
4221 -       if (ah->sw_mgmt_crypto &&
4222 -           (rxs->flag & RX_FLAG_DECRYPTED) &&
4223 -           ieee80211_is_mgmt(fc))
4224 -               /* Use software decrypt for management frames. */
4225 -               rxs->flag &= ~RX_FLAG_DECRYPTED;
4226 +       if (rs->rs_tstamp < tsf_lower &&
4227 +           unlikely(tsf_lower - rs->rs_tstamp > 0x10000000))
4228 +               rxs->mactime += 0x100000000ULL;
4229  }
4230  
4231  #ifdef CPTCFG_ATH9K_DEBUGFS
4232 @@ -1133,6 +1056,234 @@ static int ath_process_fft(struct ath_so
4233  #endif
4234  }
4235  
4236 +static bool ath9k_is_mybeacon(struct ath_softc *sc, struct ieee80211_hdr *hdr)
4237 +{
4238 +       struct ath_hw *ah = sc->sc_ah;
4239 +       struct ath_common *common = ath9k_hw_common(ah);
4240 +
4241 +       if (ieee80211_is_beacon(hdr->frame_control)) {
4242 +               RX_STAT_INC(rx_beacons);
4243 +               if (!is_zero_ether_addr(common->curbssid) &&
4244 +                   ether_addr_equal(hdr->addr3, common->curbssid))
4245 +                       return true;
4246 +       }
4247 +
4248 +       return false;
4249 +}
4250 +
4251 +/*
4252 + * For Decrypt or Demic errors, we only mark packet status here and always push
4253 + * up the frame up to let mac80211 handle the actual error case, be it no
4254 + * decryption key or real decryption error. This let us keep statistics there.
4255 + */
4256 +static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
4257 +                                  struct sk_buff *skb,
4258 +                                  struct ath_rx_status *rx_stats,
4259 +                                  struct ieee80211_rx_status *rx_status,
4260 +                                  bool *decrypt_error, u64 tsf)
4261 +{
4262 +       struct ieee80211_hw *hw = sc->hw;
4263 +       struct ath_hw *ah = sc->sc_ah;
4264 +       struct ath_common *common = ath9k_hw_common(ah);
4265 +       struct ieee80211_hdr *hdr;
4266 +       bool discard_current = sc->rx.discard_next;
4267 +       int ret = 0;
4268 +
4269 +       /*
4270 +        * Discard corrupt descriptors which are marked in
4271 +        * ath_get_next_rx_buf().
4272 +        */
4273 +       sc->rx.discard_next = rx_stats->rs_more;
4274 +       if (discard_current)
4275 +               return -EINVAL;
4276 +
4277 +       /*
4278 +        * Discard zero-length packets.
4279 +        */
4280 +       if (!rx_stats->rs_datalen) {
4281 +               RX_STAT_INC(rx_len_err);
4282 +               return -EINVAL;
4283 +       }
4284 +
4285 +        /*
4286 +         * rs_status follows rs_datalen so if rs_datalen is too large
4287 +         * we can take a hint that hardware corrupted it, so ignore
4288 +         * those frames.
4289 +         */
4290 +       if (rx_stats->rs_datalen > (common->rx_bufsize - ah->caps.rx_status_len)) {
4291 +               RX_STAT_INC(rx_len_err);
4292 +               return -EINVAL;
4293 +       }
4294 +
4295 +       /* Only use status info from the last fragment */
4296 +       if (rx_stats->rs_more)
4297 +               return 0;
4298 +
4299 +       /*
4300 +        * Return immediately if the RX descriptor has been marked
4301 +        * as corrupt based on the various error bits.
4302 +        *
4303 +        * This is different from the other corrupt descriptor
4304 +        * condition handled above.
4305 +        */
4306 +       if (rx_stats->rs_status & ATH9K_RXERR_CORRUPT_DESC) {
4307 +               ret = -EINVAL;
4308 +               goto exit;
4309 +       }
4310 +
4311 +       hdr = (struct ieee80211_hdr *) (skb->data + ah->caps.rx_status_len);
4312 +
4313 +       ath9k_process_tsf(rx_stats, rx_status, tsf);
4314 +       ath_debug_stat_rx(sc, rx_stats);
4315 +
4316 +       /*
4317 +        * Process PHY errors and return so that the packet
4318 +        * can be dropped.
4319 +        */
4320 +       if (rx_stats->rs_status & ATH9K_RXERR_PHY) {
4321 +               ath9k_dfs_process_phyerr(sc, hdr, rx_stats, rx_status->mactime);
4322 +               if (ath_process_fft(sc, hdr, rx_stats, rx_status->mactime))
4323 +                       RX_STAT_INC(rx_spectral);
4324 +
4325 +               ret = -EINVAL;
4326 +               goto exit;
4327 +       }
4328 +
4329 +       /*
4330 +        * everything but the rate is checked here, the rate check is done
4331 +        * separately to avoid doing two lookups for a rate for each frame.
4332 +        */
4333 +       if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error)) {
4334 +               ret = -EINVAL;
4335 +               goto exit;
4336 +       }
4337 +
4338 +       rx_stats->is_mybeacon = ath9k_is_mybeacon(sc, hdr);
4339 +       if (rx_stats->is_mybeacon) {
4340 +               sc->hw_busy_count = 0;
4341 +               ath_start_rx_poll(sc, 3);
4342 +       }
4343 +
4344 +       if (ath9k_process_rate(common, hw, rx_stats, rx_status)) {
4345 +               ret =-EINVAL;
4346 +               goto exit;
4347 +       }
4348 +
4349 +       ath9k_process_rssi(common, hw, rx_stats, rx_status);
4350 +
4351 +       rx_status->band = hw->conf.chandef.chan->band;
4352 +       rx_status->freq = hw->conf.chandef.chan->center_freq;
4353 +       rx_status->antenna = rx_stats->rs_antenna;
4354 +       rx_status->flag |= RX_FLAG_MACTIME_END;
4355 +
4356 +#ifdef CPTCFG_ATH9K_BTCOEX_SUPPORT
4357 +       if (ieee80211_is_data_present(hdr->frame_control) &&
4358 +           !ieee80211_is_qos_nullfunc(hdr->frame_control))
4359 +               sc->rx.num_pkts++;
4360 +#endif
4361 +
4362 +exit:
4363 +       sc->rx.discard_next = false;
4364 +       return ret;
4365 +}
4366 +
4367 +static void ath9k_rx_skb_postprocess(struct ath_common *common,
4368 +                                    struct sk_buff *skb,
4369 +                                    struct ath_rx_status *rx_stats,
4370 +                                    struct ieee80211_rx_status *rxs,
4371 +                                    bool decrypt_error)
4372 +{
4373 +       struct ath_hw *ah = common->ah;
4374 +       struct ieee80211_hdr *hdr;
4375 +       int hdrlen, padpos, padsize;
4376 +       u8 keyix;
4377 +       __le16 fc;
4378 +
4379 +       /* see if any padding is done by the hw and remove it */
4380 +       hdr = (struct ieee80211_hdr *) skb->data;
4381 +       hdrlen = ieee80211_get_hdrlen_from_skb(skb);
4382 +       fc = hdr->frame_control;
4383 +       padpos = ieee80211_hdrlen(fc);
4384 +
4385 +       /* The MAC header is padded to have 32-bit boundary if the
4386 +        * packet payload is non-zero. The general calculation for
4387 +        * padsize would take into account odd header lengths:
4388 +        * padsize = (4 - padpos % 4) % 4; However, since only
4389 +        * even-length headers are used, padding can only be 0 or 2
4390 +        * bytes and we can optimize this a bit. In addition, we must
4391 +        * not try to remove padding from short control frames that do
4392 +        * not have payload. */
4393 +       padsize = padpos & 3;
4394 +       if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
4395 +               memmove(skb->data + padsize, skb->data, padpos);
4396 +               skb_pull(skb, padsize);
4397 +       }
4398 +
4399 +       keyix = rx_stats->rs_keyix;
4400 +
4401 +       if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
4402 +           ieee80211_has_protected(fc)) {
4403 +               rxs->flag |= RX_FLAG_DECRYPTED;
4404 +       } else if (ieee80211_has_protected(fc)
4405 +                  && !decrypt_error && skb->len >= hdrlen + 4) {
4406 +               keyix = skb->data[hdrlen + 3] >> 6;
4407 +
4408 +               if (test_bit(keyix, common->keymap))
4409 +                       rxs->flag |= RX_FLAG_DECRYPTED;
4410 +       }
4411 +       if (ah->sw_mgmt_crypto &&
4412 +           (rxs->flag & RX_FLAG_DECRYPTED) &&
4413 +           ieee80211_is_mgmt(fc))
4414 +               /* Use software decrypt for management frames. */
4415 +               rxs->flag &= ~RX_FLAG_DECRYPTED;
4416 +}
4417 +
4418 +/*
4419 + * Run the LNA combining algorithm only in these cases:
4420 + *
4421 + * Standalone WLAN cards with both LNA/Antenna diversity
4422 + * enabled in the EEPROM.
4423 + *
4424 + * WLAN+BT cards which are in the supported card list
4425 + * in ath_pci_id_table and the user has loaded the
4426 + * driver with "bt_ant_diversity" set to true.
4427 + */
4428 +static void ath9k_antenna_check(struct ath_softc *sc,
4429 +                               struct ath_rx_status *rs)
4430 +{
4431 +       struct ath_hw *ah = sc->sc_ah;
4432 +       struct ath9k_hw_capabilities *pCap = &ah->caps;
4433 +       struct ath_common *common = ath9k_hw_common(ah);
4434 +
4435 +       if (!(ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB))
4436 +               return;
4437 +
4438 +       /*
4439 +        * All MPDUs in an aggregate will use the same LNA
4440 +        * as the first MPDU.
4441 +        */
4442 +       if (rs->rs_isaggr && !rs->rs_firstaggr)
4443 +               return;
4444 +
4445 +       /*
4446 +        * Change the default rx antenna if rx diversity
4447 +        * chooses the other antenna 3 times in a row.
4448 +        */
4449 +       if (sc->rx.defant != rs->rs_antenna) {
4450 +               if (++sc->rx.rxotherant >= 3)
4451 +                       ath_setdefantenna(sc, rs->rs_antenna);
4452 +       } else {
4453 +               sc->rx.rxotherant = 0;
4454 +       }
4455 +
4456 +       if (pCap->hw_caps & ATH9K_HW_CAP_BT_ANT_DIV) {
4457 +               if (common->bt_ant_diversity)
4458 +                       ath_ant_comb_scan(sc, rs);
4459 +       } else {
4460 +               ath_ant_comb_scan(sc, rs);
4461 +       }
4462 +}
4463 +
4464  static void ath9k_apply_ampdu_details(struct ath_softc *sc,
4465         struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
4466  {
4467 @@ -1159,15 +1310,12 @@ int ath_rx_tasklet(struct ath_softc *sc,
4468         struct ath_hw *ah = sc->sc_ah;
4469         struct ath_common *common = ath9k_hw_common(ah);
4470         struct ieee80211_hw *hw = sc->hw;
4471 -       struct ieee80211_hdr *hdr;
4472         int retval;
4473         struct ath_rx_status rs;
4474         enum ath9k_rx_qtype qtype;
4475         bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
4476         int dma_type;
4477 -       u8 rx_status_len = ah->caps.rx_status_len;
4478         u64 tsf = 0;
4479 -       u32 tsf_lower = 0;
4480         unsigned long flags;
4481         dma_addr_t new_buf_addr;
4482  
4483 @@ -1179,7 +1327,6 @@ int ath_rx_tasklet(struct ath_softc *sc,
4484         qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
4485  
4486         tsf = ath9k_hw_gettsf64(ah);
4487 -       tsf_lower = tsf & 0xffffffff;
4488  
4489         do {
4490                 bool decrypt_error = false;
4491 @@ -1206,55 +1353,14 @@ int ath_rx_tasklet(struct ath_softc *sc,
4492                 else
4493                         hdr_skb = skb;
4494  
4495 -               hdr = (struct ieee80211_hdr *) (hdr_skb->data + rx_status_len);
4496                 rxs = IEEE80211_SKB_RXCB(hdr_skb);
4497 -               if (ieee80211_is_beacon(hdr->frame_control)) {
4498 -                       RX_STAT_INC(rx_beacons);
4499 -                       if (!is_zero_ether_addr(common->curbssid) &&
4500 -                           ether_addr_equal(hdr->addr3, common->curbssid))
4501 -                               rs.is_mybeacon = true;
4502 -                       else
4503 -                               rs.is_mybeacon = false;
4504 -               }
4505 -               else
4506 -                       rs.is_mybeacon = false;
4507 -
4508 -               if (ieee80211_is_data_present(hdr->frame_control) &&
4509 -                   !ieee80211_is_qos_nullfunc(hdr->frame_control))
4510 -                       sc->rx.num_pkts++;
4511 -
4512 -               ath_debug_stat_rx(sc, &rs);
4513 -
4514                 memset(rxs, 0, sizeof(struct ieee80211_rx_status));
4515  
4516 -               rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
4517 -               if (rs.rs_tstamp > tsf_lower &&
4518 -                   unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
4519 -                       rxs->mactime -= 0x100000000ULL;
4520 -
4521 -               if (rs.rs_tstamp < tsf_lower &&
4522 -                   unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
4523 -                       rxs->mactime += 0x100000000ULL;
4524 -
4525 -               if (rs.rs_phyerr == ATH9K_PHYERR_RADAR)
4526 -                       ath9k_dfs_process_phyerr(sc, hdr, &rs, rxs->mactime);
4527 -
4528 -               if (rs.rs_status & ATH9K_RXERR_PHY) {
4529 -                       if (ath_process_fft(sc, hdr, &rs, rxs->mactime)) {
4530 -                               RX_STAT_INC(rx_spectral);
4531 -                               goto requeue_drop_frag;
4532 -                       }
4533 -               }
4534 -
4535 -               retval = ath9k_rx_skb_preprocess(sc, hdr, &rs, rxs,
4536 -                                                &decrypt_error);
4537 +               retval = ath9k_rx_skb_preprocess(sc, hdr_skb, &rs, rxs,
4538 +                                                &decrypt_error, tsf);
4539                 if (retval)
4540                         goto requeue_drop_frag;
4541  
4542 -               if (rs.is_mybeacon) {
4543 -                       sc->hw_busy_count = 0;
4544 -                       ath_start_rx_poll(sc, 3);
4545 -               }
4546                 /* Ensure we always have an skb to requeue once we are done
4547                  * processing the current buffer's skb */
4548                 requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
4549 @@ -1308,8 +1414,6 @@ int ath_rx_tasklet(struct ath_softc *sc,
4550                         sc->rx.frag = skb;
4551                         goto requeue;
4552                 }
4553 -               if (rs.rs_status & ATH9K_RXERR_CORRUPT_DESC)
4554 -                       goto requeue_drop_frag;
4555  
4556                 if (sc->rx.frag) {
4557                         int space = skb->len - skb_tailroom(hdr_skb);
4558 @@ -1328,22 +1432,6 @@ int ath_rx_tasklet(struct ath_softc *sc,
4559                         skb = hdr_skb;
4560                 }
4561  
4562 -
4563 -               if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) {
4564 -
4565 -                       /*
4566 -                        * change the default rx antenna if rx diversity
4567 -                        * chooses the other antenna 3 times in a row.
4568 -                        */
4569 -                       if (sc->rx.defant != rs.rs_antenna) {
4570 -                               if (++sc->rx.rxotherant >= 3)
4571 -                                       ath_setdefantenna(sc, rs.rs_antenna);
4572 -                       } else {
4573 -                               sc->rx.rxotherant = 0;
4574 -                       }
4575 -
4576 -               }
4577 -
4578                 if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
4579                         skb_trim(skb, skb->len - 8);
4580  
4581 @@ -1355,8 +1443,7 @@ int ath_rx_tasklet(struct ath_softc *sc,
4582                         ath_rx_ps(sc, skb, rs.is_mybeacon);
4583                 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
4584  
4585 -               if ((ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) && sc->ant_rx == 3)
4586 -                       ath_ant_comb_scan(sc, &rs);
4587 +               ath9k_antenna_check(sc, &rs);
4588  
4589                 ath9k_apply_ampdu_details(sc, &rs, rxs);
4590  
4591 @@ -1375,7 +1462,7 @@ requeue:
4592                 if (edma) {
4593                         ath_rx_edma_buf_link(sc, qtype);
4594                 } else {
4595 -                       ath_rx_buf_link(sc, bf);
4596 +                       ath_rx_buf_relink(sc, bf);
4597                         ath9k_hw_rxena(ah);
4598                 }
4599         } while (1);
4600 --- a/drivers/net/wireless/ath/ath9k/init.c
4601 +++ b/drivers/net/wireless/ath/ath9k/init.c
4602 @@ -53,9 +53,9 @@ static int ath9k_btcoex_enable;
4603  module_param_named(btcoex_enable, ath9k_btcoex_enable, int, 0444);
4604  MODULE_PARM_DESC(btcoex_enable, "Enable wifi-BT coexistence");
4605  
4606 -static int ath9k_enable_diversity;
4607 -module_param_named(enable_diversity, ath9k_enable_diversity, int, 0444);
4608 -MODULE_PARM_DESC(enable_diversity, "Enable Antenna diversity for AR9565");
4609 +static int ath9k_bt_ant_diversity;
4610 +module_param_named(bt_ant_diversity, ath9k_bt_ant_diversity, int, 0444);
4611 +MODULE_PARM_DESC(bt_ant_diversity, "Enable WLAN/BT RX antenna diversity");
4612  
4613  bool is_ath9k_unloaded;
4614  /* We use the hw_value as an index into our private channel structure */
4615 @@ -516,6 +516,7 @@ static void ath9k_init_misc(struct ath_s
4616  static void ath9k_init_platform(struct ath_softc *sc)
4617  {
4618         struct ath_hw *ah = sc->sc_ah;
4619 +       struct ath9k_hw_capabilities *pCap = &ah->caps;
4620         struct ath_common *common = ath9k_hw_common(ah);
4621  
4622         if (common->bus_ops->ath_bus_type != ATH_PCI)
4623 @@ -525,12 +526,27 @@ static void ath9k_init_platform(struct a
4624                                ATH9K_PCI_CUS230)) {
4625                 ah->config.xlna_gpio = 9;
4626                 ah->config.xatten_margin_cfg = true;
4627 +               ah->config.alt_mingainidx = true;
4628 +               ah->config.ant_ctrl_comm2g_switch_enable = 0x000BBB88;
4629 +               sc->ant_comb.low_rssi_thresh = 20;
4630 +               sc->ant_comb.fast_div_bias = 3;
4631  
4632                 ath_info(common, "Set parameters for %s\n",
4633                          (sc->driver_data & ATH9K_PCI_CUS198) ?
4634                          "CUS198" : "CUS230");
4635 -       } else if (sc->driver_data & ATH9K_PCI_CUS217) {
4636 +       }
4637 +
4638 +       if (sc->driver_data & ATH9K_PCI_CUS217)
4639                 ath_info(common, "CUS217 card detected\n");
4640 +
4641 +       if (sc->driver_data & ATH9K_PCI_BT_ANT_DIV) {
4642 +               pCap->hw_caps |= ATH9K_HW_CAP_BT_ANT_DIV;
4643 +               ath_info(common, "Set BT/WLAN RX diversity capability\n");
4644 +       }
4645 +
4646 +       if (sc->driver_data & ATH9K_PCI_D3_L1_WAR) {
4647 +               ah->config.pcie_waen = 0x0040473b;
4648 +               ath_info(common, "Enable WAR for ASPM D3/L1\n");
4649         }
4650  }
4651  
4652 @@ -584,6 +600,7 @@ static int ath9k_init_softc(u16 devid, s
4653  {
4654         struct ath9k_platform_data *pdata = sc->dev->platform_data;
4655         struct ath_hw *ah = NULL;
4656 +       struct ath9k_hw_capabilities *pCap;
4657         struct ath_common *common;
4658         int ret = 0, i;
4659         int csz = 0;
4660 @@ -600,6 +617,7 @@ static int ath9k_init_softc(u16 devid, s
4661         ah->reg_ops.rmw = ath9k_reg_rmw;
4662         atomic_set(&ah->intr_ref_cnt, -1);
4663         sc->sc_ah = ah;
4664 +       pCap = &ah->caps;
4665  
4666         sc->dfs_detector = dfs_pattern_detector_init(ah, NL80211_DFS_UNSET);
4667  
4668 @@ -631,11 +649,15 @@ static int ath9k_init_softc(u16 devid, s
4669         ath9k_init_platform(sc);
4670  
4671         /*
4672 -        * Enable Antenna diversity only when BTCOEX is disabled
4673 -        * and the user manually requests the feature.
4674 +        * Enable WLAN/BT RX Antenna diversity only when:
4675 +        *
4676 +        * - BTCOEX is disabled.
4677 +        * - the user manually requests the feature.
4678 +        * - the HW cap is set using the platform data.
4679          */
4680 -       if (!common->btcoex_enabled && ath9k_enable_diversity)
4681 -               common->antenna_diversity = 1;
4682 +       if (!common->btcoex_enabled && ath9k_bt_ant_diversity &&
4683 +           (pCap->hw_caps & ATH9K_HW_CAP_BT_ANT_DIV))
4684 +               common->bt_ant_diversity = 1;
4685  
4686         spin_lock_init(&common->cc_lock);
4687  
4688 @@ -710,13 +732,15 @@ static void ath9k_init_band_txpower(stru
4689         struct ieee80211_supported_band *sband;
4690         struct ieee80211_channel *chan;
4691         struct ath_hw *ah = sc->sc_ah;
4692 +       struct cfg80211_chan_def chandef;
4693         int i;
4694  
4695         sband = &sc->sbands[band];
4696         for (i = 0; i < sband->n_channels; i++) {
4697                 chan = &sband->channels[i];
4698                 ah->curchan = &ah->channels[chan->hw_value];
4699 -               ath9k_cmn_update_ichannel(ah->curchan, chan, NL80211_CHAN_HT20);
4700 +               cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_HT20);
4701 +               ath9k_cmn_update_ichannel(ah->curchan, &chandef);
4702                 ath9k_hw_set_txpowerlimit(ah, MAX_RATE_POWER, true);
4703         }
4704  }
4705 @@ -802,7 +826,8 @@ void ath9k_set_hw_capab(struct ath_softc
4706                 IEEE80211_HW_PS_NULLFUNC_STACK |
4707                 IEEE80211_HW_SPECTRUM_MGMT |
4708                 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
4709 -               IEEE80211_HW_SUPPORTS_RC_TABLE;
4710 +               IEEE80211_HW_SUPPORTS_RC_TABLE |
4711 +               IEEE80211_HW_SUPPORTS_HT_CCK_RATES;
4712  
4713         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
4714                 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
4715 --- a/drivers/net/wireless/ath/carl9170/main.c
4716 +++ b/drivers/net/wireless/ath/carl9170/main.c
4717 @@ -1878,7 +1878,8 @@ void *carl9170_alloc(size_t priv_size)
4718                      IEEE80211_HW_PS_NULLFUNC_STACK |
4719                      IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC |
4720                      IEEE80211_HW_SUPPORTS_RC_TABLE |
4721 -                    IEEE80211_HW_SIGNAL_DBM;
4722 +                    IEEE80211_HW_SIGNAL_DBM |
4723 +                    IEEE80211_HW_SUPPORTS_HT_CCK_RATES;
4724  
4725         if (!modparam_noht) {
4726                 /*
4727 --- a/drivers/net/wireless/rt2x00/rt2800lib.c
4728 +++ b/drivers/net/wireless/rt2x00/rt2800lib.c
4729 @@ -6133,7 +6133,8 @@ static int rt2800_probe_hw_mode(struct r
4730             IEEE80211_HW_SUPPORTS_PS |
4731             IEEE80211_HW_PS_NULLFUNC_STACK |
4732             IEEE80211_HW_AMPDU_AGGREGATION |
4733 -           IEEE80211_HW_REPORTS_TX_ACK_STATUS;
4734 +           IEEE80211_HW_REPORTS_TX_ACK_STATUS |
4735 +           IEEE80211_HW_SUPPORTS_HT_CCK_RATES;
4736  
4737         /*
4738          * Don't set IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING for USB devices
4739 --- a/include/net/mac80211.h
4740 +++ b/include/net/mac80211.h
4741 @@ -152,11 +152,14 @@ struct ieee80211_low_level_stats {
4742   * @IEEE80211_CHANCTX_CHANGE_WIDTH: The channel width changed
4743   * @IEEE80211_CHANCTX_CHANGE_RX_CHAINS: The number of RX chains changed
4744   * @IEEE80211_CHANCTX_CHANGE_RADAR: radar detection flag changed
4745 + * @IEEE80211_CHANCTX_CHANGE_CHANNEL: switched to another operating channel,
4746 + *     this is used only with channel switching with CSA
4747   */
4748  enum ieee80211_chanctx_change {
4749         IEEE80211_CHANCTX_CHANGE_WIDTH          = BIT(0),
4750         IEEE80211_CHANCTX_CHANGE_RX_CHAINS      = BIT(1),
4751         IEEE80211_CHANCTX_CHANGE_RADAR          = BIT(2),
4752 +       IEEE80211_CHANCTX_CHANGE_CHANNEL        = BIT(3),
4753  };
4754  
4755  /**
4756 @@ -1080,6 +1083,7 @@ enum ieee80211_vif_flags {
4757   * @addr: address of this interface
4758   * @p2p: indicates whether this AP or STA interface is a p2p
4759   *     interface, i.e. a GO or p2p-sta respectively
4760 + * @csa_active: marks whether a channel switch is going on
4761   * @driver_flags: flags/capabilities the driver has for this interface,
4762   *     these need to be set (or cleared) when the interface is added
4763   *     or, if supported by the driver, the interface type is changed
4764 @@ -1102,6 +1106,7 @@ struct ieee80211_vif {
4765         struct ieee80211_bss_conf bss_conf;
4766         u8 addr[ETH_ALEN];
4767         bool p2p;
4768 +       bool csa_active;
4769  
4770         u8 cab_queue;
4771         u8 hw_queue[IEEE80211_NUM_ACS];
4772 @@ -1499,6 +1504,7 @@ enum ieee80211_hw_flags {
4773         IEEE80211_HW_SUPPORTS_RC_TABLE                  = 1<<24,
4774         IEEE80211_HW_P2P_DEV_ADDR_FOR_INTF              = 1<<25,
4775         IEEE80211_HW_TIMING_BEACON_ONLY                 = 1<<26,
4776 +       IEEE80211_HW_SUPPORTS_HT_CCK_RATES              = 1<<27,
4777  };
4778  
4779  /**
4780 @@ -2633,6 +2639,16 @@ enum ieee80211_roc_type {
4781   * @ipv6_addr_change: IPv6 address assignment on the given interface changed.
4782   *     Currently, this is only called for managed or P2P client interfaces.
4783   *     This callback is optional; it must not sleep.
4784 + *
4785 + * @channel_switch_beacon: Starts a channel switch to a new channel.
4786 + *     Beacons are modified to include CSA or ECSA IEs before calling this
4787 + *     function. The corresponding count fields in these IEs must be
4788 + *     decremented, and when they reach zero the driver must call
4789 + *     ieee80211_csa_finish(). Drivers which use ieee80211_beacon_get()
4790 + *     get the csa counter decremented by mac80211, but must check if it is
4791 + *     zero using ieee80211_csa_is_complete() after the beacon has been
4792 + *     transmitted and then call ieee80211_csa_finish().
4793 + *
4794   */
4795  struct ieee80211_ops {
4796         void (*tx)(struct ieee80211_hw *hw,
4797 @@ -2830,6 +2846,9 @@ struct ieee80211_ops {
4798                                  struct ieee80211_vif *vif,
4799                                  struct inet6_dev *idev);
4800  #endif
4801 +       void (*channel_switch_beacon)(struct ieee80211_hw *hw,
4802 +                                     struct ieee80211_vif *vif,
4803 +                                     struct cfg80211_chan_def *chandef);
4804  };
4805  
4806  /**
4807 @@ -3325,6 +3344,25 @@ static inline struct sk_buff *ieee80211_
4808  }
4809  
4810  /**
4811 + * ieee80211_csa_finish - notify mac80211 about channel switch
4812 + * @vif: &struct ieee80211_vif pointer from the add_interface callback.
4813 + *
4814 + * After a channel switch announcement was scheduled and the counter in this
4815 + * announcement hit zero, this function must be called by the driver to
4816 + * notify mac80211 that the channel can be changed.
4817 + */
4818 +void ieee80211_csa_finish(struct ieee80211_vif *vif);
4819 +
4820 +/**
4821 + * ieee80211_csa_is_complete - find out if counters reached zero
4822 + * @vif: &struct ieee80211_vif pointer from the add_interface callback.
4823 + *
4824 + * This function returns whether the channel switch counters reached zero.
4825 + */
4826 +bool ieee80211_csa_is_complete(struct ieee80211_vif *vif);
4827 +
4828 +
4829 +/**
4830   * ieee80211_proberesp_get - retrieve a Probe Response template
4831   * @hw: pointer obtained from ieee80211_alloc_hw().
4832   * @vif: &struct ieee80211_vif pointer from the add_interface callback.
4833 --- a/net/mac80211/cfg.c
4834 +++ b/net/mac80211/cfg.c
4835 @@ -854,8 +854,8 @@ static int ieee80211_set_probe_resp(stru
4836         return 0;
4837  }
4838  
4839 -static int ieee80211_assign_beacon(struct ieee80211_sub_if_data *sdata,
4840 -                                  struct cfg80211_beacon_data *params)
4841 +int ieee80211_assign_beacon(struct ieee80211_sub_if_data *sdata,
4842 +                           struct cfg80211_beacon_data *params)
4843  {
4844         struct beacon_data *new, *old;
4845         int new_head_len, new_tail_len;
4846 @@ -1018,6 +1018,12 @@ static int ieee80211_change_beacon(struc
4847  
4848         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4849  
4850 +       /* don't allow changing the beacon while CSA is in place - offset
4851 +        * of channel switch counter may change
4852 +        */
4853 +       if (sdata->vif.csa_active)
4854 +               return -EBUSY;
4855 +
4856         old = rtnl_dereference(sdata->u.ap.beacon);
4857         if (!old)
4858                 return -ENOENT;
4859 @@ -1042,6 +1048,10 @@ static int ieee80211_stop_ap(struct wiph
4860                 return -ENOENT;
4861         old_probe_resp = rtnl_dereference(sdata->u.ap.probe_resp);
4862  
4863 +       /* abort any running channel switch */
4864 +       sdata->vif.csa_active = false;
4865 +       cancel_work_sync(&sdata->csa_finalize_work);
4866 +
4867         /* turn off carrier for this interface and dependent VLANs */
4868         list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
4869                 netif_carrier_off(vlan->dev);
4870 @@ -2784,6 +2794,178 @@ static int ieee80211_start_radar_detecti
4871         return 0;
4872  }
4873  
4874 +static struct cfg80211_beacon_data *
4875 +cfg80211_beacon_dup(struct cfg80211_beacon_data *beacon)
4876 +{
4877 +       struct cfg80211_beacon_data *new_beacon;
4878 +       u8 *pos;
4879 +       int len;
4880 +
4881 +       len = beacon->head_len + beacon->tail_len + beacon->beacon_ies_len +
4882 +             beacon->proberesp_ies_len + beacon->assocresp_ies_len +
4883 +             beacon->probe_resp_len;
4884 +
4885 +       new_beacon = kzalloc(sizeof(*new_beacon) + len, GFP_KERNEL);
4886 +       if (!new_beacon)
4887 +               return NULL;
4888 +
4889 +       pos = (u8 *)(new_beacon + 1);
4890 +       if (beacon->head_len) {
4891 +               new_beacon->head_len = beacon->head_len;
4892 +               new_beacon->head = pos;
4893 +               memcpy(pos, beacon->head, beacon->head_len);
4894 +               pos += beacon->head_len;
4895 +       }
4896 +       if (beacon->tail_len) {
4897 +               new_beacon->tail_len = beacon->tail_len;
4898 +               new_beacon->tail = pos;
4899 +               memcpy(pos, beacon->tail, beacon->tail_len);
4900 +               pos += beacon->tail_len;
4901 +       }
4902 +       if (beacon->beacon_ies_len) {
4903 +               new_beacon->beacon_ies_len = beacon->beacon_ies_len;
4904 +               new_beacon->beacon_ies = pos;
4905 +               memcpy(pos, beacon->beacon_ies, beacon->beacon_ies_len);
4906 +               pos += beacon->beacon_ies_len;
4907 +       }
4908 +       if (beacon->proberesp_ies_len) {
4909 +               new_beacon->proberesp_ies_len = beacon->proberesp_ies_len;
4910 +               new_beacon->proberesp_ies = pos;
4911 +               memcpy(pos, beacon->proberesp_ies, beacon->proberesp_ies_len);
4912 +               pos += beacon->proberesp_ies_len;
4913 +       }
4914 +       if (beacon->assocresp_ies_len) {
4915 +               new_beacon->assocresp_ies_len = beacon->assocresp_ies_len;
4916 +               new_beacon->assocresp_ies = pos;
4917 +               memcpy(pos, beacon->assocresp_ies, beacon->assocresp_ies_len);
4918 +               pos += beacon->assocresp_ies_len;
4919 +       }
4920 +       if (beacon->probe_resp_len) {
4921 +               new_beacon->probe_resp_len = beacon->probe_resp_len;
4922 +               beacon->probe_resp = pos;
4923 +               memcpy(pos, beacon->probe_resp, beacon->probe_resp_len);
4924 +               pos += beacon->probe_resp_len;
4925 +       }
4926 +
4927 +       return new_beacon;
4928 +}
4929 +
4930 +void ieee80211_csa_finalize_work(struct work_struct *work)
4931 +{
4932 +       struct ieee80211_sub_if_data *sdata =
4933 +               container_of(work, struct ieee80211_sub_if_data,
4934 +                            csa_finalize_work);
4935 +       struct ieee80211_local *local = sdata->local;
4936 +       int err, changed;
4937 +
4938 +       if (!ieee80211_sdata_running(sdata))
4939 +               return;
4940 +
4941 +       if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
4942 +               return;
4943 +
4944 +       sdata->radar_required = sdata->csa_radar_required;
4945 +       err = ieee80211_vif_change_channel(sdata, &local->csa_chandef,
4946 +                                          &changed);
4947 +       if (WARN_ON(err < 0))
4948 +               return;
4949 +
4950 +       err = ieee80211_assign_beacon(sdata, sdata->u.ap.next_beacon);
4951 +       if (err < 0)
4952 +               return;
4953 +
4954 +       changed |= err;
4955 +       kfree(sdata->u.ap.next_beacon);
4956 +       sdata->u.ap.next_beacon = NULL;
4957 +       sdata->vif.csa_active = false;
4958 +
4959 +       ieee80211_wake_queues_by_reason(&sdata->local->hw,
4960 +                                       IEEE80211_MAX_QUEUE_MAP,
4961 +                                       IEEE80211_QUEUE_STOP_REASON_CSA);
4962 +
4963 +       ieee80211_bss_info_change_notify(sdata, changed);
4964 +
4965 +       cfg80211_ch_switch_notify(sdata->dev, &local->csa_chandef);
4966 +}
4967 +
4968 +static int ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
4969 +                                   struct cfg80211_csa_settings *params)
4970 +{
4971 +       struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4972 +       struct ieee80211_local *local = sdata->local;
4973 +       struct ieee80211_chanctx_conf *chanctx_conf;
4974 +       struct ieee80211_chanctx *chanctx;
4975 +       int err, num_chanctx;
4976 +
4977 +       if (!list_empty(&local->roc_list) || local->scanning)
4978 +               return -EBUSY;
4979 +
4980 +       if (sdata->wdev.cac_started)
4981 +               return -EBUSY;
4982 +
4983 +       if (cfg80211_chandef_identical(&params->chandef,
4984 +                                      &sdata->vif.bss_conf.chandef))
4985 +               return -EINVAL;
4986 +
4987 +       rcu_read_lock();
4988 +       chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4989 +       if (!chanctx_conf) {
4990 +               rcu_read_unlock();
4991 +               return -EBUSY;
4992 +       }
4993 +
4994 +       /* don't handle for multi-VIF cases */
4995 +       chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
4996 +       if (chanctx->refcount > 1) {
4997 +               rcu_read_unlock();
4998 +               return -EBUSY;
4999 +       }
5000 +       num_chanctx = 0;
5001 +       list_for_each_entry_rcu(chanctx, &local->chanctx_list, list)
5002 +               num_chanctx++;
5003 +       rcu_read_unlock();
5004 +
5005 +       if (num_chanctx > 1)
5006 +               return -EBUSY;
5007 +
5008 +       /* don't allow another channel switch if one is already active. */
5009 +       if (sdata->vif.csa_active)
5010 +               return -EBUSY;
5011 +
5012 +       /* only handle AP for now. */
5013 +       switch (sdata->vif.type) {
5014 +       case NL80211_IFTYPE_AP:
5015 +               break;
5016 +       default:
5017 +               return -EOPNOTSUPP;
5018 +       }
5019 +
5020 +       sdata->u.ap.next_beacon = cfg80211_beacon_dup(&params->beacon_after);
5021 +       if (!sdata->u.ap.next_beacon)
5022 +               return -ENOMEM;
5023 +
5024 +       sdata->csa_counter_offset_beacon = params->counter_offset_beacon;
5025 +       sdata->csa_counter_offset_presp = params->counter_offset_presp;
5026 +       sdata->csa_radar_required = params->radar_required;
5027 +
5028 +       if (params->block_tx)
5029 +               ieee80211_stop_queues_by_reason(&local->hw,
5030 +                               IEEE80211_MAX_QUEUE_MAP,
5031 +                               IEEE80211_QUEUE_STOP_REASON_CSA);
5032 +
5033 +       err = ieee80211_assign_beacon(sdata, &params->beacon_csa);
5034 +       if (err < 0)
5035 +               return err;
5036 +
5037 +       local->csa_chandef = params->chandef;
5038 +       sdata->vif.csa_active = true;
5039 +
5040 +       ieee80211_bss_info_change_notify(sdata, err);
5041 +       drv_channel_switch_beacon(sdata, &params->chandef);
5042 +
5043 +       return 0;
5044 +}
5045 +
5046  static int ieee80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
5047                              struct ieee80211_channel *chan, bool offchan,
5048                              unsigned int wait, const u8 *buf, size_t len,
5049 @@ -3501,4 +3683,5 @@ struct cfg80211_ops mac80211_config_ops 
5050         .get_et_strings = ieee80211_get_et_strings,
5051         .get_channel = ieee80211_cfg_get_channel,
5052         .start_radar_detection = ieee80211_start_radar_detection,
5053 +       .channel_switch = ieee80211_channel_switch,
5054  };
5055 --- a/net/mac80211/chan.c
5056 +++ b/net/mac80211/chan.c
5057 @@ -410,6 +410,64 @@ int ieee80211_vif_use_channel(struct iee
5058         return ret;
5059  }
5060  
5061 +int ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata,
5062 +                                const struct cfg80211_chan_def *chandef,
5063 +                                u32 *changed)
5064 +{
5065 +       struct ieee80211_local *local = sdata->local;
5066 +       struct ieee80211_chanctx_conf *conf;
5067 +       struct ieee80211_chanctx *ctx;
5068 +       int ret;
5069 +       u32 chanctx_changed = 0;
5070 +
5071 +       /* should never be called if not performing a channel switch. */
5072 +       if (WARN_ON(!sdata->vif.csa_active))
5073 +               return -EINVAL;
5074 +
5075 +       if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
5076 +                                    IEEE80211_CHAN_DISABLED))
5077 +               return -EINVAL;
5078 +
5079 +       mutex_lock(&local->chanctx_mtx);
5080 +       conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
5081 +                                        lockdep_is_held(&local->chanctx_mtx));
5082 +       if (!conf) {
5083 +               ret = -EINVAL;
5084 +               goto out;
5085 +       }
5086 +
5087 +       ctx = container_of(conf, struct ieee80211_chanctx, conf);
5088 +       if (ctx->refcount != 1) {
5089 +               ret = -EINVAL;
5090 +               goto out;
5091 +       }
5092 +
5093 +       if (sdata->vif.bss_conf.chandef.width != chandef->width) {
5094 +               chanctx_changed = IEEE80211_CHANCTX_CHANGE_WIDTH;
5095 +               *changed |= BSS_CHANGED_BANDWIDTH;
5096 +       }
5097 +
5098 +       sdata->vif.bss_conf.chandef = *chandef;
5099 +       ctx->conf.def = *chandef;
5100 +
5101 +       chanctx_changed |= IEEE80211_CHANCTX_CHANGE_CHANNEL;
5102 +       drv_change_chanctx(local, ctx, chanctx_changed);
5103 +
5104 +       if (!local->use_chanctx) {
5105 +               local->_oper_chandef = *chandef;
5106 +               ieee80211_hw_config(local, 0);
5107 +       }
5108 +
5109 +       ieee80211_recalc_chanctx_chantype(local, ctx);
5110 +       ieee80211_recalc_smps_chanctx(local, ctx);
5111 +       ieee80211_recalc_radar_chanctx(local, ctx);
5112 +
5113 +       ret = 0;
5114 + out:
5115 +       mutex_unlock(&local->chanctx_mtx);
5116 +       return ret;
5117 +}
5118 +
5119  int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
5120                                    const struct cfg80211_chan_def *chandef,
5121                                    u32 *changed)
5122 --- a/net/mac80211/driver-ops.h
5123 +++ b/net/mac80211/driver-ops.h
5124 @@ -1104,4 +1104,17 @@ static inline void drv_ipv6_addr_change(
5125  }
5126  #endif
5127  
5128 +static inline void
5129 +drv_channel_switch_beacon(struct ieee80211_sub_if_data *sdata,
5130 +                         struct cfg80211_chan_def *chandef)
5131 +{
5132 +       struct ieee80211_local *local = sdata->local;
5133 +
5134 +       if (local->ops->channel_switch_beacon) {
5135 +               trace_drv_channel_switch_beacon(local, sdata, chandef);
5136 +               local->ops->channel_switch_beacon(&local->hw, &sdata->vif,
5137 +                                                 chandef);
5138 +       }
5139 +}
5140 +
5141  #endif /* __MAC80211_DRIVER_OPS */
5142 --- a/net/mac80211/ieee80211_i.h
5143 +++ b/net/mac80211/ieee80211_i.h
5144 @@ -53,9 +53,6 @@ struct ieee80211_local;
5145   * increased memory use (about 2 kB of RAM per entry). */
5146  #define IEEE80211_FRAGMENT_MAX 4
5147  
5148 -#define TU_TO_JIFFIES(x)       (usecs_to_jiffies((x) * 1024))
5149 -#define TU_TO_EXP_TIME(x)      (jiffies + TU_TO_JIFFIES(x))
5150 -
5151  /* power level hasn't been configured (or set to automatic) */
5152  #define IEEE80211_UNSET_POWER_LEVEL    INT_MIN
5153  
5154 @@ -259,6 +256,8 @@ struct ieee80211_if_ap {
5155         struct beacon_data __rcu *beacon;
5156         struct probe_resp __rcu *probe_resp;
5157  
5158 +       /* to be used after channel switch. */
5159 +       struct cfg80211_beacon_data *next_beacon;
5160         struct list_head vlans;
5161  
5162         struct ps_data ps;
5163 @@ -713,6 +712,11 @@ struct ieee80211_sub_if_data {
5164  
5165         struct ieee80211_tx_queue_params tx_conf[IEEE80211_NUM_ACS];
5166  
5167 +       struct work_struct csa_finalize_work;
5168 +       int csa_counter_offset_beacon;
5169 +       int csa_counter_offset_presp;
5170 +       bool csa_radar_required;
5171 +
5172         /* used to reconfigure hardware SM PS */
5173         struct work_struct recalc_smps;
5174  
5175 @@ -1346,6 +1350,9 @@ void ieee80211_roc_notify_destroy(struct
5176  void ieee80211_sw_roc_work(struct work_struct *work);
5177  void ieee80211_handle_roc_started(struct ieee80211_roc_work *roc);
5178  
5179 +/* channel switch handling */
5180 +void ieee80211_csa_finalize_work(struct work_struct *work);
5181 +
5182  /* interface handling */
5183  int ieee80211_iface_init(void);
5184  void ieee80211_iface_exit(void);
5185 @@ -1367,6 +1374,8 @@ void ieee80211_del_virtual_monitor(struc
5186  
5187  bool __ieee80211_recalc_txpower(struct ieee80211_sub_if_data *sdata);
5188  void ieee80211_recalc_txpower(struct ieee80211_sub_if_data *sdata);
5189 +int ieee80211_assign_beacon(struct ieee80211_sub_if_data *sdata,
5190 +                           struct cfg80211_beacon_data *params);
5191  
5192  static inline bool ieee80211_sdata_running(struct ieee80211_sub_if_data *sdata)
5193  {
5194 @@ -1627,6 +1636,11 @@ int __must_check
5195  ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
5196                                const struct cfg80211_chan_def *chandef,
5197                                u32 *changed);
5198 +/* NOTE: only use ieee80211_vif_change_channel() for channel switch */
5199 +int __must_check
5200 +ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata,
5201 +                            const struct cfg80211_chan_def *chandef,
5202 +                            u32 *changed);
5203  void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata);
5204  void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata);
5205  void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
5206 --- a/net/mac80211/trace.h
5207 +++ b/net/mac80211/trace.h
5208 @@ -1906,6 +1906,32 @@ TRACE_EVENT(api_radar_detected,
5209         )
5210  );
5211  
5212 +TRACE_EVENT(drv_channel_switch_beacon,
5213 +       TP_PROTO(struct ieee80211_local *local,
5214 +                struct ieee80211_sub_if_data *sdata,
5215 +                struct cfg80211_chan_def *chandef),
5216 +
5217 +       TP_ARGS(local, sdata, chandef),
5218 +
5219 +       TP_STRUCT__entry(
5220 +               LOCAL_ENTRY
5221 +               VIF_ENTRY
5222 +               CHANDEF_ENTRY
5223 +       ),
5224 +
5225 +       TP_fast_assign(
5226 +               LOCAL_ASSIGN;
5227 +               VIF_ASSIGN;
5228 +               CHANDEF_ASSIGN(chandef);
5229 +       ),
5230 +
5231 +       TP_printk(
5232 +               LOCAL_PR_FMT VIF_PR_FMT " channel switch to " CHANDEF_PR_FMT,
5233 +               LOCAL_PR_ARG, VIF_PR_ARG, CHANDEF_PR_ARG
5234 +       )
5235 +);
5236 +
5237 +
5238  #ifdef CPTCFG_MAC80211_MESSAGE_TRACING
5239  #undef TRACE_SYSTEM
5240  #define TRACE_SYSTEM mac80211_msg
5241 --- a/net/mac80211/tx.c
5242 +++ b/net/mac80211/tx.c
5243 @@ -2326,6 +2326,81 @@ static int ieee80211_beacon_add_tim(stru
5244         return 0;
5245  }
5246  
5247 +void ieee80211_csa_finish(struct ieee80211_vif *vif)
5248 +{
5249 +       struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5250 +
5251 +       ieee80211_queue_work(&sdata->local->hw,
5252 +                            &sdata->csa_finalize_work);
5253 +}
5254 +EXPORT_SYMBOL(ieee80211_csa_finish);
5255 +
5256 +static void ieee80211_update_csa(struct ieee80211_sub_if_data *sdata,
5257 +                                struct beacon_data *beacon)
5258 +{
5259 +       struct probe_resp *resp;
5260 +       int counter_offset_beacon = sdata->csa_counter_offset_beacon;
5261 +       int counter_offset_presp = sdata->csa_counter_offset_presp;
5262 +
5263 +       /* warn if the driver did not check for/react to csa completeness */
5264 +       if (WARN_ON(((u8 *)beacon->tail)[counter_offset_beacon] == 0))
5265 +               return;
5266 +
5267 +       ((u8 *)beacon->tail)[counter_offset_beacon]--;
5268 +
5269 +       if (sdata->vif.type == NL80211_IFTYPE_AP &&
5270 +           counter_offset_presp) {
5271 +               rcu_read_lock();
5272 +               resp = rcu_dereference(sdata->u.ap.probe_resp);
5273 +
5274 +               /* if nl80211 accepted the offset, this should not happen. */
5275 +               if (WARN_ON(!resp)) {
5276 +                       rcu_read_unlock();
5277 +                       return;
5278 +               }
5279 +               resp->data[counter_offset_presp]--;
5280 +               rcu_read_unlock();
5281 +       }
5282 +}
5283 +
5284 +bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
5285 +{
5286 +       struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5287 +       struct beacon_data *beacon = NULL;
5288 +       u8 *beacon_data;
5289 +       size_t beacon_data_len;
5290 +       int counter_beacon = sdata->csa_counter_offset_beacon;
5291 +       int ret = false;
5292 +
5293 +       if (!ieee80211_sdata_running(sdata))
5294 +               return false;
5295 +
5296 +       rcu_read_lock();
5297 +       if (vif->type == NL80211_IFTYPE_AP) {
5298 +               struct ieee80211_if_ap *ap = &sdata->u.ap;
5299 +
5300 +               beacon = rcu_dereference(ap->beacon);
5301 +               if (WARN_ON(!beacon || !beacon->tail))
5302 +                       goto out;
5303 +               beacon_data = beacon->tail;
5304 +               beacon_data_len = beacon->tail_len;
5305 +       } else {
5306 +               WARN_ON(1);
5307 +               goto out;
5308 +       }
5309 +
5310 +       if (WARN_ON(counter_beacon > beacon_data_len))
5311 +               goto out;
5312 +
5313 +       if (beacon_data[counter_beacon] == 0)
5314 +               ret = true;
5315 + out:
5316 +       rcu_read_unlock();
5317 +
5318 +       return ret;
5319 +}
5320 +EXPORT_SYMBOL(ieee80211_csa_is_complete);
5321 +
5322  struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
5323                                          struct ieee80211_vif *vif,
5324                                          u16 *tim_offset, u16 *tim_length)
5325 @@ -2356,6 +2431,9 @@ struct sk_buff *ieee80211_beacon_get_tim
5326                 struct beacon_data *beacon = rcu_dereference(ap->beacon);
5327  
5328                 if (beacon) {
5329 +                       if (sdata->vif.csa_active)
5330 +                               ieee80211_update_csa(sdata, beacon);
5331 +
5332                         /*
5333                          * headroom, head length,
5334                          * tail length and maximum TIM length
5335 --- a/net/wireless/rdev-ops.h
5336 +++ b/net/wireless/rdev-ops.h
5337 @@ -923,4 +923,16 @@ static inline void rdev_crit_proto_stop(
5338         trace_rdev_return_void(&rdev->wiphy);
5339  }
5340  
5341 +static inline int rdev_channel_switch(struct cfg80211_registered_device *rdev,
5342 +                                     struct net_device *dev,
5343 +                                     struct cfg80211_csa_settings *params)
5344 +{
5345 +       int ret;
5346 +
5347 +       trace_rdev_channel_switch(&rdev->wiphy, dev, params);
5348 +       ret = rdev->ops->channel_switch(&rdev->wiphy, dev, params);
5349 +       trace_rdev_return_int(&rdev->wiphy, ret);
5350 +       return ret;
5351 +}
5352 +
5353  #endif /* __CFG80211_RDEV_OPS */
5354 --- a/net/wireless/trace.h
5355 +++ b/net/wireless/trace.h
5356 @@ -1841,6 +1841,39 @@ TRACE_EVENT(rdev_crit_proto_stop,
5357                   WIPHY_PR_ARG, WDEV_PR_ARG)
5358  );
5359  
5360 +TRACE_EVENT(rdev_channel_switch,
5361 +       TP_PROTO(struct wiphy *wiphy, struct net_device *netdev,
5362 +                struct cfg80211_csa_settings *params),
5363 +       TP_ARGS(wiphy, netdev, params),
5364 +       TP_STRUCT__entry(
5365 +               WIPHY_ENTRY
5366 +               NETDEV_ENTRY
5367 +               CHAN_DEF_ENTRY
5368 +               __field(u16, counter_offset_beacon)
5369 +               __field(u16, counter_offset_presp)
5370 +               __field(bool, radar_required)
5371 +               __field(bool, block_tx)
5372 +               __field(u8, count)
5373 +       ),
5374 +       TP_fast_assign(
5375 +               WIPHY_ASSIGN;
5376 +               NETDEV_ASSIGN;
5377 +               CHAN_DEF_ASSIGN(&params->chandef);
5378 +               __entry->counter_offset_beacon = params->counter_offset_beacon;
5379 +               __entry->counter_offset_presp = params->counter_offset_presp;
5380 +               __entry->radar_required = params->radar_required;
5381 +               __entry->block_tx = params->block_tx;
5382 +               __entry->count = params->count;
5383 +       ),
5384 +       TP_printk(WIPHY_PR_FMT ", " NETDEV_PR_FMT ", " CHAN_DEF_PR_FMT
5385 +                 ", block_tx: %d, count: %u, radar_required: %d"
5386 +                 ", counter offsets (beacon/presp): %u/%u",
5387 +                 WIPHY_PR_ARG, NETDEV_PR_ARG, CHAN_DEF_PR_ARG,
5388 +                 __entry->block_tx, __entry->count, __entry->radar_required,
5389 +                 __entry->counter_offset_beacon,
5390 +                 __entry->counter_offset_presp)
5391 +);
5392 +
5393  /*************************************************************
5394   *          cfg80211 exported functions traces              *
5395   *************************************************************/
5396 --- a/drivers/net/wireless/ath/ath.h
5397 +++ b/drivers/net/wireless/ath/ath.h
5398 @@ -159,7 +159,7 @@ struct ath_common {
5399  
5400         bool btcoex_enabled;
5401         bool disable_ani;
5402 -       bool antenna_diversity;
5403 +       bool bt_ant_diversity;
5404  };
5405  
5406  struct sk_buff *ath_rxbuf_alloc(struct ath_common *common,
5407 --- a/drivers/net/wireless/ath/ath9k/antenna.c
5408 +++ b/drivers/net/wireless/ath/ath9k/antenna.c
5409 @@ -16,37 +16,119 @@
5410  
5411  #include "ath9k.h"
5412  
5413 -static inline bool ath_is_alt_ant_ratio_better(int alt_ratio, int maxdelta,
5414 +/*
5415 + * AR9285
5416 + * ======
5417 + *
5418 + * EEPROM has 2 4-bit fields containing the card configuration.
5419 + *
5420 + * antdiv_ctl1:
5421 + * ------------
5422 + * bb_enable_ant_div_lnadiv : 1
5423 + * bb_ant_div_alt_gaintb    : 1
5424 + * bb_ant_div_main_gaintb   : 1
5425 + * bb_enable_ant_fast_div   : 1
5426 + *
5427 + * antdiv_ctl2:
5428 + * -----------
5429 + * bb_ant_div_alt_lnaconf  : 2
5430 + * bb_ant_div_main_lnaconf : 2
5431 + *
5432 + * The EEPROM bits are used as follows:
5433 + * ------------------------------------
5434 + *
5435 + * bb_enable_ant_div_lnadiv      - Enable LNA path rx antenna diversity/combining.
5436 + *                                 Set in AR_PHY_MULTICHAIN_GAIN_CTL.
5437 + *
5438 + * bb_ant_div_[alt/main]_gaintb  - 0 -> Antenna config Alt/Main uses gaintable 0
5439 + *                                 1 -> Antenna config Alt/Main uses gaintable 1
5440 + *                                 Set in AR_PHY_MULTICHAIN_GAIN_CTL.
5441 + *
5442 + * bb_enable_ant_fast_div        - Enable fast antenna diversity.
5443 + *                                 Set in AR_PHY_CCK_DETECT.
5444 + *
5445 + * bb_ant_div_[alt/main]_lnaconf - Alt/Main LNA diversity/combining input config.
5446 + *                                 Set in AR_PHY_MULTICHAIN_GAIN_CTL.
5447 + *                                 10=LNA1
5448 + *                                 01=LNA2
5449 + *                                 11=LNA1+LNA2
5450 + *                                 00=LNA1-LNA2
5451 + *
5452 + * AR9485 / AR9565 / AR9331
5453 + * ========================
5454 + *
5455 + * The same bits are present in the EEPROM, but the location in the
5456 + * EEPROM is different (ant_div_control in ar9300_BaseExtension_1).
5457 + *
5458 + * ant_div_alt_lnaconf      ==> bit 0~1
5459 + * ant_div_main_lnaconf     ==> bit 2~3
5460 + * ant_div_alt_gaintb       ==> bit 4
5461 + * ant_div_main_gaintb      ==> bit 5
5462 + * enable_ant_div_lnadiv    ==> bit 6
5463 + * enable_ant_fast_div      ==> bit 7
5464 + */
5465 +
5466 +static inline bool ath_is_alt_ant_ratio_better(struct ath_ant_comb *antcomb,
5467 +                                              int alt_ratio, int maxdelta,
5468                                                int mindelta, int main_rssi_avg,
5469                                                int alt_rssi_avg, int pkt_count)
5470  {
5471 -       return (((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
5472 -                (alt_rssi_avg > main_rssi_avg + maxdelta)) ||
5473 -               (alt_rssi_avg > main_rssi_avg + mindelta)) && (pkt_count > 50);
5474 +       if (pkt_count <= 50)
5475 +               return false;
5476 +
5477 +       if (alt_rssi_avg > main_rssi_avg + mindelta)
5478 +               return true;
5479 +
5480 +       if (alt_ratio >= antcomb->ant_ratio2 &&
5481 +           alt_rssi_avg >= antcomb->low_rssi_thresh &&
5482 +           (alt_rssi_avg > main_rssi_avg + maxdelta))
5483 +               return true;
5484 +
5485 +       return false;
5486  }
5487  
5488 -static inline bool ath_ant_div_comb_alt_check(u8 div_group, int alt_ratio,
5489 -                                             int curr_main_set, int curr_alt_set,
5490 -                                             int alt_rssi_avg, int main_rssi_avg)
5491 +static inline bool ath_ant_div_comb_alt_check(struct ath_hw_antcomb_conf *conf,
5492 +                                             struct ath_ant_comb *antcomb,
5493 +                                             int alt_ratio, int alt_rssi_avg,
5494 +                                             int main_rssi_avg)
5495  {
5496 -       bool result = false;
5497 -       switch (div_group) {
5498 +       bool result, set1, set2;
5499 +
5500 +       result = set1 = set2 = false;
5501 +
5502 +       if (conf->main_lna_conf == ATH_ANT_DIV_COMB_LNA2 &&
5503 +           conf->alt_lna_conf == ATH_ANT_DIV_COMB_LNA1)
5504 +               set1 = true;
5505 +
5506 +       if (conf->main_lna_conf == ATH_ANT_DIV_COMB_LNA1 &&
5507 +           conf->alt_lna_conf == ATH_ANT_DIV_COMB_LNA2)
5508 +               set2 = true;
5509 +
5510 +       switch (conf->div_group) {
5511         case 0:
5512                 if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
5513                         result = true;
5514                 break;
5515         case 1:
5516         case 2:
5517 -               if ((((curr_main_set == ATH_ANT_DIV_COMB_LNA2) &&
5518 -                     (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) &&
5519 -                     (alt_rssi_avg >= (main_rssi_avg - 5))) ||
5520 -                    ((curr_main_set == ATH_ANT_DIV_COMB_LNA1) &&
5521 -                     (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) &&
5522 -                     (alt_rssi_avg >= (main_rssi_avg - 2)))) &&
5523 -                   (alt_rssi_avg >= 4))
5524 +               if (alt_rssi_avg < 4 || alt_rssi_avg < antcomb->low_rssi_thresh)
5525 +                       break;
5526 +
5527 +               if ((set1 && (alt_rssi_avg >= (main_rssi_avg - 5))) ||
5528 +                   (set2 && (alt_rssi_avg >= (main_rssi_avg - 2))) ||
5529 +                   (alt_ratio > antcomb->ant_ratio))
5530                         result = true;
5531 -               else
5532 -                       result = false;
5533 +
5534 +               break;
5535 +       case 3:
5536 +               if (alt_rssi_avg < 4 || alt_rssi_avg < antcomb->low_rssi_thresh)
5537 +                       break;
5538 +
5539 +               if ((set1 && (alt_rssi_avg >= (main_rssi_avg - 3))) ||
5540 +                   (set2 && (alt_rssi_avg >= (main_rssi_avg + 3))) ||
5541 +                   (alt_ratio > antcomb->ant_ratio))
5542 +                       result = true;
5543 +
5544                 break;
5545         }
5546  
5547 @@ -108,6 +190,74 @@ static void ath_lnaconf_alt_good_scan(st
5548         }
5549  }
5550  
5551 +static void ath_ant_set_alt_ratio(struct ath_ant_comb *antcomb,
5552 +                                 struct ath_hw_antcomb_conf *conf)
5553 +{
5554 +       /* set alt to the conf with maximun ratio */
5555 +       if (antcomb->first_ratio && antcomb->second_ratio) {
5556 +               if (antcomb->rssi_second > antcomb->rssi_third) {
5557 +                       /* first alt*/
5558 +                       if ((antcomb->first_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1) ||
5559 +                           (antcomb->first_quick_scan_conf == ATH_ANT_DIV_COMB_LNA2))
5560 +                               /* Set alt LNA1 or LNA2*/
5561 +                               if (conf->main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
5562 +                                       conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1;
5563 +                               else
5564 +                                       conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA2;
5565 +                       else
5566 +                               /* Set alt to A+B or A-B */
5567 +                               conf->alt_lna_conf =
5568 +                                       antcomb->first_quick_scan_conf;
5569 +               } else if ((antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1) ||
5570 +                          (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA2)) {
5571 +                       /* Set alt LNA1 or LNA2 */
5572 +                       if (conf->main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
5573 +                               conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1;
5574 +                       else
5575 +                               conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA2;
5576 +               } else {
5577 +                       /* Set alt to A+B or A-B */
5578 +                       conf->alt_lna_conf = antcomb->second_quick_scan_conf;
5579 +               }
5580 +       } else if (antcomb->first_ratio) {
5581 +               /* first alt */
5582 +               if ((antcomb->first_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1) ||
5583 +                   (antcomb->first_quick_scan_conf == ATH_ANT_DIV_COMB_LNA2))
5584 +                       /* Set alt LNA1 or LNA2 */
5585 +                       if (conf->main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
5586 +                               conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1;
5587 +                       else
5588 +                               conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA2;
5589 +               else
5590 +                       /* Set alt to A+B or A-B */
5591 +                       conf->alt_lna_conf = antcomb->first_quick_scan_conf;
5592 +       } else if (antcomb->second_ratio) {
5593 +               /* second alt */
5594 +               if ((antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1) ||
5595 +                   (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA2))
5596 +                       /* Set alt LNA1 or LNA2 */
5597 +                       if (conf->main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
5598 +                               conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1;
5599 +                       else
5600 +                               conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA2;
5601 +               else
5602 +                       /* Set alt to A+B or A-B */
5603 +                       conf->alt_lna_conf = antcomb->second_quick_scan_conf;
5604 +       } else {
5605 +               /* main is largest */
5606 +               if ((antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) ||
5607 +                   (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2))
5608 +                       /* Set alt LNA1 or LNA2 */
5609 +                       if (conf->main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
5610 +                               conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1;
5611 +                       else
5612 +                               conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA2;
5613 +               else
5614 +                       /* Set alt to A+B or A-B */
5615 +                       conf->alt_lna_conf = antcomb->main_conf;
5616 +       }
5617 +}
5618 +
5619  static void ath_select_ant_div_from_quick_scan(struct ath_ant_comb *antcomb,
5620                                        struct ath_hw_antcomb_conf *div_ant_conf,
5621                                        int main_rssi_avg, int alt_rssi_avg,
5622 @@ -129,7 +279,7 @@ static void ath_select_ant_div_from_quic
5623  
5624                 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
5625                         /* main is LNA1 */
5626 -                       if (ath_is_alt_ant_ratio_better(alt_ratio,
5627 +                       if (ath_is_alt_ant_ratio_better(antcomb, alt_ratio,
5628                                                 ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
5629                                                 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
5630                                                 main_rssi_avg, alt_rssi_avg,
5631 @@ -138,7 +288,7 @@ static void ath_select_ant_div_from_quic
5632                         else
5633                                 antcomb->first_ratio = false;
5634                 } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
5635 -                       if (ath_is_alt_ant_ratio_better(alt_ratio,
5636 +                       if (ath_is_alt_ant_ratio_better(antcomb, alt_ratio,
5637                                                 ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
5638                                                 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
5639                                                 main_rssi_avg, alt_rssi_avg,
5640 @@ -147,11 +297,11 @@ static void ath_select_ant_div_from_quic
5641                         else
5642                                 antcomb->first_ratio = false;
5643                 } else {
5644 -                       if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
5645 -                             (alt_rssi_avg > main_rssi_avg +
5646 -                              ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
5647 -                            (alt_rssi_avg > main_rssi_avg)) &&
5648 -                           (antcomb->total_pkt_count > 50))
5649 +                       if (ath_is_alt_ant_ratio_better(antcomb, alt_ratio,
5650 +                                               ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
5651 +                                               0,
5652 +                                               main_rssi_avg, alt_rssi_avg,
5653 +                                               antcomb->total_pkt_count))
5654                                 antcomb->first_ratio = true;
5655                         else
5656                                 antcomb->first_ratio = false;
5657 @@ -164,17 +314,21 @@ static void ath_select_ant_div_from_quic
5658                 antcomb->rssi_first = main_rssi_avg;
5659                 antcomb->rssi_third = alt_rssi_avg;
5660  
5661 -               if (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1)
5662 +               switch(antcomb->second_quick_scan_conf) {
5663 +               case ATH_ANT_DIV_COMB_LNA1:
5664                         antcomb->rssi_lna1 = alt_rssi_avg;
5665 -               else if (antcomb->second_quick_scan_conf ==
5666 -                        ATH_ANT_DIV_COMB_LNA2)
5667 +                       break;
5668 +               case ATH_ANT_DIV_COMB_LNA2:
5669                         antcomb->rssi_lna2 = alt_rssi_avg;
5670 -               else if (antcomb->second_quick_scan_conf ==
5671 -                        ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2) {
5672 +                       break;
5673 +               case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
5674                         if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2)
5675                                 antcomb->rssi_lna2 = main_rssi_avg;
5676                         else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1)
5677                                 antcomb->rssi_lna1 = main_rssi_avg;
5678 +                       break;
5679 +               default:
5680 +                       break;
5681                 }
5682  
5683                 if (antcomb->rssi_lna2 > antcomb->rssi_lna1 +
5684 @@ -184,7 +338,7 @@ static void ath_select_ant_div_from_quic
5685                         div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
5686  
5687                 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
5688 -                       if (ath_is_alt_ant_ratio_better(alt_ratio,
5689 +                       if (ath_is_alt_ant_ratio_better(antcomb, alt_ratio,
5690                                                 ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
5691                                                 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
5692                                                 main_rssi_avg, alt_rssi_avg,
5693 @@ -193,7 +347,7 @@ static void ath_select_ant_div_from_quic
5694                         else
5695                                 antcomb->second_ratio = false;
5696                 } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
5697 -                       if (ath_is_alt_ant_ratio_better(alt_ratio,
5698 +                       if (ath_is_alt_ant_ratio_better(antcomb, alt_ratio,
5699                                                 ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
5700                                                 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
5701                                                 main_rssi_avg, alt_rssi_avg,
5702 @@ -202,105 +356,18 @@ static void ath_select_ant_div_from_quic
5703                         else
5704                                 antcomb->second_ratio = false;
5705                 } else {
5706 -                       if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
5707 -                             (alt_rssi_avg > main_rssi_avg +
5708 -                              ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
5709 -                            (alt_rssi_avg > main_rssi_avg)) &&
5710 -                           (antcomb->total_pkt_count > 50))
5711 +                       if (ath_is_alt_ant_ratio_better(antcomb, alt_ratio,
5712 +                                               ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
5713 +                                               0,
5714 +                                               main_rssi_avg, alt_rssi_avg,
5715 +                                               antcomb->total_pkt_count))
5716                                 antcomb->second_ratio = true;
5717                         else
5718                                 antcomb->second_ratio = false;
5719                 }
5720  
5721 -               /* set alt to the conf with maximun ratio */
5722 -               if (antcomb->first_ratio && antcomb->second_ratio) {
5723 -                       if (antcomb->rssi_second > antcomb->rssi_third) {
5724 -                               /* first alt*/
5725 -                               if ((antcomb->first_quick_scan_conf ==
5726 -                                   ATH_ANT_DIV_COMB_LNA1) ||
5727 -                                   (antcomb->first_quick_scan_conf ==
5728 -                                   ATH_ANT_DIV_COMB_LNA2))
5729 -                                       /* Set alt LNA1 or LNA2*/
5730 -                                       if (div_ant_conf->main_lna_conf ==
5731 -                                           ATH_ANT_DIV_COMB_LNA2)
5732 -                                               div_ant_conf->alt_lna_conf =
5733 -                                                       ATH_ANT_DIV_COMB_LNA1;
5734 -                                       else
5735 -                                               div_ant_conf->alt_lna_conf =
5736 -                                                       ATH_ANT_DIV_COMB_LNA2;
5737 -                               else
5738 -                                       /* Set alt to A+B or A-B */
5739 -                                       div_ant_conf->alt_lna_conf =
5740 -                                               antcomb->first_quick_scan_conf;
5741 -                       } else if ((antcomb->second_quick_scan_conf ==
5742 -                                  ATH_ANT_DIV_COMB_LNA1) ||
5743 -                                  (antcomb->second_quick_scan_conf ==
5744 -                                  ATH_ANT_DIV_COMB_LNA2)) {
5745 -                               /* Set alt LNA1 or LNA2 */
5746 -                               if (div_ant_conf->main_lna_conf ==
5747 -                                   ATH_ANT_DIV_COMB_LNA2)
5748 -                                       div_ant_conf->alt_lna_conf =
5749 -                                               ATH_ANT_DIV_COMB_LNA1;
5750 -                               else
5751 -                                       div_ant_conf->alt_lna_conf =
5752 -                                               ATH_ANT_DIV_COMB_LNA2;
5753 -                       } else {
5754 -                               /* Set alt to A+B or A-B */
5755 -                               div_ant_conf->alt_lna_conf =
5756 -                                       antcomb->second_quick_scan_conf;
5757 -                       }
5758 -               } else if (antcomb->first_ratio) {
5759 -                       /* first alt */
5760 -                       if ((antcomb->first_quick_scan_conf ==
5761 -                           ATH_ANT_DIV_COMB_LNA1) ||
5762 -                           (antcomb->first_quick_scan_conf ==
5763 -                           ATH_ANT_DIV_COMB_LNA2))
5764 -                                       /* Set alt LNA1 or LNA2 */
5765 -                               if (div_ant_conf->main_lna_conf ==
5766 -                                   ATH_ANT_DIV_COMB_LNA2)
5767 -                                       div_ant_conf->alt_lna_conf =
5768 -                                                       ATH_ANT_DIV_COMB_LNA1;
5769 -                               else
5770 -                                       div_ant_conf->alt_lna_conf =
5771 -                                                       ATH_ANT_DIV_COMB_LNA2;
5772 -                       else
5773 -                               /* Set alt to A+B or A-B */
5774 -                               div_ant_conf->alt_lna_conf =
5775 -                                               antcomb->first_quick_scan_conf;
5776 -               } else if (antcomb->second_ratio) {
5777 -                               /* second alt */
5778 -                       if ((antcomb->second_quick_scan_conf ==
5779 -                           ATH_ANT_DIV_COMB_LNA1) ||
5780 -                           (antcomb->second_quick_scan_conf ==
5781 -                           ATH_ANT_DIV_COMB_LNA2))
5782 -                               /* Set alt LNA1 or LNA2 */
5783 -                               if (div_ant_conf->main_lna_conf ==
5784 -                                   ATH_ANT_DIV_COMB_LNA2)
5785 -                                       div_ant_conf->alt_lna_conf =
5786 -                                               ATH_ANT_DIV_COMB_LNA1;
5787 -                               else
5788 -                                       div_ant_conf->alt_lna_conf =
5789 -                                               ATH_ANT_DIV_COMB_LNA2;
5790 -                       else
5791 -                               /* Set alt to A+B or A-B */
5792 -                               div_ant_conf->alt_lna_conf =
5793 -                                               antcomb->second_quick_scan_conf;
5794 -               } else {
5795 -                       /* main is largest */
5796 -                       if ((antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) ||
5797 -                           (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2))
5798 -                               /* Set alt LNA1 or LNA2 */
5799 -                               if (div_ant_conf->main_lna_conf ==
5800 -                                   ATH_ANT_DIV_COMB_LNA2)
5801 -                                       div_ant_conf->alt_lna_conf =
5802 -                                                       ATH_ANT_DIV_COMB_LNA1;
5803 -                               else
5804 -                                       div_ant_conf->alt_lna_conf =
5805 -                                                       ATH_ANT_DIV_COMB_LNA2;
5806 -                       else
5807 -                               /* Set alt to A+B or A-B */
5808 -                               div_ant_conf->alt_lna_conf = antcomb->main_conf;
5809 -               }
5810 +               ath_ant_set_alt_ratio(antcomb, div_ant_conf);
5811 +
5812                 break;
5813         default:
5814                 break;
5815 @@ -430,8 +497,7 @@ static void ath_ant_div_conf_fast_divbia
5816                         ant_conf->fast_div_bias = 0x1;
5817                         break;
5818                 case 0x10: /* LNA2 A-B */
5819 -                       if (!(antcomb->scan) &&
5820 -                               (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
5821 +                       if (!antcomb->scan && (alt_ratio > antcomb->ant_ratio))
5822                                 ant_conf->fast_div_bias = 0x1;
5823                         else
5824                                 ant_conf->fast_div_bias = 0x2;
5825 @@ -440,15 +506,13 @@ static void ath_ant_div_conf_fast_divbia
5826                         ant_conf->fast_div_bias = 0x1;
5827                         break;
5828                 case 0x13: /* LNA2 A+B */
5829 -                       if (!(antcomb->scan) &&
5830 -                               (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
5831 +                       if (!antcomb->scan && (alt_ratio > antcomb->ant_ratio))
5832                                 ant_conf->fast_div_bias = 0x1;
5833                         else
5834                                 ant_conf->fast_div_bias = 0x2;
5835                         break;
5836                 case 0x20: /* LNA1 A-B */
5837 -                       if (!(antcomb->scan) &&
5838 -                               (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
5839 +                       if (!antcomb->scan && (alt_ratio > antcomb->ant_ratio))
5840                                 ant_conf->fast_div_bias = 0x1;
5841                         else
5842                                 ant_conf->fast_div_bias = 0x2;
5843 @@ -457,8 +521,7 @@ static void ath_ant_div_conf_fast_divbia
5844                         ant_conf->fast_div_bias = 0x1;
5845                         break;
5846                 case 0x23: /* LNA1 A+B */
5847 -                       if (!(antcomb->scan) &&
5848 -                               (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
5849 +                       if (!antcomb->scan && (alt_ratio > antcomb->ant_ratio))
5850                                 ant_conf->fast_div_bias = 0x1;
5851                         else
5852                                 ant_conf->fast_div_bias = 0x2;
5853 @@ -475,6 +538,9 @@ static void ath_ant_div_conf_fast_divbia
5854                 default:
5855                         break;
5856                 }
5857 +
5858 +               if (antcomb->fast_div_bias)
5859 +                       ant_conf->fast_div_bias = antcomb->fast_div_bias;
5860         } else if (ant_conf->div_group == 3) {
5861                 switch ((ant_conf->main_lna_conf << 4) |
5862                         ant_conf->alt_lna_conf) {
5863 @@ -540,6 +606,138 @@ static void ath_ant_div_conf_fast_divbia
5864         }
5865  }
5866  
5867 +static void ath_ant_try_scan(struct ath_ant_comb *antcomb,
5868 +                            struct ath_hw_antcomb_conf *conf,
5869 +                            int curr_alt_set, int alt_rssi_avg,
5870 +                            int main_rssi_avg)
5871 +{
5872 +       switch (curr_alt_set) {
5873 +       case ATH_ANT_DIV_COMB_LNA2:
5874 +               antcomb->rssi_lna2 = alt_rssi_avg;
5875 +               antcomb->rssi_lna1 = main_rssi_avg;
5876 +               antcomb->scan = true;
5877 +               /* set to A+B */
5878 +               conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
5879 +               conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
5880 +               break;
5881 +       case ATH_ANT_DIV_COMB_LNA1:
5882 +               antcomb->rssi_lna1 = alt_rssi_avg;
5883 +               antcomb->rssi_lna2 = main_rssi_avg;
5884 +               antcomb->scan = true;
5885 +               /* set to A+B */
5886 +               conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
5887 +               conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
5888 +               break;
5889 +       case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
5890 +               antcomb->rssi_add = alt_rssi_avg;
5891 +               antcomb->scan = true;
5892 +               /* set to A-B */
5893 +               conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
5894 +               break;
5895 +       case ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2:
5896 +               antcomb->rssi_sub = alt_rssi_avg;
5897 +               antcomb->scan = false;
5898 +               if (antcomb->rssi_lna2 >
5899 +                   (antcomb->rssi_lna1 + ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)) {
5900 +                       /* use LNA2 as main LNA */
5901 +                       if ((antcomb->rssi_add > antcomb->rssi_lna1) &&
5902 +                           (antcomb->rssi_add > antcomb->rssi_sub)) {
5903 +                               /* set to A+B */
5904 +                               conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
5905 +                               conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
5906 +                       } else if (antcomb->rssi_sub >
5907 +                                  antcomb->rssi_lna1) {
5908 +                               /* set to A-B */
5909 +                               conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
5910 +                               conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
5911 +                       } else {
5912 +                               /* set to LNA1 */
5913 +                               conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
5914 +                               conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1;
5915 +                       }
5916 +               } else {
5917 +                       /* use LNA1 as main LNA */
5918 +                       if ((antcomb->rssi_add > antcomb->rssi_lna2) &&
5919 +                           (antcomb->rssi_add > antcomb->rssi_sub)) {
5920 +                               /* set to A+B */
5921 +                               conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
5922 +                               conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
5923 +                       } else if (antcomb->rssi_sub >
5924 +                                  antcomb->rssi_lna1) {
5925 +                               /* set to A-B */
5926 +                               conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
5927 +                               conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
5928 +                       } else {
5929 +                               /* set to LNA2 */
5930 +                               conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
5931 +                               conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA2;
5932 +                       }
5933 +               }
5934 +               break;
5935 +       default:
5936 +               break;
5937 +       }
5938 +}
5939 +
5940 +static bool ath_ant_try_switch(struct ath_hw_antcomb_conf *div_ant_conf,
5941 +                              struct ath_ant_comb *antcomb,
5942 +                              int alt_ratio, int alt_rssi_avg,
5943 +                              int main_rssi_avg, int curr_main_set,
5944 +                              int curr_alt_set)
5945 +{
5946 +       bool ret = false;
5947 +
5948 +       if (ath_ant_div_comb_alt_check(div_ant_conf, antcomb, alt_ratio,
5949 +                                      alt_rssi_avg, main_rssi_avg)) {
5950 +               if (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) {
5951 +                       /*
5952 +                        * Switch main and alt LNA.
5953 +                        */
5954 +                       div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
5955 +                       div_ant_conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1;
5956 +               } else if (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) {
5957 +                       div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
5958 +                       div_ant_conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA2;
5959 +               }
5960 +
5961 +               ret = true;
5962 +       } else if ((curr_alt_set != ATH_ANT_DIV_COMB_LNA1) &&
5963 +                  (curr_alt_set != ATH_ANT_DIV_COMB_LNA2)) {
5964 +               /*
5965 +                 Set alt to another LNA.
5966 +               */
5967 +               if (curr_main_set == ATH_ANT_DIV_COMB_LNA2)
5968 +                       div_ant_conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA1;
5969 +               else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1)
5970 +                       div_ant_conf->alt_lna_conf = ATH_ANT_DIV_COMB_LNA2;
5971 +
5972 +               ret = true;
5973 +       }
5974 +
5975 +       return ret;
5976 +}
5977 +
5978 +static bool ath_ant_short_scan_check(struct ath_ant_comb *antcomb)
5979 +{
5980 +       int alt_ratio;
5981 +
5982 +       if (!antcomb->scan || !antcomb->alt_good)
5983 +               return false;
5984 +
5985 +       if (time_after(jiffies, antcomb->scan_start_time +
5986 +                      msecs_to_jiffies(ATH_ANT_DIV_COMB_SHORT_SCAN_INTR)))
5987 +               return true;
5988 +
5989 +       if (antcomb->total_pkt_count == ATH_ANT_DIV_COMB_SHORT_SCAN_PKTCOUNT) {
5990 +               alt_ratio = ((antcomb->alt_recv_cnt * 100) /
5991 +                            antcomb->total_pkt_count);
5992 +               if (alt_ratio < antcomb->ant_ratio)
5993 +                       return true;
5994 +       }
5995 +
5996 +       return false;
5997 +}
5998 +
5999  void ath_ant_comb_scan(struct ath_softc *sc, struct ath_rx_status *rs)
6000  {
6001         struct ath_hw_antcomb_conf div_ant_conf;
6002 @@ -549,41 +747,46 @@ void ath_ant_comb_scan(struct ath_softc 
6003         int main_rssi = rs->rs_rssi_ctl0;
6004         int alt_rssi = rs->rs_rssi_ctl1;
6005         int rx_ant_conf,  main_ant_conf;
6006 -       bool short_scan = false;
6007 +       bool short_scan = false, ret;
6008  
6009         rx_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_CURRENT_SHIFT) &
6010                        ATH_ANT_RX_MASK;
6011         main_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_MAIN_SHIFT) &
6012                          ATH_ANT_RX_MASK;
6013  
6014 +       if (alt_rssi >= antcomb->low_rssi_thresh) {
6015 +               antcomb->ant_ratio = ATH_ANT_DIV_COMB_ALT_ANT_RATIO;
6016 +               antcomb->ant_ratio2 = ATH_ANT_DIV_COMB_ALT_ANT_RATIO2;
6017 +       } else {
6018 +               antcomb->ant_ratio = ATH_ANT_DIV_COMB_ALT_ANT_RATIO_LOW_RSSI;
6019 +               antcomb->ant_ratio2 = ATH_ANT_DIV_COMB_ALT_ANT_RATIO2_LOW_RSSI;
6020 +       }
6021 +
6022         /* Record packet only when both main_rssi and  alt_rssi is positive */
6023         if (main_rssi > 0 && alt_rssi > 0) {
6024                 antcomb->total_pkt_count++;
6025                 antcomb->main_total_rssi += main_rssi;
6026                 antcomb->alt_total_rssi  += alt_rssi;
6027 +
6028                 if (main_ant_conf == rx_ant_conf)
6029                         antcomb->main_recv_cnt++;
6030                 else
6031                         antcomb->alt_recv_cnt++;
6032         }
6033  
6034 -       /* Short scan check */
6035 -       if (antcomb->scan && antcomb->alt_good) {
6036 -               if (time_after(jiffies, antcomb->scan_start_time +
6037 -                   msecs_to_jiffies(ATH_ANT_DIV_COMB_SHORT_SCAN_INTR)))
6038 -                       short_scan = true;
6039 -               else
6040 -                       if (antcomb->total_pkt_count ==
6041 -                           ATH_ANT_DIV_COMB_SHORT_SCAN_PKTCOUNT) {
6042 -                               alt_ratio = ((antcomb->alt_recv_cnt * 100) /
6043 -                                           antcomb->total_pkt_count);
6044 -                               if (alt_ratio < ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
6045 -                                       short_scan = true;
6046 -                       }
6047 +       if (main_ant_conf == rx_ant_conf) {
6048 +               ANT_STAT_INC(ANT_MAIN, recv_cnt);
6049 +               ANT_LNA_INC(ANT_MAIN, rx_ant_conf);
6050 +       } else {
6051 +               ANT_STAT_INC(ANT_ALT, recv_cnt);
6052 +               ANT_LNA_INC(ANT_ALT, rx_ant_conf);
6053         }
6054  
6055 +       /* Short scan check */
6056 +       short_scan = ath_ant_short_scan_check(antcomb);
6057 +
6058         if (((antcomb->total_pkt_count < ATH_ANT_DIV_COMB_MAX_PKTCOUNT) ||
6059 -           rs->rs_moreaggr) && !short_scan)
6060 +            rs->rs_moreaggr) && !short_scan)
6061                 return;
6062  
6063         if (antcomb->total_pkt_count) {
6064 @@ -595,15 +798,13 @@ void ath_ant_comb_scan(struct ath_softc 
6065                                  antcomb->total_pkt_count);
6066         }
6067  
6068 -
6069         ath9k_hw_antdiv_comb_conf_get(sc->sc_ah, &div_ant_conf);
6070         curr_alt_set = div_ant_conf.alt_lna_conf;
6071         curr_main_set = div_ant_conf.main_lna_conf;
6072 -
6073         antcomb->count++;
6074  
6075         if (antcomb->count == ATH_ANT_DIV_COMB_MAX_COUNT) {
6076 -               if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
6077 +               if (alt_ratio > antcomb->ant_ratio) {
6078                         ath_lnaconf_alt_good_scan(antcomb, div_ant_conf,
6079                                                   main_rssi_avg);
6080                         antcomb->alt_good = true;
6081 @@ -617,153 +818,47 @@ void ath_ant_comb_scan(struct ath_softc 
6082         }
6083  
6084         if (!antcomb->scan) {
6085 -               if (ath_ant_div_comb_alt_check(div_ant_conf.div_group,
6086 -                                       alt_ratio, curr_main_set, curr_alt_set,
6087 -                                       alt_rssi_avg, main_rssi_avg)) {
6088 -                       if (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) {
6089 -                               /* Switch main and alt LNA */
6090 -                               div_ant_conf.main_lna_conf =
6091 -                                               ATH_ANT_DIV_COMB_LNA2;
6092 -                               div_ant_conf.alt_lna_conf  =
6093 -                                               ATH_ANT_DIV_COMB_LNA1;
6094 -                       } else if (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) {
6095 -                               div_ant_conf.main_lna_conf =
6096 -                                               ATH_ANT_DIV_COMB_LNA1;
6097 -                               div_ant_conf.alt_lna_conf  =
6098 -                                               ATH_ANT_DIV_COMB_LNA2;
6099 -                       }
6100 -
6101 -                       goto div_comb_done;
6102 -               } else if ((curr_alt_set != ATH_ANT_DIV_COMB_LNA1) &&
6103 -                          (curr_alt_set != ATH_ANT_DIV_COMB_LNA2)) {
6104 -                       /* Set alt to another LNA */
6105 -                       if (curr_main_set == ATH_ANT_DIV_COMB_LNA2)
6106 -                               div_ant_conf.alt_lna_conf =
6107 -                                               ATH_ANT_DIV_COMB_LNA1;
6108 -                       else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1)
6109 -                               div_ant_conf.alt_lna_conf =
6110 -                                               ATH_ANT_DIV_COMB_LNA2;
6111 -
6112 -                       goto div_comb_done;
6113 -               }
6114 -
6115 -               if ((alt_rssi_avg < (main_rssi_avg +
6116 -                                    div_ant_conf.lna1_lna2_delta)))
6117 +               ret = ath_ant_try_switch(&div_ant_conf, antcomb, alt_ratio,
6118 +                                        alt_rssi_avg, main_rssi_avg,
6119 +                                        curr_main_set, curr_alt_set);
6120 +               if (ret)
6121                         goto div_comb_done;
6122         }
6123  
6124 +       if (!antcomb->scan &&
6125 +           (alt_rssi_avg < (main_rssi_avg + div_ant_conf.lna1_lna2_delta)))
6126 +               goto div_comb_done;
6127 +
6128         if (!antcomb->scan_not_start) {
6129 -               switch (curr_alt_set) {
6130 -               case ATH_ANT_DIV_COMB_LNA2:
6131 -                       antcomb->rssi_lna2 = alt_rssi_avg;
6132 -                       antcomb->rssi_lna1 = main_rssi_avg;
6133 -                       antcomb->scan = true;
6134 -                       /* set to A+B */
6135 -                       div_ant_conf.main_lna_conf =
6136 -                               ATH_ANT_DIV_COMB_LNA1;
6137 -                       div_ant_conf.alt_lna_conf  =
6138 -                               ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
6139 -                       break;
6140 -               case ATH_ANT_DIV_COMB_LNA1:
6141 -                       antcomb->rssi_lna1 = alt_rssi_avg;
6142 -                       antcomb->rssi_lna2 = main_rssi_avg;
6143 -                       antcomb->scan = true;
6144 -                       /* set to A+B */
6145 -                       div_ant_conf.main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
6146 -                       div_ant_conf.alt_lna_conf  =
6147 -                               ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
6148 -                       break;
6149 -               case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
6150 -                       antcomb->rssi_add = alt_rssi_avg;
6151 -                       antcomb->scan = true;
6152 -                       /* set to A-B */
6153 -                       div_ant_conf.alt_lna_conf =
6154 -                               ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
6155 -                       break;
6156 -               case ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2:
6157 -                       antcomb->rssi_sub = alt_rssi_avg;
6158 -                       antcomb->scan = false;
6159 -                       if (antcomb->rssi_lna2 >
6160 -                           (antcomb->rssi_lna1 +
6161 -                           ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)) {
6162 -                               /* use LNA2 as main LNA */
6163 -                               if ((antcomb->rssi_add > antcomb->rssi_lna1) &&
6164 -                                   (antcomb->rssi_add > antcomb->rssi_sub)) {
6165 -                                       /* set to A+B */
6166 -                                       div_ant_conf.main_lna_conf =
6167 -                                               ATH_ANT_DIV_COMB_LNA2;
6168 -                                       div_ant_conf.alt_lna_conf  =
6169 -                                               ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
6170 -                               } else if (antcomb->rssi_sub >
6171 -                                          antcomb->rssi_lna1) {
6172 -                                       /* set to A-B */
6173 -                                       div_ant_conf.main_lna_conf =
6174 -                                               ATH_ANT_DIV_COMB_LNA2;
6175 -                                       div_ant_conf.alt_lna_conf =
6176 -                                               ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
6177 -                               } else {
6178 -                                       /* set to LNA1 */
6179 -                                       div_ant_conf.main_lna_conf =
6180 -                                               ATH_ANT_DIV_COMB_LNA2;
6181 -                                       div_ant_conf.alt_lna_conf =
6182 -                                               ATH_ANT_DIV_COMB_LNA1;
6183 -                               }
6184 -                       } else {
6185 -                               /* use LNA1 as main LNA */
6186 -                               if ((antcomb->rssi_add > antcomb->rssi_lna2) &&
6187 -                                   (antcomb->rssi_add > antcomb->rssi_sub)) {
6188 -                                       /* set to A+B */
6189 -                                       div_ant_conf.main_lna_conf =
6190 -                                               ATH_ANT_DIV_COMB_LNA1;
6191 -                                       div_ant_conf.alt_lna_conf  =
6192 -                                               ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
6193 -                               } else if (antcomb->rssi_sub >
6194 -                                          antcomb->rssi_lna1) {
6195 -                                       /* set to A-B */
6196 -                                       div_ant_conf.main_lna_conf =
6197 -                                               ATH_ANT_DIV_COMB_LNA1;
6198 -                                       div_ant_conf.alt_lna_conf =
6199 -                                               ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
6200 -                               } else {
6201 -                                       /* set to LNA2 */
6202 -                                       div_ant_conf.main_lna_conf =
6203 -                                               ATH_ANT_DIV_COMB_LNA1;
6204 -                                       div_ant_conf.alt_lna_conf =
6205 -                                               ATH_ANT_DIV_COMB_LNA2;
6206 -                               }
6207 -                       }
6208 -                       break;
6209 -               default:
6210 -                       break;
6211 -               }
6212 +               ath_ant_try_scan(antcomb, &div_ant_conf, curr_alt_set,
6213 +                                alt_rssi_avg, main_rssi_avg);
6214         } else {
6215                 if (!antcomb->alt_good) {
6216                         antcomb->scan_not_start = false;
6217                         /* Set alt to another LNA */
6218                         if (curr_main_set == ATH_ANT_DIV_COMB_LNA2) {
6219                                 div_ant_conf.main_lna_conf =
6220 -                                               ATH_ANT_DIV_COMB_LNA2;
6221 +                                       ATH_ANT_DIV_COMB_LNA2;
6222                                 div_ant_conf.alt_lna_conf =
6223 -                                               ATH_ANT_DIV_COMB_LNA1;
6224 +                                       ATH_ANT_DIV_COMB_LNA1;
6225                         } else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1) {
6226                                 div_ant_conf.main_lna_conf =
6227 -                                               ATH_ANT_DIV_COMB_LNA1;
6228 +                                       ATH_ANT_DIV_COMB_LNA1;
6229                                 div_ant_conf.alt_lna_conf =
6230 -                                               ATH_ANT_DIV_COMB_LNA2;
6231 +                                       ATH_ANT_DIV_COMB_LNA2;
6232                         }
6233                         goto div_comb_done;
6234                 }
6235 +               ath_select_ant_div_from_quick_scan(antcomb, &div_ant_conf,
6236 +                                                  main_rssi_avg, alt_rssi_avg,
6237 +                                                  alt_ratio);
6238 +               antcomb->quick_scan_cnt++;
6239         }
6240  
6241 -       ath_select_ant_div_from_quick_scan(antcomb, &div_ant_conf,
6242 -                                          main_rssi_avg, alt_rssi_avg,
6243 -                                          alt_ratio);
6244 -
6245 -       antcomb->quick_scan_cnt++;
6246 -
6247  div_comb_done:
6248         ath_ant_div_conf_fast_divbias(&div_ant_conf, antcomb, alt_ratio);
6249         ath9k_hw_antdiv_comb_conf_set(sc->sc_ah, &div_ant_conf);
6250 +       ath9k_debug_stat_ant(sc, &div_ant_conf, main_rssi_avg, alt_rssi_avg);
6251  
6252         antcomb->scan_start_time = jiffies;
6253         antcomb->total_pkt_count = 0;
6254 @@ -772,26 +867,3 @@ div_comb_done:
6255         antcomb->main_recv_cnt = 0;
6256         antcomb->alt_recv_cnt = 0;
6257  }
6258 -
6259 -void ath_ant_comb_update(struct ath_softc *sc)
6260 -{
6261 -       struct ath_hw *ah = sc->sc_ah;
6262 -       struct ath_common *common = ath9k_hw_common(ah);
6263 -       struct ath_hw_antcomb_conf div_ant_conf;
6264 -       u8 lna_conf;
6265 -
6266 -       ath9k_hw_antdiv_comb_conf_get(ah, &div_ant_conf);
6267 -
6268 -       if (sc->ant_rx == 1)
6269 -               lna_conf = ATH_ANT_DIV_COMB_LNA1;
6270 -       else
6271 -               lna_conf = ATH_ANT_DIV_COMB_LNA2;
6272 -
6273 -       div_ant_conf.main_lna_conf = lna_conf;
6274 -       div_ant_conf.alt_lna_conf = lna_conf;
6275 -
6276 -       ath9k_hw_antdiv_comb_conf_set(ah, &div_ant_conf);
6277 -
6278 -       if (common->antenna_diversity)
6279 -               ath9k_hw_antctrl_shared_chain_lnadiv(ah, true);
6280 -}
6281 --- a/drivers/net/wireless/ath/ath9k/ar5008_phy.c
6282 +++ b/drivers/net/wireless/ath/ath9k/ar5008_phy.c
6283 @@ -610,7 +610,15 @@ static void ar5008_hw_override_ini(struc
6284         REG_SET_BIT(ah, AR_DIAG_SW, (AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT));
6285  
6286         if (AR_SREV_9280_20_OR_LATER(ah)) {
6287 -               val = REG_READ(ah, AR_PCU_MISC_MODE2);
6288 +               /*
6289 +                * For AR9280 and above, there is a new feature that allows
6290 +                * Multicast search based on both MAC Address and Key ID.
6291 +                * By default, this feature is enabled. But since the driver
6292 +                * is not using this feature, we switch it off; otherwise
6293 +                * multicast search based on MAC addr only will fail.
6294 +                */
6295 +               val = REG_READ(ah, AR_PCU_MISC_MODE2) &
6296 +                       (~AR_ADHOC_MCAST_KEYID_ENABLE);
6297  
6298                 if (!AR_SREV_9271(ah))
6299                         val &= ~AR_PCU_MISC_MODE2_HWWAR1;
6300 --- a/drivers/net/wireless/ath/ath9k/ar9002_phy.c
6301 +++ b/drivers/net/wireless/ath/ath9k/ar9002_phy.c
6302 @@ -555,6 +555,69 @@ static void ar9002_hw_antdiv_comb_conf_s
6303         REG_WRITE(ah, AR_PHY_MULTICHAIN_GAIN_CTL, regval);
6304  }
6305  
6306 +#ifdef CPTCFG_ATH9K_BTCOEX_SUPPORT
6307 +
6308 +static void ar9002_hw_set_bt_ant_diversity(struct ath_hw *ah, bool enable)
6309 +{
6310 +       struct ath_btcoex_hw *btcoex = &ah->btcoex_hw;
6311 +       u8 antdiv_ctrl1, antdiv_ctrl2;
6312 +       u32 regval;
6313 +
6314 +       if (enable) {
6315 +               antdiv_ctrl1 = ATH_BT_COEX_ANTDIV_CONTROL1_ENABLE;
6316 +               antdiv_ctrl2 = ATH_BT_COEX_ANTDIV_CONTROL2_ENABLE;
6317 +
6318 +               /*
6319 +                * Don't disable BT ant to allow BB to control SWCOM.
6320 +                */
6321 +               btcoex->bt_coex_mode2 &= (~(AR_BT_DISABLE_BT_ANT));
6322 +               REG_WRITE(ah, AR_BT_COEX_MODE2, btcoex->bt_coex_mode2);
6323 +
6324 +               REG_WRITE(ah, AR_PHY_SWITCH_COM, ATH_BT_COEX_ANT_DIV_SWITCH_COM);
6325 +               REG_RMW(ah, AR_PHY_SWITCH_CHAIN_0, 0, 0xf0000000);
6326 +       } else {
6327 +               /*
6328 +                * Disable antenna diversity, use LNA1 only.
6329 +                */
6330 +               antdiv_ctrl1 = ATH_BT_COEX_ANTDIV_CONTROL1_FIXED_A;
6331 +               antdiv_ctrl2 = ATH_BT_COEX_ANTDIV_CONTROL2_FIXED_A;
6332 +
6333 +               /*
6334 +                * Disable BT Ant. to allow concurrent BT and WLAN receive.
6335 +                */
6336 +               btcoex->bt_coex_mode2 |= AR_BT_DISABLE_BT_ANT;
6337 +               REG_WRITE(ah, AR_BT_COEX_MODE2, btcoex->bt_coex_mode2);
6338 +
6339 +               /*
6340 +                * Program SWCOM table to make sure RF switch always parks
6341 +                * at BT side.
6342 +                */
6343 +               REG_WRITE(ah, AR_PHY_SWITCH_COM, 0);
6344 +               REG_RMW(ah, AR_PHY_SWITCH_CHAIN_0, 0, 0xf0000000);
6345 +       }
6346 +
6347 +       regval = REG_READ(ah, AR_PHY_MULTICHAIN_GAIN_CTL);
6348 +       regval &= (~(AR_PHY_9285_ANT_DIV_CTL_ALL));
6349 +        /*
6350 +        * Clear ant_fast_div_bias [14:9] since for WB195,
6351 +        * the main LNA is always LNA1.
6352 +        */
6353 +       regval &= (~(AR_PHY_9285_FAST_DIV_BIAS));
6354 +       regval |= SM(antdiv_ctrl1, AR_PHY_9285_ANT_DIV_CTL);
6355 +       regval |= SM(antdiv_ctrl2, AR_PHY_9285_ANT_DIV_ALT_LNACONF);
6356 +       regval |= SM((antdiv_ctrl2 >> 2), AR_PHY_9285_ANT_DIV_MAIN_LNACONF);
6357 +       regval |= SM((antdiv_ctrl1 >> 1), AR_PHY_9285_ANT_DIV_ALT_GAINTB);
6358 +       regval |= SM((antdiv_ctrl1 >> 2), AR_PHY_9285_ANT_DIV_MAIN_GAINTB);
6359 +       REG_WRITE(ah, AR_PHY_MULTICHAIN_GAIN_CTL, regval);
6360 +
6361 +       regval = REG_READ(ah, AR_PHY_CCK_DETECT);
6362 +       regval &= (~AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV);
6363 +       regval |= SM((antdiv_ctrl1 >> 3), AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV);
6364 +       REG_WRITE(ah, AR_PHY_CCK_DETECT, regval);
6365 +}
6366 +
6367 +#endif
6368 +
6369  static void ar9002_hw_spectral_scan_config(struct ath_hw *ah,
6370                                     struct ath_spec_scan *param)
6371  {
6372 @@ -634,5 +697,9 @@ void ar9002_hw_attach_phy_ops(struct ath
6373         ops->spectral_scan_trigger = ar9002_hw_spectral_scan_trigger;
6374         ops->spectral_scan_wait = ar9002_hw_spectral_scan_wait;
6375  
6376 +#ifdef CPTCFG_ATH9K_BTCOEX_SUPPORT
6377 +       ops->set_bt_ant_diversity = ar9002_hw_set_bt_ant_diversity;
6378 +#endif
6379 +
6380         ar9002_hw_set_nf_limits(ah);
6381  }
6382 --- a/drivers/net/wireless/ath/ath9k/ar9002_phy.h
6383 +++ b/drivers/net/wireless/ath/ath9k/ar9002_phy.h
6384 @@ -317,13 +317,15 @@
6385  #define AR_PHY_9285_ANT_DIV_ALT_GAINTB_S    29
6386  #define AR_PHY_9285_ANT_DIV_MAIN_GAINTB     0x40000000
6387  #define AR_PHY_9285_ANT_DIV_MAIN_GAINTB_S   30
6388 -#define AR_PHY_9285_ANT_DIV_LNA1            2
6389 -#define AR_PHY_9285_ANT_DIV_LNA2            1
6390 -#define AR_PHY_9285_ANT_DIV_LNA1_PLUS_LNA2  3
6391 -#define AR_PHY_9285_ANT_DIV_LNA1_MINUS_LNA2 0
6392  #define AR_PHY_9285_ANT_DIV_GAINTB_0        0
6393  #define AR_PHY_9285_ANT_DIV_GAINTB_1        1
6394  
6395 +#define ATH_BT_COEX_ANTDIV_CONTROL1_ENABLE  0x0b
6396 +#define ATH_BT_COEX_ANTDIV_CONTROL2_ENABLE  0x09
6397 +#define ATH_BT_COEX_ANTDIV_CONTROL1_FIXED_A 0x04
6398 +#define ATH_BT_COEX_ANTDIV_CONTROL2_FIXED_A 0x09
6399 +#define ATH_BT_COEX_ANT_DIV_SWITCH_COM      0x66666666
6400 +
6401  #define AR_PHY_EXT_CCA0             0x99b8
6402  #define AR_PHY_EXT_CCA0_THRESH62    0x000000FF
6403  #define AR_PHY_EXT_CCA0_THRESH62_S  0
6404 --- a/drivers/net/wireless/ath/ath9k/ar9003_eeprom.c
6405 +++ b/drivers/net/wireless/ath/ath9k/ar9003_eeprom.c
6406 @@ -3541,13 +3541,12 @@ static u16 ar9003_switch_com_spdt_get(st
6407         return le16_to_cpu(ar9003_modal_header(ah, is2ghz)->switchcomspdt);
6408  }
6409  
6410 -
6411 -static u32 ar9003_hw_ant_ctrl_common_get(struct ath_hw *ah, bool is2ghz)
6412 +u32 ar9003_hw_ant_ctrl_common_get(struct ath_hw *ah, bool is2ghz)
6413  {
6414         return le32_to_cpu(ar9003_modal_header(ah, is2ghz)->antCtrlCommon);
6415  }
6416  
6417 -static u32 ar9003_hw_ant_ctrl_common_2_get(struct ath_hw *ah, bool is2ghz)
6418 +u32 ar9003_hw_ant_ctrl_common_2_get(struct ath_hw *ah, bool is2ghz)
6419  {
6420         return le32_to_cpu(ar9003_modal_header(ah, is2ghz)->antCtrlCommon2);
6421  }
6422 @@ -3561,6 +3560,7 @@ static u16 ar9003_hw_ant_ctrl_chain_get(
6423  
6424  static void ar9003_hw_ant_ctrl_apply(struct ath_hw *ah, bool is2ghz)
6425  {
6426 +       struct ath_common *common = ath9k_hw_common(ah);
6427         struct ath9k_hw_capabilities *pCap = &ah->caps;
6428         int chain;
6429         u32 regval, value, gpio;
6430 @@ -3614,6 +3614,11 @@ static void ar9003_hw_ant_ctrl_apply(str
6431         }
6432  
6433         value = ar9003_hw_ant_ctrl_common_2_get(ah, is2ghz);
6434 +       if (AR_SREV_9485(ah) && common->bt_ant_diversity) {
6435 +               regval &= ~AR_SWITCH_TABLE_COM2_ALL;
6436 +               regval |= ah->config.ant_ctrl_comm2g_switch_enable;
6437 +
6438 +       }
6439         REG_RMW_FIELD(ah, AR_PHY_SWITCH_COM_2, AR_SWITCH_TABLE_COM2_ALL, value);
6440  
6441         if ((AR_SREV_9462(ah)) && (ah->rxchainmask == 0x2)) {
6442 @@ -3645,8 +3650,11 @@ static void ar9003_hw_ant_ctrl_apply(str
6443                 regval &= (~AR_PHY_ANT_DIV_LNADIV);
6444                 regval |= ((value >> 6) & 0x1) << AR_PHY_ANT_DIV_LNADIV_S;
6445  
6446 +               if (AR_SREV_9485(ah) && common->bt_ant_diversity)
6447 +                       regval |= AR_ANT_DIV_ENABLE;
6448 +
6449                 if (AR_SREV_9565(ah)) {
6450 -                       if (ah->shared_chain_lnadiv) {
6451 +                       if (common->bt_ant_diversity) {
6452                                 regval |= (1 << AR_PHY_ANT_SW_RX_PROT_S);
6453                         } else {
6454                                 regval &= ~(1 << AR_PHY_ANT_DIV_LNADIV_S);
6455 @@ -3656,10 +3664,14 @@ static void ar9003_hw_ant_ctrl_apply(str
6456  
6457                 REG_WRITE(ah, AR_PHY_MC_GAIN_CTRL, regval);
6458  
6459 -               /*enable fast_div */
6460 +               /* enable fast_div */
6461                 regval = REG_READ(ah, AR_PHY_CCK_DETECT);
6462                 regval &= (~AR_FAST_DIV_ENABLE);
6463                 regval |= ((value >> 7) & 0x1) << AR_FAST_DIV_ENABLE_S;
6464 +
6465 +               if (AR_SREV_9485(ah) && common->bt_ant_diversity)
6466 +                       regval |= AR_FAST_DIV_ENABLE;
6467 +
6468                 REG_WRITE(ah, AR_PHY_CCK_DETECT, regval);
6469  
6470                 if (pCap->hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) {
6471 @@ -3673,9 +3685,9 @@ static void ar9003_hw_ant_ctrl_apply(str
6472                                      AR_PHY_ANT_DIV_ALT_GAINTB |
6473                                      AR_PHY_ANT_DIV_MAIN_GAINTB));
6474                         /* by default use LNA1 for the main antenna */
6475 -                       regval |= (AR_PHY_ANT_DIV_LNA1 <<
6476 +                       regval |= (ATH_ANT_DIV_COMB_LNA1 <<
6477                                    AR_PHY_ANT_DIV_MAIN_LNACONF_S);
6478 -                       regval |= (AR_PHY_ANT_DIV_LNA2 <<
6479 +                       regval |= (ATH_ANT_DIV_COMB_LNA2 <<
6480                                    AR_PHY_ANT_DIV_ALT_LNACONF_S);
6481                         REG_WRITE(ah, AR_PHY_MC_GAIN_CTRL, regval);
6482                 }
6483 @@ -3813,6 +3825,11 @@ static void ar9003_hw_atten_apply(struct
6484                         else
6485                                 value = ar9003_hw_atten_chain_get_margin(ah, i, chan);
6486  
6487 +                       if (ah->config.alt_mingainidx)
6488 +                               REG_RMW_FIELD(ah, AR_PHY_EXT_ATTEN_CTL_0,
6489 +                                             AR_PHY_EXT_ATTEN_CTL_XATTEN1_MARGIN,
6490 +                                             value);
6491 +
6492                         REG_RMW_FIELD(ah, ext_atten_reg[i],
6493                                       AR_PHY_EXT_ATTEN_CTL_XATTEN1_MARGIN,
6494                                       value);
6495 --- a/drivers/net/wireless/ath/ath9k/ar9003_eeprom.h
6496 +++ b/drivers/net/wireless/ath/ath9k/ar9003_eeprom.h
6497 @@ -334,6 +334,8 @@ struct ar9300_eeprom {
6498  
6499  s32 ar9003_hw_get_tx_gain_idx(struct ath_hw *ah);
6500  s32 ar9003_hw_get_rx_gain_idx(struct ath_hw *ah);
6501 +u32 ar9003_hw_ant_ctrl_common_get(struct ath_hw *ah, bool is2ghz);
6502 +u32 ar9003_hw_ant_ctrl_common_2_get(struct ath_hw *ah, bool is2ghz);
6503  
6504  u8 *ar9003_get_spur_chan_ptr(struct ath_hw *ah, bool is_2ghz);
6505  
6506 --- a/drivers/net/wireless/ath/ath9k/ar9003_phy.h
6507 +++ b/drivers/net/wireless/ath/ath9k/ar9003_phy.h
6508 @@ -148,6 +148,8 @@
6509  #define AR_PHY_SFCORR_SPUR_SUBCHNL_SD_S   28
6510  #define AR_PHY_EXT_CCA_THRESH62 0x007F0000
6511  #define AR_PHY_EXT_CCA_THRESH62_S       16
6512 +#define AR_PHY_EXTCHN_PWRTHR1_ANT_DIV_ALT_ANT_MINGAINIDX    0x0000FF00
6513 +#define AR_PHY_EXTCHN_PWRTHR1_ANT_DIV_ALT_ANT_MINGAINIDX_S  8
6514  #define AR_PHY_EXT_MINCCA_PWR   0x01FF0000
6515  #define AR_PHY_EXT_MINCCA_PWR_S 16
6516  #define AR_PHY_EXT_CYCPWR_THR1 0x0000FE00L
6517 @@ -296,11 +298,6 @@
6518  #define AR_PHY_ANT_DIV_MAIN_GAINTB              0x40000000
6519  #define AR_PHY_ANT_DIV_MAIN_GAINTB_S            30
6520  
6521 -#define AR_PHY_ANT_DIV_LNA1_MINUS_LNA2          0x0
6522 -#define AR_PHY_ANT_DIV_LNA2                     0x1
6523 -#define AR_PHY_ANT_DIV_LNA1                     0x2
6524 -#define AR_PHY_ANT_DIV_LNA1_PLUS_LNA2           0x3
6525 -
6526  #define AR_PHY_EXTCHN_PWRTHR1   (AR_AGC_BASE + 0x2c)
6527  #define AR_PHY_EXT_CHN_WIN      (AR_AGC_BASE + 0x30)
6528  #define AR_PHY_20_40_DET_THR    (AR_AGC_BASE + 0x34)
6529 --- a/drivers/net/wireless/ath/ath9k/debug.h
6530 +++ b/drivers/net/wireless/ath/ath9k/debug.h
6531 @@ -28,9 +28,13 @@ struct fft_sample_tlv;
6532  #ifdef CPTCFG_ATH9K_DEBUGFS
6533  #define TX_STAT_INC(q, c) sc->debug.stats.txstats[q].c++
6534  #define RESET_STAT_INC(sc, type) sc->debug.stats.reset[type]++
6535 +#define ANT_STAT_INC(i, c) sc->debug.stats.ant_stats[i].c++
6536 +#define ANT_LNA_INC(i, c) sc->debug.stats.ant_stats[i].lna_recv_cnt[c]++;
6537  #else
6538  #define TX_STAT_INC(q, c) do { } while (0)
6539  #define RESET_STAT_INC(sc, type) do { } while (0)
6540 +#define ANT_STAT_INC(i, c) do { } while (0)
6541 +#define ANT_LNA_INC(i, c) do { } while (0)
6542  #endif
6543  
6544  enum ath_reset_type {
6545 @@ -243,11 +247,22 @@ struct ath_rx_stats {
6546         u32 rx_spectral;
6547  };
6548  
6549 +#define ANT_MAIN 0
6550 +#define ANT_ALT  1
6551 +
6552 +struct ath_antenna_stats {
6553 +       u32 recv_cnt;
6554 +       u32 rssi_avg;
6555 +       u32 lna_recv_cnt[4];
6556 +       u32 lna_attempt_cnt[4];
6557 +};
6558 +
6559  struct ath_stats {
6560         struct ath_interrupt_stats istats;
6561         struct ath_tx_stats txstats[ATH9K_NUM_TX_QUEUES];
6562         struct ath_rx_stats rxstats;
6563         struct ath_dfs_stats dfs_stats;
6564 +       struct ath_antenna_stats ant_stats[2];
6565         u32 reset[__RESET_TYPE_MAX];
6566  };
6567  
6568 @@ -281,10 +296,11 @@ void ath9k_sta_remove_debugfs(struct iee
6569                               struct ieee80211_vif *vif,
6570                               struct ieee80211_sta *sta,
6571                               struct dentry *dir);
6572 -
6573  void ath_debug_send_fft_sample(struct ath_softc *sc,
6574                                struct fft_sample_tlv *fft_sample);
6575 -
6576 +void ath9k_debug_stat_ant(struct ath_softc *sc,
6577 +                         struct ath_hw_antcomb_conf *div_ant_conf,
6578 +                         int main_rssi_avg, int alt_rssi_avg);
6579  #else
6580  
6581  #define RX_STAT_INC(c) /* NOP */
6582 @@ -297,12 +313,10 @@ static inline int ath9k_init_debug(struc
6583  static inline void ath9k_deinit_debug(struct ath_softc *sc)
6584  {
6585  }
6586 -
6587  static inline void ath_debug_stat_interrupt(struct ath_softc *sc,
6588                                             enum ath9k_int status)
6589  {
6590  }
6591 -
6592  static inline void ath_debug_stat_tx(struct ath_softc *sc,
6593                                      struct ath_buf *bf,
6594                                      struct ath_tx_status *ts,
6595 @@ -310,11 +324,16 @@ static inline void ath_debug_stat_tx(str
6596                                      unsigned int flags)
6597  {
6598  }
6599 -
6600  static inline void ath_debug_stat_rx(struct ath_softc *sc,
6601                                      struct ath_rx_status *rs)
6602  {
6603  }
6604 +static inline void ath9k_debug_stat_ant(struct ath_softc *sc,
6605 +                                       struct ath_hw_antcomb_conf *div_ant_conf,
6606 +                                       int main_rssi_avg, int alt_rssi_avg)
6607 +{
6608 +
6609 +}
6610  
6611  #endif /* CPTCFG_ATH9K_DEBUGFS */
6612  
6613 --- a/drivers/net/wireless/ath/ath9k/eeprom_4k.c
6614 +++ b/drivers/net/wireless/ath/ath9k/eeprom_4k.c
6615 @@ -812,6 +812,7 @@ static void ath9k_hw_4k_set_gain(struct 
6616  static void ath9k_hw_4k_set_board_values(struct ath_hw *ah,
6617                                          struct ath9k_channel *chan)
6618  {
6619 +       struct ath9k_hw_capabilities *pCap = &ah->caps;
6620         struct modal_eep_4k_header *pModal;
6621         struct ar5416_eeprom_4k *eep = &ah->eeprom.map4k;
6622         struct base_eep_header_4k *pBase = &eep->baseEepHeader;
6623 @@ -858,6 +859,24 @@ static void ath9k_hw_4k_set_board_values
6624  
6625                 REG_WRITE(ah, AR_PHY_CCK_DETECT, regVal);
6626                 regVal = REG_READ(ah, AR_PHY_CCK_DETECT);
6627 +
6628 +               if (pCap->hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) {
6629 +                       /*
6630 +                        * If diversity combining is enabled,
6631 +                        * set MAIN to LNA1 and ALT to LNA2 initially.
6632 +                        */
6633 +                       regVal = REG_READ(ah, AR_PHY_MULTICHAIN_GAIN_CTL);
6634 +                       regVal &= (~(AR_PHY_9285_ANT_DIV_MAIN_LNACONF |
6635 +                                    AR_PHY_9285_ANT_DIV_ALT_LNACONF));
6636 +
6637 +                       regVal |= (ATH_ANT_DIV_COMB_LNA1 <<
6638 +                                  AR_PHY_9285_ANT_DIV_MAIN_LNACONF_S);
6639 +                       regVal |= (ATH_ANT_DIV_COMB_LNA2 <<
6640 +                                  AR_PHY_9285_ANT_DIV_ALT_LNACONF_S);
6641 +                       regVal &= (~(AR_PHY_9285_FAST_DIV_BIAS));
6642 +                       regVal |= (0 << AR_PHY_9285_FAST_DIV_BIAS_S);
6643 +                       REG_WRITE(ah, AR_PHY_MULTICHAIN_GAIN_CTL, regVal);
6644 +               }
6645         }
6646  
6647         if (pModal->version >= 2) {
6648 --- a/drivers/net/wireless/ath/ath9k/hw-ops.h
6649 +++ b/drivers/net/wireless/ath/ath9k/hw-ops.h
6650 @@ -78,13 +78,16 @@ static inline void ath9k_hw_antdiv_comb_
6651         ath9k_hw_ops(ah)->antdiv_comb_conf_set(ah, antconf);
6652  }
6653  
6654 -static inline void ath9k_hw_antctrl_shared_chain_lnadiv(struct ath_hw *ah,
6655 -                                                       bool enable)
6656 +#ifdef CPTCFG_ATH9K_BTCOEX_SUPPORT
6657 +
6658 +static inline void ath9k_hw_set_bt_ant_diversity(struct ath_hw *ah, bool enable)
6659  {
6660 -       if (ath9k_hw_ops(ah)->antctrl_shared_chain_lnadiv)
6661 -               ath9k_hw_ops(ah)->antctrl_shared_chain_lnadiv(ah, enable);
6662 +       if (ath9k_hw_ops(ah)->set_bt_ant_diversity)
6663 +               ath9k_hw_ops(ah)->set_bt_ant_diversity(ah, enable);
6664  }
6665  
6666 +#endif
6667 +
6668  /* Private hardware call ops */
6669  
6670  /* PHY ops */
6671 --- a/drivers/net/wireless/ath/ath9k/hw.c
6672 +++ b/drivers/net/wireless/ath/ath9k/hw.c
6673 @@ -450,7 +450,6 @@ static void ath9k_hw_init_config(struct 
6674         ah->config.ack_6mb = 0x0;
6675         ah->config.cwm_ignore_extcca = 0;
6676         ah->config.pcie_clock_req = 0;
6677 -       ah->config.pcie_waen = 0;
6678         ah->config.analog_shiftreg = 1;
6679  
6680         for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
6681 @@ -1069,7 +1068,7 @@ void ath9k_hw_init_global_settings(struc
6682                 if (IS_CHAN_A_FAST_CLOCK(ah, chan))
6683                     tx_lat += 11;
6684  
6685 -               sifstime *= 2;
6686 +               sifstime = 32;
6687                 ack_offset = 16;
6688                 slottime = 13;
6689         } else if (IS_CHAN_QUARTER_RATE(chan)) {
6690 @@ -1079,7 +1078,7 @@ void ath9k_hw_init_global_settings(struc
6691                 if (IS_CHAN_A_FAST_CLOCK(ah, chan))
6692                     tx_lat += 22;
6693  
6694 -               sifstime *= 4;
6695 +               sifstime = 64;
6696                 ack_offset = 32;
6697                 slottime = 21;
6698         } else {
6699 @@ -1116,7 +1115,6 @@ void ath9k_hw_init_global_settings(struc
6700                 ctstimeout += 48 - sifstime - ah->slottime;
6701         }
6702  
6703 -
6704         ath9k_hw_set_sifs_time(ah, sifstime);
6705         ath9k_hw_setslottime(ah, slottime);
6706         ath9k_hw_set_ack_timeout(ah, acktimeout);
6707 @@ -1496,16 +1494,18 @@ static bool ath9k_hw_channel_change(stru
6708                                     struct ath9k_channel *chan)
6709  {
6710         struct ath_common *common = ath9k_hw_common(ah);
6711 +       struct ath9k_hw_capabilities *pCap = &ah->caps;
6712 +       bool band_switch = false, mode_diff = false;
6713 +       u8 ini_reloaded = 0;
6714         u32 qnum;
6715         int r;
6716 -       bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
6717 -       bool band_switch, mode_diff;
6718 -       u8 ini_reloaded;
6719 -
6720 -       band_switch = (chan->channelFlags & (CHANNEL_2GHZ | CHANNEL_5GHZ)) !=
6721 -                     (ah->curchan->channelFlags & (CHANNEL_2GHZ |
6722 -                                                   CHANNEL_5GHZ));
6723 -       mode_diff = (chan->chanmode != ah->curchan->chanmode);
6724 +
6725 +       if (pCap->hw_caps & ATH9K_HW_CAP_FCC_BAND_SWITCH) {
6726 +               u32 cur = ah->curchan->channelFlags & (CHANNEL_2GHZ | CHANNEL_5GHZ);
6727 +               u32 new = chan->channelFlags & (CHANNEL_2GHZ | CHANNEL_5GHZ);
6728 +               band_switch = (cur != new);
6729 +               mode_diff = (chan->chanmode != ah->curchan->chanmode);
6730 +       }
6731  
6732         for (qnum = 0; qnum < AR_NUM_QCU; qnum++) {
6733                 if (ath9k_hw_numtxpending(ah, qnum)) {
6734 @@ -1520,11 +1520,12 @@ static bool ath9k_hw_channel_change(stru
6735                 return false;
6736         }
6737  
6738 -       if (edma && (band_switch || mode_diff)) {
6739 +       if (band_switch || mode_diff) {
6740                 ath9k_hw_mark_phy_inactive(ah);
6741                 udelay(5);
6742  
6743 -               ath9k_hw_init_pll(ah, NULL);
6744 +               if (band_switch)
6745 +                       ath9k_hw_init_pll(ah, chan);
6746  
6747                 if (ath9k_hw_fast_chan_change(ah, chan, &ini_reloaded)) {
6748                         ath_err(common, "Failed to do fast channel change\n");
6749 @@ -1541,22 +1542,21 @@ static bool ath9k_hw_channel_change(stru
6750         }
6751         ath9k_hw_set_clockrate(ah);
6752         ath9k_hw_apply_txpower(ah, chan, false);
6753 -       ath9k_hw_rfbus_done(ah);
6754  
6755         if (IS_CHAN_OFDM(chan) || IS_CHAN_HT(chan))
6756                 ath9k_hw_set_delta_slope(ah, chan);
6757  
6758         ath9k_hw_spur_mitigate_freq(ah, chan);
6759  
6760 -       if (edma && (band_switch || mode_diff)) {
6761 -               ah->ah_flags |= AH_FASTCC;
6762 -               if (band_switch || ini_reloaded)
6763 -                       ah->eep_ops->set_board_values(ah, chan);
6764 +       if (band_switch || ini_reloaded)
6765 +               ah->eep_ops->set_board_values(ah, chan);
6766  
6767 -               ath9k_hw_init_bb(ah, chan);
6768 +       ath9k_hw_init_bb(ah, chan);
6769 +       ath9k_hw_rfbus_done(ah);
6770  
6771 -               if (band_switch || ini_reloaded)
6772 -                       ath9k_hw_init_cal(ah, chan);
6773 +       if (band_switch || ini_reloaded) {
6774 +               ah->ah_flags |= AH_FASTCC;
6775 +               ath9k_hw_init_cal(ah, chan);
6776                 ah->ah_flags &= ~AH_FASTCC;
6777         }
6778  
6779 @@ -1778,16 +1778,11 @@ static void ath9k_hw_init_desc(struct at
6780  /*
6781   * Fast channel change:
6782   * (Change synthesizer based on channel freq without resetting chip)
6783 - *
6784 - * Don't do FCC when
6785 - *   - Flag is not set
6786 - *   - Chip is just coming out of full sleep
6787 - *   - Channel to be set is same as current channel
6788 - *   - Channel flags are different, (eg.,moving from 2GHz to 5GHz channel)
6789   */
6790  static int ath9k_hw_do_fastcc(struct ath_hw *ah, struct ath9k_channel *chan)
6791  {
6792         struct ath_common *common = ath9k_hw_common(ah);
6793 +       struct ath9k_hw_capabilities *pCap = &ah->caps;
6794         int ret;
6795  
6796         if (AR_SREV_9280(ah) && common->bus_ops->ath_bus_type == ATH_PCI)
6797 @@ -1806,9 +1801,21 @@ static int ath9k_hw_do_fastcc(struct ath
6798             (CHANNEL_HALF | CHANNEL_QUARTER))
6799                 goto fail;
6800  
6801 -       if ((chan->channelFlags & CHANNEL_ALL) !=
6802 -           (ah->curchan->channelFlags & CHANNEL_ALL))
6803 -               goto fail;
6804 +       /*
6805 +        * If cross-band fcc is not supoprted, bail out if
6806 +        * either channelFlags or chanmode differ.
6807 +        *
6808 +        * chanmode will be different if the HT operating mode
6809 +        * changes because of CSA.
6810 +        */
6811 +       if (!(pCap->hw_caps & ATH9K_HW_CAP_FCC_BAND_SWITCH)) {
6812 +               if ((chan->channelFlags & CHANNEL_ALL) !=
6813 +                   (ah->curchan->channelFlags & CHANNEL_ALL))
6814 +                       goto fail;
6815 +
6816 +               if (chan->chanmode != ah->curchan->chanmode)
6817 +                       goto fail;
6818 +       }
6819  
6820         if (!ath9k_hw_check_alive(ah))
6821                 goto fail;
6822 @@ -2047,7 +2054,7 @@ int ath9k_hw_reset(struct ath_hw *ah, st
6823  
6824         ath9k_hw_apply_gpio_override(ah);
6825  
6826 -       if (AR_SREV_9565(ah) && ah->shared_chain_lnadiv)
6827 +       if (AR_SREV_9565(ah) && common->bt_ant_diversity)
6828                 REG_SET_BIT(ah, AR_BTCOEX_WL_LNADIV, AR_BTCOEX_WL_LNADIV_FORCE_ON);
6829  
6830         return 0;
6831 @@ -2550,34 +2557,28 @@ int ath9k_hw_fill_cap_info(struct ath_hw
6832         if (AR_SREV_9287_11_OR_LATER(ah) || AR_SREV_9271(ah))
6833                 pCap->hw_caps |= ATH9K_HW_CAP_SGI_20;
6834  
6835 -       if (AR_SREV_9285(ah))
6836 +       if (AR_SREV_9285(ah)) {
6837                 if (ah->eep_ops->get_eeprom(ah, EEP_MODAL_VER) >= 3) {
6838                         ant_div_ctl1 =
6839                                 ah->eep_ops->get_eeprom(ah, EEP_ANT_DIV_CTL1);
6840 -                       if ((ant_div_ctl1 & 0x1) && ((ant_div_ctl1 >> 3) & 0x1))
6841 +                       if ((ant_div_ctl1 & 0x1) && ((ant_div_ctl1 >> 3) & 0x1)) {
6842                                 pCap->hw_caps |= ATH9K_HW_CAP_ANT_DIV_COMB;
6843 +                               ath_info(common, "Enable LNA combining\n");
6844 +                       }
6845                 }
6846 +       }
6847 +
6848         if (AR_SREV_9300_20_OR_LATER(ah)) {
6849                 if (ah->eep_ops->get_eeprom(ah, EEP_CHAIN_MASK_REDUCE))
6850                         pCap->hw_caps |= ATH9K_HW_CAP_APM;
6851         }
6852  
6853 -
6854         if (AR_SREV_9330(ah) || AR_SREV_9485(ah) || AR_SREV_9565(ah)) {
6855                 ant_div_ctl1 = ah->eep_ops->get_eeprom(ah, EEP_ANT_DIV_CTL1);
6856 -               /*
6857 -                * enable the diversity-combining algorithm only when
6858 -                * both enable_lna_div and enable_fast_div are set
6859 -                *              Table for Diversity
6860 -                * ant_div_alt_lnaconf          bit 0-1
6861 -                * ant_div_main_lnaconf         bit 2-3
6862 -                * ant_div_alt_gaintb           bit 4
6863 -                * ant_div_main_gaintb          bit 5
6864 -                * enable_ant_div_lnadiv        bit 6
6865 -                * enable_ant_fast_div          bit 7
6866 -                */
6867 -               if ((ant_div_ctl1 >> 0x6) == 0x3)
6868 +               if ((ant_div_ctl1 >> 0x6) == 0x3) {
6869                         pCap->hw_caps |= ATH9K_HW_CAP_ANT_DIV_COMB;
6870 +                       ath_info(common, "Enable LNA combining\n");
6871 +               }
6872         }
6873  
6874         if (ath9k_hw_dfs_tested(ah))
6875 @@ -2610,6 +2611,13 @@ int ath9k_hw_fill_cap_info(struct ath_hw
6876             ah->eep_ops->get_eeprom(ah, EEP_PAPRD))
6877                         pCap->hw_caps |= ATH9K_HW_CAP_PAPRD;
6878  
6879 +       /*
6880 +        * Fast channel change across bands is available
6881 +        * only for AR9462 and AR9565.
6882 +        */
6883 +       if (AR_SREV_9462(ah) || AR_SREV_9565(ah))
6884 +               pCap->hw_caps |= ATH9K_HW_CAP_FCC_BAND_SWITCH;
6885 +
6886         return 0;
6887  }
6888  
6889 --- a/drivers/net/wireless/ath/ath9k/hw.h
6890 +++ b/drivers/net/wireless/ath/ath9k/hw.h
6891 @@ -247,6 +247,8 @@ enum ath9k_hw_caps {
6892         ATH9K_HW_CAP_DFS                        = BIT(16),
6893         ATH9K_HW_WOW_DEVICE_CAPABLE             = BIT(17),
6894         ATH9K_HW_CAP_PAPRD                      = BIT(18),
6895 +       ATH9K_HW_CAP_FCC_BAND_SWITCH            = BIT(19),
6896 +       ATH9K_HW_CAP_BT_ANT_DIV                 = BIT(20),
6897  };
6898  
6899  /*
6900 @@ -309,8 +311,11 @@ struct ath9k_ops_config {
6901         u16 ani_poll_interval; /* ANI poll interval in ms */
6902  
6903         /* Platform specific config */
6904 +       u32 aspm_l1_fix;
6905         u32 xlna_gpio;
6906 +       u32 ant_ctrl_comm2g_switch_enable;
6907         bool xatten_margin_cfg;
6908 +       bool alt_mingainidx;
6909  };
6910  
6911  enum ath9k_int {
6912 @@ -716,11 +721,14 @@ struct ath_hw_ops {
6913                         struct ath_hw_antcomb_conf *antconf);
6914         void (*antdiv_comb_conf_set)(struct ath_hw *ah,
6915                         struct ath_hw_antcomb_conf *antconf);
6916 -       void (*antctrl_shared_chain_lnadiv)(struct ath_hw *hw, bool enable);
6917         void (*spectral_scan_config)(struct ath_hw *ah,
6918                                      struct ath_spec_scan *param);
6919         void (*spectral_scan_trigger)(struct ath_hw *ah);
6920         void (*spectral_scan_wait)(struct ath_hw *ah);
6921 +
6922 +#ifdef CPTCFG_ATH9K_BTCOEX_SUPPORT
6923 +       void (*set_bt_ant_diversity)(struct ath_hw *hw, bool enable);
6924 +#endif
6925  };
6926  
6927  struct ath_nf_limits {
6928 @@ -765,7 +773,6 @@ struct ath_hw {
6929         bool aspm_enabled;
6930         bool is_monitoring;
6931         bool need_an_top2_fixup;
6932 -       bool shared_chain_lnadiv;
6933         u16 tx_trig_level;
6934  
6935         u32 nf_regs[6];
6936 --- a/drivers/net/wireless/ath/ath9k/pci.c
6937 +++ b/drivers/net/wireless/ath/ath9k/pci.c
6938 @@ -29,6 +29,60 @@ static DEFINE_PCI_DEVICE_TABLE(ath_pci_i
6939         { PCI_VDEVICE(ATHEROS, 0x0027) }, /* PCI   */
6940         { PCI_VDEVICE(ATHEROS, 0x0029) }, /* PCI   */
6941         { PCI_VDEVICE(ATHEROS, 0x002A) }, /* PCI-E */
6942 +
6943 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
6944 +                        0x002A,
6945 +                        PCI_VENDOR_ID_AZWAVE,
6946 +                        0x1C71),
6947 +         .driver_data = ATH9K_PCI_D3_L1_WAR },
6948 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
6949 +                        0x002A,
6950 +                        PCI_VENDOR_ID_FOXCONN,
6951 +                        0xE01F),
6952 +         .driver_data = ATH9K_PCI_D3_L1_WAR },
6953 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
6954 +                        0x002A,
6955 +                        0x11AD, /* LITEON */
6956 +                        0x6632),
6957 +         .driver_data = ATH9K_PCI_D3_L1_WAR },
6958 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
6959 +                        0x002A,
6960 +                        0x11AD, /* LITEON */
6961 +                        0x6642),
6962 +         .driver_data = ATH9K_PCI_D3_L1_WAR },
6963 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
6964 +                        0x002A,
6965 +                        PCI_VENDOR_ID_QMI,
6966 +                        0x0306),
6967 +         .driver_data = ATH9K_PCI_D3_L1_WAR },
6968 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
6969 +                        0x002A,
6970 +                        0x185F, /* WNC */
6971 +                        0x309D),
6972 +         .driver_data = ATH9K_PCI_D3_L1_WAR },
6973 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
6974 +                        0x002A,
6975 +                        0x10CF, /* Fujitsu */
6976 +                        0x147C),
6977 +         .driver_data = ATH9K_PCI_D3_L1_WAR },
6978 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
6979 +                        0x002A,
6980 +                        0x10CF, /* Fujitsu */
6981 +                        0x147D),
6982 +         .driver_data = ATH9K_PCI_D3_L1_WAR },
6983 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
6984 +                        0x002A,
6985 +                        0x10CF, /* Fujitsu */
6986 +                        0x1536),
6987 +         .driver_data = ATH9K_PCI_D3_L1_WAR },
6988 +
6989 +       /* AR9285 card for Asus */
6990 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
6991 +                        0x002B,
6992 +                        PCI_VENDOR_ID_AZWAVE,
6993 +                        0x2C37),
6994 +         .driver_data = ATH9K_PCI_BT_ANT_DIV },
6995 +
6996         { PCI_VDEVICE(ATHEROS, 0x002B) }, /* PCI-E */
6997         { PCI_VDEVICE(ATHEROS, 0x002C) }, /* PCI-E 802.11n bonded out */
6998         { PCI_VDEVICE(ATHEROS, 0x002D) }, /* PCI   */
6999 @@ -40,29 +94,106 @@ static DEFINE_PCI_DEVICE_TABLE(ath_pci_i
7000                          0x0032,
7001                          PCI_VENDOR_ID_AZWAVE,
7002                          0x2086),
7003 -         .driver_data = ATH9K_PCI_CUS198 },
7004 +         .driver_data = ATH9K_PCI_CUS198 | ATH9K_PCI_BT_ANT_DIV },
7005         { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7006                          0x0032,
7007                          PCI_VENDOR_ID_AZWAVE,
7008                          0x1237),
7009 -         .driver_data = ATH9K_PCI_CUS198 },
7010 +         .driver_data = ATH9K_PCI_CUS198 | ATH9K_PCI_BT_ANT_DIV },
7011         { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7012                          0x0032,
7013                          PCI_VENDOR_ID_AZWAVE,
7014                          0x2126),
7015 -         .driver_data = ATH9K_PCI_CUS198 },
7016 +         .driver_data = ATH9K_PCI_CUS198 | ATH9K_PCI_BT_ANT_DIV },
7017 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7018 +                        0x0032,
7019 +                        PCI_VENDOR_ID_AZWAVE,
7020 +                        0x126A),
7021 +         .driver_data = ATH9K_PCI_CUS198 | ATH9K_PCI_BT_ANT_DIV },
7022  
7023         /* PCI-E CUS230 */
7024         { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7025                          0x0032,
7026                          PCI_VENDOR_ID_AZWAVE,
7027                          0x2152),
7028 -         .driver_data = ATH9K_PCI_CUS230 },
7029 +         .driver_data = ATH9K_PCI_CUS230 | ATH9K_PCI_BT_ANT_DIV },
7030         { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7031                          0x0032,
7032                          PCI_VENDOR_ID_FOXCONN,
7033                          0xE075),
7034 -         .driver_data = ATH9K_PCI_CUS230 },
7035 +         .driver_data = ATH9K_PCI_CUS230 | ATH9K_PCI_BT_ANT_DIV },
7036 +
7037 +       /* WB225 */
7038 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7039 +                        0x0032,
7040 +                        PCI_VENDOR_ID_ATHEROS,
7041 +                        0x3119),
7042 +         .driver_data = ATH9K_PCI_BT_ANT_DIV },
7043 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7044 +                        0x0032,
7045 +                        PCI_VENDOR_ID_ATHEROS,
7046 +                        0x3122),
7047 +         .driver_data = ATH9K_PCI_BT_ANT_DIV },
7048 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7049 +                        0x0032,
7050 +                        0x185F, /* WNC */
7051 +                        0x3119),
7052 +         .driver_data = ATH9K_PCI_BT_ANT_DIV },
7053 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7054 +                        0x0032,
7055 +                        0x185F, /* WNC */
7056 +                        0x3027),
7057 +         .driver_data = ATH9K_PCI_BT_ANT_DIV },
7058 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7059 +                        0x0032,
7060 +                        PCI_VENDOR_ID_SAMSUNG,
7061 +                        0x4105),
7062 +         .driver_data = ATH9K_PCI_BT_ANT_DIV },
7063 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7064 +                        0x0032,
7065 +                        PCI_VENDOR_ID_SAMSUNG,
7066 +                        0x4106),
7067 +         .driver_data = ATH9K_PCI_BT_ANT_DIV },
7068 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7069 +                        0x0032,
7070 +                        PCI_VENDOR_ID_SAMSUNG,
7071 +                        0x410D),
7072 +         .driver_data = ATH9K_PCI_BT_ANT_DIV },
7073 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7074 +                        0x0032,
7075 +                        PCI_VENDOR_ID_SAMSUNG,
7076 +                        0x410E),
7077 +         .driver_data = ATH9K_PCI_BT_ANT_DIV },
7078 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7079 +                        0x0032,
7080 +                        PCI_VENDOR_ID_SAMSUNG,
7081 +                        0x410F),
7082 +         .driver_data = ATH9K_PCI_BT_ANT_DIV },
7083 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7084 +                        0x0032,
7085 +                        PCI_VENDOR_ID_SAMSUNG,
7086 +                        0xC706),
7087 +         .driver_data = ATH9K_PCI_BT_ANT_DIV },
7088 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7089 +                        0x0032,
7090 +                        PCI_VENDOR_ID_SAMSUNG,
7091 +                        0xC680),
7092 +         .driver_data = ATH9K_PCI_BT_ANT_DIV },
7093 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7094 +                        0x0032,
7095 +                        PCI_VENDOR_ID_SAMSUNG,
7096 +                        0xC708),
7097 +         .driver_data = ATH9K_PCI_BT_ANT_DIV },
7098 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7099 +                        0x0032,
7100 +                        PCI_VENDOR_ID_LENOVO,
7101 +                        0x3218),
7102 +         .driver_data = ATH9K_PCI_BT_ANT_DIV },
7103 +       { PCI_DEVICE_SUB(PCI_VENDOR_ID_ATHEROS,
7104 +                        0x0032,
7105 +                        PCI_VENDOR_ID_LENOVO,
7106 +                        0x3219),
7107 +         .driver_data = ATH9K_PCI_BT_ANT_DIV },
7108  
7109         { PCI_VDEVICE(ATHEROS, 0x0032) }, /* PCI-E  AR9485 */
7110         { PCI_VDEVICE(ATHEROS, 0x0033) }, /* PCI-E  AR9580 */
7111 @@ -229,6 +360,22 @@ static void ath_pci_aspm_init(struct ath
7112                 return;
7113         }
7114  
7115 +       /*
7116 +        * 0x70c - Ack Frequency Register.
7117 +        *
7118 +        * Bits 27:29 - DEFAULT_L1_ENTRANCE_LATENCY.
7119 +        *
7120 +        * 000 : 1 us
7121 +        * 001 : 2 us
7122 +        * 010 : 4 us
7123 +        * 011 : 8 us
7124 +        * 100 : 16 us
7125 +        * 101 : 32 us
7126 +        * 110/111 : 64 us
7127 +        */
7128 +       if (AR_SREV_9462(ah))
7129 +               pci_read_config_dword(pdev, 0x70c, &ah->config.aspm_l1_fix);
7130 +
7131         pcie_capability_read_word(parent, PCI_EXP_LNKCTL, &aspm);
7132         if (aspm & (PCI_EXP_LNKCTL_ASPM_L0S | PCI_EXP_LNKCTL_ASPM_L1)) {
7133                 ah->aspm_enabled = true;
7134 --- a/drivers/net/wireless/ath/ath9k/phy.h
7135 +++ b/drivers/net/wireless/ath/ath9k/phy.h
7136 @@ -48,4 +48,11 @@
7137  #define AR_PHY_PLL_CONTROL 0x16180
7138  #define AR_PHY_PLL_MODE 0x16184
7139  
7140 +enum ath9k_ant_div_comb_lna_conf {
7141 +       ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2,
7142 +       ATH_ANT_DIV_COMB_LNA2,
7143 +       ATH_ANT_DIV_COMB_LNA1,
7144 +       ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2,
7145 +};
7146 +
7147  #endif
7148 --- a/drivers/net/wireless/iwlwifi/mvm/time-event.c
7149 +++ b/drivers/net/wireless/iwlwifi/mvm/time-event.c
7150 @@ -73,7 +73,6 @@
7151  #include "iwl-prph.h"
7152  
7153  /* A TimeUnit is 1024 microsecond */
7154 -#define TU_TO_JIFFIES(_tu)     (usecs_to_jiffies((_tu) * 1024))
7155  #define MSEC_TO_TU(_msec)      (_msec*1000/1024)
7156  
7157  /*
7158 @@ -191,8 +190,7 @@ static void iwl_mvm_te_handle_notif(stru
7159                 iwl_mvm_te_clear_data(mvm, te_data);
7160         } else if (le32_to_cpu(notif->action) & TE_NOTIF_HOST_EVENT_START) {
7161                 te_data->running = true;
7162 -               te_data->end_jiffies = jiffies +
7163 -                       TU_TO_JIFFIES(te_data->duration);
7164 +               te_data->end_jiffies = TU_TO_EXP_TIME(te_data->duration);
7165  
7166                 if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
7167                         set_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
7168 @@ -329,8 +327,7 @@ void iwl_mvm_protect_session(struct iwl_
7169         lockdep_assert_held(&mvm->mutex);
7170  
7171         if (te_data->running &&
7172 -           time_after(te_data->end_jiffies,
7173 -                      jiffies + TU_TO_JIFFIES(min_duration))) {
7174 +           time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
7175                 IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
7176                              jiffies_to_msecs(te_data->end_jiffies - jiffies));
7177                 return;
7178 --- a/include/linux/ieee80211.h
7179 +++ b/include/linux/ieee80211.h
7180 @@ -2279,4 +2279,8 @@ static inline bool ieee80211_check_tim(c
7181         return !!(tim->virtual_map[index] & mask);
7182  }
7183  
7184 +/* convert time units */
7185 +#define TU_TO_JIFFIES(x)       (usecs_to_jiffies((x) * 1024))
7186 +#define TU_TO_EXP_TIME(x)      (jiffies + TU_TO_JIFFIES(x))
7187 +
7188  #endif /* LINUX_IEEE80211_H */
7189 --- a/net/mac80211/rate.c
7190 +++ b/net/mac80211/rate.c
7191 @@ -210,7 +210,7 @@ static bool rc_no_data_or_no_ack_use_min
7192                 !ieee80211_is_data(fc);
7193  }
7194  
7195 -static void rc_send_low_broadcast(s8 *idx, u32 basic_rates,
7196 +static void rc_send_low_basicrate(s8 *idx, u32 basic_rates,
7197                                   struct ieee80211_supported_band *sband)
7198  {
7199         u8 i;
7200 @@ -272,28 +272,37 @@ static void __rate_control_send_low(stru
7201  }
7202  
7203  
7204 -bool rate_control_send_low(struct ieee80211_sta *sta,
7205 +bool rate_control_send_low(struct ieee80211_sta *pubsta,
7206                            void *priv_sta,
7207                            struct ieee80211_tx_rate_control *txrc)
7208  {
7209         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
7210         struct ieee80211_supported_band *sband = txrc->sband;
7211 +       struct sta_info *sta;
7212         int mcast_rate;
7213 +       bool use_basicrate = false;
7214  
7215 -       if (!sta || !priv_sta || rc_no_data_or_no_ack_use_min(txrc)) {
7216 -               __rate_control_send_low(txrc->hw, sband, sta, info);
7217 +       if (!pubsta || !priv_sta || rc_no_data_or_no_ack_use_min(txrc)) {
7218 +               __rate_control_send_low(txrc->hw, sband, pubsta, info);
7219  
7220 -               if (!sta && txrc->bss) {
7221 +               if (!pubsta && txrc->bss) {
7222                         mcast_rate = txrc->bss_conf->mcast_rate[sband->band];
7223                         if (mcast_rate > 0) {
7224                                 info->control.rates[0].idx = mcast_rate - 1;
7225                                 return true;
7226                         }
7227 +                       use_basicrate = true;
7228 +               } else if (pubsta) {
7229 +                       sta = container_of(pubsta, struct sta_info, sta);
7230 +                       if (ieee80211_vif_is_mesh(&sta->sdata->vif))
7231 +                               use_basicrate = true;
7232 +               }
7233  
7234 -                       rc_send_low_broadcast(&info->control.rates[0].idx,
7235 +               if (use_basicrate)
7236 +                       rc_send_low_basicrate(&info->control.rates[0].idx,
7237                                               txrc->bss_conf->basic_rates,
7238                                               sband);
7239 -               }
7240 +
7241                 return true;
7242         }
7243         return false;
7244 --- a/drivers/net/wireless/ath/ath9k/Kconfig
7245 +++ b/drivers/net/wireless/ath/ath9k/Kconfig
7246 @@ -60,7 +60,7 @@ config ATH9K_AHB
7247  
7248  config ATH9K_DEBUGFS
7249         bool "Atheros ath9k debugging"
7250 -       depends on ATH9K
7251 +       depends on ATH9K && DEBUG_FS
7252         select MAC80211_DEBUGFS
7253         depends on RELAY
7254         ---help---
7255 --- a/drivers/net/wireless/ath/ath9k/ar9002_hw.c
7256 +++ b/drivers/net/wireless/ath/ath9k/ar9002_hw.c
7257 @@ -269,13 +269,12 @@ static void ar9002_hw_configpcipowersave
7258                         if (ah->config.pcie_waen & AR_WA_D3_L1_DISABLE)
7259                                 val |= AR_WA_D3_L1_DISABLE;
7260                 } else {
7261 -                       if (((AR_SREV_9285(ah) ||
7262 -                             AR_SREV_9271(ah) ||
7263 -                             AR_SREV_9287(ah)) &&
7264 -                            (AR9285_WA_DEFAULT & AR_WA_D3_L1_DISABLE)) ||
7265 -                           (AR_SREV_9280(ah) &&
7266 -                            (AR9280_WA_DEFAULT & AR_WA_D3_L1_DISABLE))) {
7267 -                               val |= AR_WA_D3_L1_DISABLE;
7268 +                       if (AR_SREV_9285(ah) || AR_SREV_9271(ah) || AR_SREV_9287(ah)) {
7269 +                               if (AR9285_WA_DEFAULT & AR_WA_D3_L1_DISABLE)
7270 +                                       val |= AR_WA_D3_L1_DISABLE;
7271 +                       } else if (AR_SREV_9280(ah)) {
7272 +                               if (AR9280_WA_DEFAULT & AR_WA_D3_L1_DISABLE)
7273 +                                       val |= AR_WA_D3_L1_DISABLE;
7274                         }
7275                 }
7276  
7277 @@ -297,24 +296,18 @@ static void ar9002_hw_configpcipowersave
7278         } else {
7279                 if (ah->config.pcie_waen) {
7280                         val = ah->config.pcie_waen;
7281 -                       if (!power_off)
7282 -                               val &= (~AR_WA_D3_L1_DISABLE);
7283 +                       val &= (~AR_WA_D3_L1_DISABLE);
7284                 } else {
7285 -                       if (AR_SREV_9285(ah) ||
7286 -                           AR_SREV_9271(ah) ||
7287 -                           AR_SREV_9287(ah)) {
7288 +                       if (AR_SREV_9285(ah) || AR_SREV_9271(ah) || AR_SREV_9287(ah)) {
7289                                 val = AR9285_WA_DEFAULT;
7290 -                               if (!power_off)
7291 -                                       val &= (~AR_WA_D3_L1_DISABLE);
7292 -                       }
7293 -                       else if (AR_SREV_9280(ah)) {
7294 +                               val &= (~AR_WA_D3_L1_DISABLE);
7295 +                       } else if (AR_SREV_9280(ah)) {
7296                                 /*
7297                                  * For AR9280 chips, bit 22 of 0x4004
7298                                  * needs to be set.
7299                                  */
7300                                 val = AR9280_WA_DEFAULT;
7301 -                               if (!power_off)
7302 -                                       val &= (~AR_WA_D3_L1_DISABLE);
7303 +                               val &= (~AR_WA_D3_L1_DISABLE);
7304                         } else {
7305                                 val = AR_WA_DEFAULT;
7306                         }
7307 --- a/drivers/net/wireless/ath/ath9k/ar9003_hw.c
7308 +++ b/drivers/net/wireless/ath/ath9k/ar9003_hw.c
7309 @@ -153,7 +153,7 @@ static void ar9003_hw_init_mode_regs(str
7310                 if (!ah->is_clk_25mhz)
7311                         INIT_INI_ARRAY(&ah->iniAdditional,
7312                                        ar9340_1p0_radio_core_40M);
7313 -       } else if (AR_SREV_9485_11(ah)) {
7314 +       } else if (AR_SREV_9485_11_OR_LATER(ah)) {
7315                 /* mac */
7316                 INIT_INI_ARRAY(&ah->iniMac[ATH_INI_CORE],
7317                                 ar9485_1_1_mac_core);
7318 @@ -424,7 +424,7 @@ static void ar9003_tx_gain_table_mode0(s
7319         else if (AR_SREV_9340(ah))
7320                 INIT_INI_ARRAY(&ah->iniModesTxGain,
7321                         ar9340Modes_lowest_ob_db_tx_gain_table_1p0);
7322 -       else if (AR_SREV_9485_11(ah))
7323 +       else if (AR_SREV_9485_11_OR_LATER(ah))
7324                 INIT_INI_ARRAY(&ah->iniModesTxGain,
7325                         ar9485_modes_lowest_ob_db_tx_gain_1_1);
7326         else if (AR_SREV_9550(ah))
7327 @@ -458,7 +458,7 @@ static void ar9003_tx_gain_table_mode1(s
7328         else if (AR_SREV_9340(ah))
7329                 INIT_INI_ARRAY(&ah->iniModesTxGain,
7330                         ar9340Modes_high_ob_db_tx_gain_table_1p0);
7331 -       else if (AR_SREV_9485_11(ah))
7332 +       else if (AR_SREV_9485_11_OR_LATER(ah))
7333                 INIT_INI_ARRAY(&ah->iniModesTxGain,
7334                         ar9485Modes_high_ob_db_tx_gain_1_1);
7335         else if (AR_SREV_9580(ah))
7336 @@ -492,7 +492,7 @@ static void ar9003_tx_gain_table_mode2(s
7337         else if (AR_SREV_9340(ah))
7338                 INIT_INI_ARRAY(&ah->iniModesTxGain,
7339                         ar9340Modes_low_ob_db_tx_gain_table_1p0);
7340 -       else if (AR_SREV_9485_11(ah))
7341 +       else if (AR_SREV_9485_11_OR_LATER(ah))
7342                 INIT_INI_ARRAY(&ah->iniModesTxGain,
7343                         ar9485Modes_low_ob_db_tx_gain_1_1);
7344         else if (AR_SREV_9580(ah))
7345 @@ -517,7 +517,7 @@ static void ar9003_tx_gain_table_mode3(s
7346         else if (AR_SREV_9340(ah))
7347                 INIT_INI_ARRAY(&ah->iniModesTxGain,
7348                         ar9340Modes_high_power_tx_gain_table_1p0);
7349 -       else if (AR_SREV_9485_11(ah))
7350 +       else if (AR_SREV_9485_11_OR_LATER(ah))
7351                 INIT_INI_ARRAY(&ah->iniModesTxGain,
7352                         ar9485Modes_high_power_tx_gain_1_1);
7353         else if (AR_SREV_9580(ah))
7354 @@ -552,7 +552,7 @@ static void ar9003_tx_gain_table_mode4(s
7355  
7356  static void ar9003_tx_gain_table_mode5(struct ath_hw *ah)
7357  {
7358 -       if (AR_SREV_9485_11(ah))
7359 +       if (AR_SREV_9485_11_OR_LATER(ah))
7360                 INIT_INI_ARRAY(&ah->iniModesTxGain,
7361                         ar9485Modes_green_ob_db_tx_gain_1_1);
7362         else if (AR_SREV_9340(ah))
7363 @@ -571,7 +571,7 @@ static void ar9003_tx_gain_table_mode6(s
7364         if (AR_SREV_9340(ah))
7365                 INIT_INI_ARRAY(&ah->iniModesTxGain,
7366                         ar9340Modes_low_ob_db_and_spur_tx_gain_table_1p0);
7367 -       else if (AR_SREV_9485_11(ah))
7368 +       else if (AR_SREV_9485_11_OR_LATER(ah))
7369                 INIT_INI_ARRAY(&ah->iniModesTxGain,
7370                         ar9485Modes_green_spur_ob_db_tx_gain_1_1);
7371         else if (AR_SREV_9580(ah))
7372 @@ -611,7 +611,7 @@ static void ar9003_rx_gain_table_mode0(s
7373         else if (AR_SREV_9340(ah))
7374                 INIT_INI_ARRAY(&ah->iniModesRxGain,
7375                                 ar9340Common_rx_gain_table_1p0);
7376 -       else if (AR_SREV_9485_11(ah))
7377 +       else if (AR_SREV_9485_11_OR_LATER(ah))
7378                 INIT_INI_ARRAY(&ah->iniModesRxGain,
7379                                ar9485_common_rx_gain_1_1);
7380         else if (AR_SREV_9550(ah)) {
7381 @@ -644,7 +644,7 @@ static void ar9003_rx_gain_table_mode1(s
7382         else if (AR_SREV_9340(ah))
7383                 INIT_INI_ARRAY(&ah->iniModesRxGain,
7384                         ar9340Common_wo_xlna_rx_gain_table_1p0);
7385 -       else if (AR_SREV_9485_11(ah))
7386 +       else if (AR_SREV_9485_11_OR_LATER(ah))
7387                 INIT_INI_ARRAY(&ah->iniModesRxGain,
7388                         ar9485Common_wo_xlna_rx_gain_1_1);
7389         else if (AR_SREV_9462_21(ah))
7390 @@ -745,16 +745,25 @@ static void ar9003_hw_init_mode_gain_reg
7391  static void ar9003_hw_configpcipowersave(struct ath_hw *ah,
7392                                          bool power_off)
7393  {
7394 +       /*
7395 +        * Increase L1 Entry Latency. Some WB222 boards don't have
7396 +        * this change in eeprom/OTP.
7397 +        *
7398 +        */
7399 +       if (AR_SREV_9462(ah)) {
7400 +               u32 val = ah->config.aspm_l1_fix;
7401 +               if ((val & 0xff000000) == 0x17000000) {
7402 +                       val &= 0x00ffffff;
7403 +                       val |= 0x27000000;
7404 +                       REG_WRITE(ah, 0x570c, val);
7405 +               }
7406 +       }
7407 +
7408         /* Nothing to do on restore for 11N */
7409         if (!power_off /* !restore */) {
7410                 /* set bit 19 to allow forcing of pcie core into L1 state */
7411                 REG_SET_BIT(ah, AR_PCIE_PM_CTRL, AR_PCIE_PM_CTRL_ENA);
7412 -
7413 -               /* Several PCIe massages to ensure proper behaviour */
7414 -               if (ah->config.pcie_waen)
7415 -                       REG_WRITE(ah, AR_WA, ah->config.pcie_waen);
7416 -               else
7417 -                       REG_WRITE(ah, AR_WA, ah->WARegVal);
7418 +               REG_WRITE(ah, AR_WA, ah->WARegVal);
7419         }
7420  
7421         /*
7422 --- a/drivers/net/wireless/ath/ath9k/ar9003_mac.c
7423 +++ b/drivers/net/wireless/ath/ath9k/ar9003_mac.c
7424 @@ -491,6 +491,7 @@ int ath9k_hw_process_rxdesc_edma(struct 
7425         rxs->rs_rate = MS(rxsp->status1, AR_RxRate);
7426         rxs->rs_more = (rxsp->status2 & AR_RxMore) ? 1 : 0;
7427  
7428 +       rxs->rs_firstaggr = (rxsp->status11 & AR_RxFirstAggr) ? 1 : 0;
7429         rxs->rs_isaggr = (rxsp->status11 & AR_RxAggr) ? 1 : 0;
7430         rxs->rs_moreaggr = (rxsp->status11 & AR_RxMoreAggr) ? 1 : 0;
7431         rxs->rs_antenna = (MS(rxsp->status4, AR_RxAntenna) & 0x7);
7432 --- a/drivers/net/wireless/ath/ath9k/common.c
7433 +++ b/drivers/net/wireless/ath/ath9k/common.c
7434 @@ -49,37 +49,40 @@ int ath9k_cmn_get_hw_crypto_keytype(stru
7435  }
7436  EXPORT_SYMBOL(ath9k_cmn_get_hw_crypto_keytype);
7437  
7438 -static u32 ath9k_get_extchanmode(struct ieee80211_channel *chan,
7439 -                                enum nl80211_channel_type channel_type)
7440 +static u32 ath9k_get_extchanmode(struct cfg80211_chan_def *chandef)
7441  {
7442         u32 chanmode = 0;
7443  
7444 -       switch (chan->band) {
7445 +       switch (chandef->chan->band) {
7446         case IEEE80211_BAND_2GHZ:
7447 -               switch (channel_type) {
7448 -               case NL80211_CHAN_NO_HT:
7449 -               case NL80211_CHAN_HT20:
7450 +               switch (chandef->width) {
7451 +               case NL80211_CHAN_WIDTH_20_NOHT:
7452 +               case NL80211_CHAN_WIDTH_20:
7453                         chanmode = CHANNEL_G_HT20;
7454                         break;
7455 -               case NL80211_CHAN_HT40PLUS:
7456 -                       chanmode = CHANNEL_G_HT40PLUS;
7457 +               case NL80211_CHAN_WIDTH_40:
7458 +                       if (chandef->center_freq1 > chandef->chan->center_freq)
7459 +                               chanmode = CHANNEL_G_HT40PLUS;
7460 +                       else
7461 +                               chanmode = CHANNEL_G_HT40MINUS;
7462                         break;
7463 -               case NL80211_CHAN_HT40MINUS:
7464 -                       chanmode = CHANNEL_G_HT40MINUS;
7465 +               default:
7466                         break;
7467                 }
7468                 break;
7469         case IEEE80211_BAND_5GHZ:
7470 -               switch (channel_type) {
7471 -               case NL80211_CHAN_NO_HT:
7472 -               case NL80211_CHAN_HT20:
7473 +               switch (chandef->width) {
7474 +               case NL80211_CHAN_WIDTH_20_NOHT:
7475 +               case NL80211_CHAN_WIDTH_20:
7476                         chanmode = CHANNEL_A_HT20;
7477                         break;
7478 -               case NL80211_CHAN_HT40PLUS:
7479 -                       chanmode = CHANNEL_A_HT40PLUS;
7480 +               case NL80211_CHAN_WIDTH_40:
7481 +                       if (chandef->center_freq1 > chandef->chan->center_freq)
7482 +                               chanmode = CHANNEL_A_HT40PLUS;
7483 +                       else
7484 +                               chanmode = CHANNEL_A_HT40MINUS;
7485                         break;
7486 -               case NL80211_CHAN_HT40MINUS:
7487 -                       chanmode = CHANNEL_A_HT40MINUS;
7488 +               default:
7489                         break;
7490                 }
7491                 break;
7492 @@ -94,13 +97,12 @@ static u32 ath9k_get_extchanmode(struct 
7493   * Update internal channel flags.
7494   */
7495  void ath9k_cmn_update_ichannel(struct ath9k_channel *ichan,
7496 -                              struct ieee80211_channel *chan,
7497 -                              enum nl80211_channel_type channel_type)
7498 +                              struct cfg80211_chan_def *chandef)
7499  {
7500 -       ichan->channel = chan->center_freq;
7501 -       ichan->chan = chan;
7502 +       ichan->channel = chandef->chan->center_freq;
7503 +       ichan->chan = chandef->chan;
7504  
7505 -       if (chan->band == IEEE80211_BAND_2GHZ) {
7506 +       if (chandef->chan->band == IEEE80211_BAND_2GHZ) {
7507                 ichan->chanmode = CHANNEL_G;
7508                 ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM;
7509         } else {
7510 @@ -108,8 +110,22 @@ void ath9k_cmn_update_ichannel(struct at
7511                 ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
7512         }
7513  
7514 -       if (channel_type != NL80211_CHAN_NO_HT)
7515 -               ichan->chanmode = ath9k_get_extchanmode(chan, channel_type);
7516 +       switch (chandef->width) {
7517 +       case NL80211_CHAN_WIDTH_5:
7518 +               ichan->channelFlags |= CHANNEL_QUARTER;
7519 +               break;
7520 +       case NL80211_CHAN_WIDTH_10:
7521 +               ichan->channelFlags |= CHANNEL_HALF;
7522 +               break;
7523 +       case NL80211_CHAN_WIDTH_20_NOHT:
7524 +               break;
7525 +       case NL80211_CHAN_WIDTH_20:
7526 +       case NL80211_CHAN_WIDTH_40:
7527 +               ichan->chanmode = ath9k_get_extchanmode(chandef);
7528 +               break;
7529 +       default:
7530 +               WARN_ON(1);
7531 +       }
7532  }
7533  EXPORT_SYMBOL(ath9k_cmn_update_ichannel);
7534  
7535 @@ -125,8 +141,7 @@ struct ath9k_channel *ath9k_cmn_get_curc
7536  
7537         chan_idx = curchan->hw_value;
7538         channel = &ah->channels[chan_idx];
7539 -       ath9k_cmn_update_ichannel(channel, curchan,
7540 -                                 cfg80211_get_chandef_type(&hw->conf.chandef));
7541 +       ath9k_cmn_update_ichannel(channel, &hw->conf.chandef);
7542  
7543         return channel;
7544  }
7545 --- a/drivers/net/wireless/ath/ath9k/common.h
7546 +++ b/drivers/net/wireless/ath/ath9k/common.h
7547 @@ -44,8 +44,7 @@
7548  
7549  int ath9k_cmn_get_hw_crypto_keytype(struct sk_buff *skb);
7550  void ath9k_cmn_update_ichannel(struct ath9k_channel *ichan,
7551 -                              struct ieee80211_channel *chan,
7552 -                              enum nl80211_channel_type channel_type);
7553 +                              struct cfg80211_chan_def *chandef);
7554  struct ath9k_channel *ath9k_cmn_get_curchannel(struct ieee80211_hw *hw,
7555                                                struct ath_hw *ah);
7556  int ath9k_cmn_count_streams(unsigned int chainmask, int max);
7557 --- a/drivers/net/wireless/ath/ath9k/hif_usb.c
7558 +++ b/drivers/net/wireless/ath/ath9k/hif_usb.c
7559 @@ -115,10 +115,10 @@ static int hif_usb_send_regout(struct hi
7560         cmd->skb = skb;
7561         cmd->hif_dev = hif_dev;
7562  
7563 -       usb_fill_bulk_urb(urb, hif_dev->udev,
7564 -                        usb_sndbulkpipe(hif_dev->udev, USB_REG_OUT_PIPE),
7565 +       usb_fill_int_urb(urb, hif_dev->udev,
7566 +                        usb_sndintpipe(hif_dev->udev, USB_REG_OUT_PIPE),
7567                          skb->data, skb->len,
7568 -                        hif_usb_regout_cb, cmd);
7569 +                        hif_usb_regout_cb, cmd, 1);
7570  
7571         usb_anchor_urb(urb, &hif_dev->regout_submitted);
7572         ret = usb_submit_urb(urb, GFP_KERNEL);
7573 @@ -723,11 +723,11 @@ static void ath9k_hif_usb_reg_in_cb(stru
7574                         return;
7575                 }
7576  
7577 -               usb_fill_bulk_urb(urb, hif_dev->udev,
7578 -                                usb_rcvbulkpipe(hif_dev->udev,
7579 +               usb_fill_int_urb(urb, hif_dev->udev,
7580 +                                usb_rcvintpipe(hif_dev->udev,
7581                                                  USB_REG_IN_PIPE),
7582                                  nskb->data, MAX_REG_IN_BUF_SIZE,
7583 -                                ath9k_hif_usb_reg_in_cb, nskb);
7584 +                                ath9k_hif_usb_reg_in_cb, nskb, 1);
7585         }
7586  
7587  resubmit:
7588 @@ -909,11 +909,11 @@ static int ath9k_hif_usb_alloc_reg_in_ur
7589                         goto err_skb;
7590                 }
7591  
7592 -               usb_fill_bulk_urb(urb, hif_dev->udev,
7593 -                                 usb_rcvbulkpipe(hif_dev->udev,
7594 +               usb_fill_int_urb(urb, hif_dev->udev,
7595 +                                 usb_rcvintpipe(hif_dev->udev,
7596                                                   USB_REG_IN_PIPE),
7597                                   skb->data, MAX_REG_IN_BUF_SIZE,
7598 -                                 ath9k_hif_usb_reg_in_cb, skb);
7599 +                                 ath9k_hif_usb_reg_in_cb, skb, 1);
7600  
7601                 /* Anchor URB */
7602                 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
7603 @@ -1031,9 +1031,7 @@ static int ath9k_hif_usb_download_fw(str
7604  
7605  static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev)
7606  {
7607 -       struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
7608 -       struct usb_endpoint_descriptor *endp;
7609 -       int ret, idx;
7610 +       int ret;
7611  
7612         ret = ath9k_hif_usb_download_fw(hif_dev);
7613         if (ret) {
7614 @@ -1043,20 +1041,6 @@ static int ath9k_hif_usb_dev_init(struct
7615                 return ret;
7616         }
7617  
7618 -       /* On downloading the firmware to the target, the USB descriptor of EP4
7619 -        * is 'patched' to change the type of the endpoint to Bulk. This will
7620 -        * bring down CPU usage during the scan period.
7621 -        */
7622 -       for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
7623 -               endp = &alt->endpoint[idx].desc;
7624 -               if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
7625 -                               == USB_ENDPOINT_XFER_INT) {
7626 -                       endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
7627 -                       endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
7628 -                       endp->bInterval = 0;
7629 -               }
7630 -       }
7631 -
7632         /* Alloc URBs */
7633         ret = ath9k_hif_usb_alloc_urbs(hif_dev);
7634         if (ret) {
7635 @@ -1268,7 +1252,7 @@ static void ath9k_hif_usb_reboot(struct 
7636         if (!buf)
7637                 return;
7638  
7639 -       ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
7640 +       ret = usb_interrupt_msg(udev, usb_sndintpipe(udev, USB_REG_OUT_PIPE),
7641                            buf, 4, NULL, HZ);
7642         if (ret)
7643                 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
7644 --- a/drivers/net/wireless/ath/ath9k/htc_drv_main.c
7645 +++ b/drivers/net/wireless/ath/ath9k/htc_drv_main.c
7646 @@ -1203,16 +1203,13 @@ static int ath9k_htc_config(struct ieee8
7647  
7648         if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) || chip_reset) {
7649                 struct ieee80211_channel *curchan = hw->conf.chandef.chan;
7650 -               enum nl80211_channel_type channel_type =
7651 -                       cfg80211_get_chandef_type(&hw->conf.chandef);
7652                 int pos = curchan->hw_value;
7653  
7654                 ath_dbg(common, CONFIG, "Set channel: %d MHz\n",
7655                         curchan->center_freq);
7656  
7657                 ath9k_cmn_update_ichannel(&priv->ah->channels[pos],
7658 -                                         hw->conf.chandef.chan,
7659 -                                         channel_type);
7660 +                                         &hw->conf.chandef);
7661  
7662                 if (ath9k_htc_set_channel(priv, hw, &priv->ah->channels[pos]) < 0) {
7663                         ath_err(common, "Unable to set channel\n");
7664 --- a/drivers/net/wireless/ath/ath9k/htc_drv_txrx.c
7665 +++ b/drivers/net/wireless/ath/ath9k/htc_drv_txrx.c
7666 @@ -448,6 +448,7 @@ static void ath9k_htc_tx_process(struct 
7667         struct ieee80211_conf *cur_conf = &priv->hw->conf;
7668         bool txok;
7669         int slot;
7670 +       int hdrlen, padsize;
7671  
7672         slot = strip_drv_header(priv, skb);
7673         if (slot < 0) {
7674 @@ -504,6 +505,15 @@ send_mac80211:
7675  
7676         ath9k_htc_tx_clear_slot(priv, slot);
7677  
7678 +       /* Remove padding before handing frame back to mac80211 */
7679 +       hdrlen = ieee80211_get_hdrlen_from_skb(skb);
7680 +
7681 +       padsize = hdrlen & 3;
7682 +       if (padsize && skb->len > hdrlen + padsize) {
7683 +               memmove(skb->data + padsize, skb->data, hdrlen);
7684 +               skb_pull(skb, padsize);
7685 +       }
7686 +
7687         /* Send status to mac80211 */
7688         ieee80211_tx_status(priv->hw, skb);
7689  }
7690 --- a/drivers/net/wireless/ath/ath9k/link.c
7691 +++ b/drivers/net/wireless/ath/ath9k/link.c
7692 @@ -41,7 +41,7 @@ void ath_tx_complete_poll_work(struct wo
7693                                 txq->axq_tx_inprogress = true;
7694                         }
7695                 }
7696 -               ath_txq_unlock_complete(sc, txq);
7697 +               ath_txq_unlock(sc, txq);
7698         }
7699  
7700         if (needreset) {
7701 --- a/drivers/net/wireless/ath/ath9k/mac.c
7702 +++ b/drivers/net/wireless/ath/ath9k/mac.c
7703 @@ -583,9 +583,9 @@ int ath9k_hw_rxprocdesc(struct ath_hw *a
7704         rs->rs_rate = MS(ads.ds_rxstatus0, AR_RxRate);
7705         rs->rs_more = (ads.ds_rxstatus1 & AR_RxMore) ? 1 : 0;
7706  
7707 +       rs->rs_firstaggr = (ads.ds_rxstatus8 & AR_RxFirstAggr) ? 1 : 0;
7708         rs->rs_isaggr = (ads.ds_rxstatus8 & AR_RxAggr) ? 1 : 0;
7709 -       rs->rs_moreaggr =
7710 -               (ads.ds_rxstatus8 & AR_RxMoreAggr) ? 1 : 0;
7711 +       rs->rs_moreaggr = (ads.ds_rxstatus8 & AR_RxMoreAggr) ? 1 : 0;
7712         rs->rs_antenna = MS(ads.ds_rxstatus3, AR_RxAntenna);
7713  
7714         /* directly mapped flags for ieee80211_rx_status */
7715 --- a/drivers/net/wireless/ath/ath9k/mac.h
7716 +++ b/drivers/net/wireless/ath/ath9k/mac.h
7717 @@ -140,6 +140,7 @@ struct ath_rx_status {
7718         int8_t rs_rssi_ext1;
7719         int8_t rs_rssi_ext2;
7720         u8 rs_isaggr;
7721 +       u8 rs_firstaggr;
7722         u8 rs_moreaggr;
7723         u8 rs_num_delims;
7724         u8 rs_flags;
7725 @@ -569,6 +570,7 @@ struct ar5416_desc {
7726  #define AR_RxAggr           0x00020000
7727  #define AR_PostDelimCRCErr  0x00040000
7728  #define AR_RxStatusRsvd71   0x3ff80000
7729 +#define AR_RxFirstAggr      0x20000000
7730  #define AR_DecryptBusyErr   0x40000000
7731  #define AR_KeyMiss          0x80000000
7732  
7733 --- a/drivers/net/wireless/ath/ath9k/rc.c
7734 +++ b/drivers/net/wireless/ath/ath9k/rc.c
7735 @@ -1324,8 +1324,8 @@ static void ath_rate_update(void *priv, 
7736                 ath_rc_init(sc, priv_sta);
7737  
7738                 ath_dbg(ath9k_hw_common(sc->sc_ah), CONFIG,
7739 -                       "Operating HT Bandwidth changed to: %d\n",
7740 -                       cfg80211_get_chandef_type(&sc->hw->conf.chandef));
7741 +                       "Operating Bandwidth changed to: %d\n",
7742 +                       sc->hw->conf.chandef.width);
7743         }
7744  }
7745  
7746 --- a/drivers/net/wireless/ath/ath9k/reg.h
7747 +++ b/drivers/net/wireless/ath/ath9k/reg.h
7748 @@ -893,9 +893,9 @@
7749  
7750  #define AR_SREV_9485(_ah) \
7751         (((_ah)->hw_version.macVersion == AR_SREV_VERSION_9485))
7752 -#define AR_SREV_9485_11(_ah) \
7753 -       (AR_SREV_9485(_ah) && \
7754 -        ((_ah)->hw_version.macRev == AR_SREV_REVISION_9485_11))
7755 +#define AR_SREV_9485_11_OR_LATER(_ah) \
7756 +       (((_ah)->hw_version.macVersion == AR_SREV_VERSION_9485) && \
7757 +        ((_ah)->hw_version.macRev >= AR_SREV_REVISION_9485_11))
7758  #define AR_SREV_9485_OR_LATER(_ah) \
7759         (((_ah)->hw_version.macVersion >= AR_SREV_VERSION_9485))
7760