ath9k: enable u-apsd support
[openwrt.git] / package / kernel / mac80211 / patches / 552-ath9k_p2p_ps_support.patch
1 From 6744d0a7ea037c7d65e13ca906da93009b241d00 Mon Sep 17 00:00:00 2001
2 From: Felix Fietkau <nbd@openwrt.org>
3 Date: Tue, 11 Feb 2014 11:16:24 +0100
4 Subject: [PATCH] ath9k: implement p2p client powersave support
5
6 Use generic TSF timers to trigger powersave state changes based
7 information from the P2P NoA attribute.
8 Opportunistic Powersave is not handled, because the driver does not
9 support powersave at the moment.
10
11 Signed-off-by: Felix Fietkau <nbd@openwrt.org>
12 ---
13  drivers/net/wireless/ath/ath9k/ath9k.h |  12 ++++
14  drivers/net/wireless/ath/ath9k/init.c  |   6 ++
15  drivers/net/wireless/ath/ath9k/main.c  | 104 +++++++++++++++++++++++++++++++++
16  drivers/net/wireless/ath/ath9k/recv.c  |   3 +
17  4 files changed, 125 insertions(+)
18
19 --- a/drivers/net/wireless/ath/ath9k/main.c
20 +++ b/drivers/net/wireless/ath/ath9k/main.c
21 @@ -261,6 +261,8 @@ static bool ath_complete_reset(struct at
22         sc->gtt_cnt = 0;
23         ieee80211_wake_queues(sc->hw);
24  
25 +       ath9k_p2p_ps_timer(sc);
26 +
27         return true;
28  }
29  
30 @@ -1126,6 +1128,8 @@ static int ath9k_add_interface(struct ie
31         if (ath9k_uses_beacons(vif->type))
32                 ath9k_beacon_assign_slot(sc, vif);
33  
34 +       avp->vif = vif;
35 +
36         an->sc = sc;
37         an->sta = NULL;
38         an->vif = vif;
39 @@ -1170,6 +1174,29 @@ static int ath9k_change_interface(struct
40         return 0;
41  }
42  
43 +static void
44 +ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
45 +{
46 +       struct ath_hw *ah = sc->sc_ah;
47 +       s32 tsf, target_tsf;
48 +
49 +       if (!avp || !avp->noa.has_next_tsf)
50 +               return;
51 +
52 +       ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
53 +
54 +       tsf = ath9k_hw_gettsf32(sc->sc_ah);
55 +
56 +       target_tsf = avp->noa.next_tsf;
57 +       if (!avp->noa.absent)
58 +               target_tsf -= ATH_P2P_PS_STOP_TIME;
59 +
60 +       if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
61 +               target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
62 +
63 +       ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
64 +}
65 +
66  static void ath9k_remove_interface(struct ieee80211_hw *hw,
67                                    struct ieee80211_vif *vif)
68  {
69 @@ -1181,6 +1208,13 @@ static void ath9k_remove_interface(struc
70  
71         mutex_lock(&sc->mutex);
72  
73 +       spin_lock_bh(&sc->sc_pcu_lock);
74 +       if (avp == sc->p2p_ps_vif) {
75 +               sc->p2p_ps_vif = NULL;
76 +               ath9k_update_p2p_ps_timer(sc, NULL);
77 +       }
78 +       spin_unlock_bh(&sc->sc_pcu_lock);
79 +
80         sc->nvifs--;
81         sc->tx99_vif = NULL;
82  
83 @@ -1649,6 +1683,70 @@ static void ath9k_bss_assoc_iter(void *d
84                 ath9k_set_assoc_state(sc, vif);
85  }
86  
87 +void ath9k_p2p_ps_timer(void *priv)
88 +{
89 +       struct ath_softc *sc = priv;
90 +       struct ath_vif *avp = sc->p2p_ps_vif;
91 +       struct ieee80211_vif *vif;
92 +       struct ieee80211_sta *sta;
93 +       struct ath_node *an;
94 +       u32 tsf;
95 +
96 +       if (!avp)
97 +               return;
98 +
99 +       tsf = ath9k_hw_gettsf32(sc->sc_ah);
100 +       if (!avp->noa.absent)
101 +               tsf += ATH_P2P_PS_STOP_TIME;
102 +
103 +       if (!avp->noa.has_next_tsf ||
104 +           avp->noa.next_tsf - tsf > BIT(31))
105 +               ieee80211_update_p2p_noa(&avp->noa, tsf);
106 +
107 +       ath9k_update_p2p_ps_timer(sc, avp);
108 +
109 +       rcu_read_lock();
110 +
111 +       vif = avp->vif;
112 +       sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
113 +       if (!sta)
114 +               goto out;
115 +
116 +       an = (void *) sta->drv_priv;
117 +       if (an->sleeping == !!avp->noa.absent)
118 +               goto out;
119 +
120 +       an->sleeping = avp->noa.absent;
121 +       if (an->sleeping)
122 +               ath_tx_aggr_sleep(sta, sc, an);
123 +       else
124 +               ath_tx_aggr_wakeup(sc, an);
125 +
126 +out:
127 +       rcu_read_unlock();
128 +}
129 +
130 +void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
131 +{
132 +       struct ath_vif *avp = (void *)vif->drv_priv;
133 +       u32 tsf;
134 +
135 +       if (!sc->p2p_ps_timer)
136 +               return;
137 +
138 +       if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
139 +               return;
140 +
141 +       sc->p2p_ps_vif = avp;
142 +
143 +       if (sc->ps_flags & PS_BEACON_SYNC)
144 +               return;
145 +
146 +       tsf = ath9k_hw_gettsf32(sc->sc_ah);
147 +       ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
148 +       ath9k_update_p2p_ps_timer(sc, avp);
149 +}
150 +
151  static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
152                                    struct ieee80211_vif *vif,
153                                    struct ieee80211_bss_conf *bss_conf,
154 @@ -1723,6 +1821,12 @@ static void ath9k_bss_info_changed(struc
155                 }
156         }
157  
158 +       if (changed & BSS_CHANGED_P2P_PS) {
159 +               spin_lock_bh(&sc->sc_pcu_lock);
160 +               ath9k_update_p2p_ps(sc, vif);
161 +               spin_unlock_bh(&sc->sc_pcu_lock);
162 +       }
163 +
164         if (changed & CHECK_ANI)
165                 ath_check_ani(sc);
166  
167 --- a/drivers/net/wireless/ath/ath9k/ath9k.h
168 +++ b/drivers/net/wireless/ath/ath9k/ath9k.h
169 @@ -115,6 +115,9 @@ int ath_descdma_setup(struct ath_softc *
170  #define ATH_TXFIFO_DEPTH           8
171  #define ATH_TX_ERROR               0x01
172  
173 +/* Stop tx traffic 1ms before the GO goes away */
174 +#define ATH_P2P_PS_STOP_TIME       1000
175 +
176  #define IEEE80211_SEQ_SEQ_SHIFT    4
177  #define IEEE80211_SEQ_MAX          4096
178  #define IEEE80211_WEP_IVLEN        3
179 @@ -363,11 +366,15 @@ void ath9k_release_buffered_frames(struc
180  /********/
181  
182  struct ath_vif {
183 +       struct ieee80211_vif *vif;
184         struct ath_node mcast_node;
185         int av_bslot;
186         bool primary_sta_vif;
187         __le64 tsf_adjust; /* TSF adjustment for staggered beacons */
188         struct ath_buf *av_bcbuf;
189 +
190 +       /* P2P Client */
191 +       struct ieee80211_noa_data noa;
192  };
193  
194  struct ath9k_vif_iter_data {
195 @@ -472,6 +479,8 @@ int ath_update_survey_stats(struct ath_s
196  void ath_update_survey_nf(struct ath_softc *sc, int channel);
197  void ath9k_queue_reset(struct ath_softc *sc, enum ath_reset_type type);
198  void ath_ps_full_sleep(unsigned long data);
199 +void ath9k_p2p_ps_timer(void *priv);
200 +void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif);
201  
202  /**********/
203  /* BTCOEX */
204 @@ -741,6 +750,9 @@ struct ath_softc {
205         struct completion paprd_complete;
206         wait_queue_head_t tx_wait;
207  
208 +       struct ath_gen_timer *p2p_ps_timer;
209 +       struct ath_vif *p2p_ps_vif;
210 +
211         unsigned long sc_flags;
212         unsigned long driver_data;
213  
214 --- a/drivers/net/wireless/ath/ath9k/init.c
215 +++ b/drivers/net/wireless/ath/ath9k/init.c
216 @@ -797,6 +797,9 @@ static int ath9k_init_softc(u16 devid, s
217         if (ret)
218                 goto err_btcoex;
219  
220 +       sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
221 +               NULL, sc, AR_FIRST_NDP_TIMER);
222 +
223         ath9k_cmn_init_crypto(sc->sc_ah);
224         ath9k_init_misc(sc);
225         ath_fill_led_pin(sc);
226 @@ -1082,6 +1085,9 @@ static void ath9k_deinit_softc(struct at
227  {
228         int i = 0;
229  
230 +       if (sc->p2p_ps_timer)
231 +               ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
232 +
233         ath9k_deinit_btcoex(sc);
234  
235         for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
236 --- a/drivers/net/wireless/ath/ath9k/recv.c
237 +++ b/drivers/net/wireless/ath/ath9k/recv.c
238 @@ -539,6 +539,9 @@ static void ath_rx_ps_beacon(struct ath_
239                 ath_dbg(common, PS,
240                         "Reconfigure beacon timers based on synchronized timestamp\n");
241                 ath9k_set_beacon(sc);
242 +
243 +               if (sc->p2p_ps_vif)
244 +                       ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
245         }
246  
247         if (ath_beacon_dtim_pending_cab(skb)) {