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