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