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