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