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