mac80211: update to wireless-testing 2014-03-31
[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 @@ -1128,6 +1130,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 @@ -1172,6 +1176,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 @@ -1183,6 +1210,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,72 @@ 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 +       unsigned long flags;
134 +       u32 tsf;
135 +
136 +       if (!sc->p2p_ps_timer)
137 +               return;
138 +
139 +       if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
140 +               return;
141 +
142 +       sc->p2p_ps_vif = avp;
143 +
144 +       spin_lock_irqsave(&sc->sc_pm_lock, flags);
145 +       if (!(sc->ps_flags & PS_BEACON_SYNC)) {
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 +       spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
151 +}
152 +
153  static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
154                                    struct ieee80211_vif *vif,
155                                    struct ieee80211_bss_conf *bss_conf,
156 @@ -1723,6 +1823,12 @@ static void ath9k_bss_info_changed(struc
157                 }
158         }
159  
160 +       if (changed & BSS_CHANGED_P2P_PS) {
161 +               spin_lock_bh(&sc->sc_pcu_lock);
162 +               ath9k_update_p2p_ps(sc, vif);
163 +               spin_unlock_bh(&sc->sc_pcu_lock);
164 +       }
165 +
166         if (changed & CHECK_ANI)
167                 ath_check_ani(sc);
168  
169 --- a/drivers/net/wireless/ath/ath9k/ath9k.h
170 +++ b/drivers/net/wireless/ath/ath9k/ath9k.h
171 @@ -114,6 +114,9 @@ int ath_descdma_setup(struct ath_softc *
172  #define ATH_TXFIFO_DEPTH           8
173  #define ATH_TX_ERROR               0x01
174  
175 +/* Stop tx traffic 1ms before the GO goes away */
176 +#define ATH_P2P_PS_STOP_TIME       1000
177 +
178  #define IEEE80211_SEQ_SEQ_SHIFT    4
179  #define IEEE80211_SEQ_MAX          4096
180  #define IEEE80211_WEP_IVLEN        3
181 @@ -367,11 +370,15 @@ void ath9k_release_buffered_frames(struc
182  /********/
183  
184  struct ath_vif {
185 +       struct ieee80211_vif *vif;
186         struct ath_node mcast_node;
187         int av_bslot;
188         bool primary_sta_vif;
189         __le64 tsf_adjust; /* TSF adjustment for staggered beacons */
190         struct ath_buf *av_bcbuf;
191 +
192 +       /* P2P Client */
193 +       struct ieee80211_noa_data noa;
194  };
195  
196  struct ath9k_vif_iter_data {
197 @@ -464,6 +471,8 @@ int ath_update_survey_stats(struct ath_s
198  void ath_update_survey_nf(struct ath_softc *sc, int channel);
199  void ath9k_queue_reset(struct ath_softc *sc, enum ath_reset_type type);
200  void ath_ps_full_sleep(unsigned long data);
201 +void ath9k_p2p_ps_timer(void *priv);
202 +void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif);
203  
204  /**********/
205  /* BTCOEX */
206 @@ -724,6 +733,9 @@ struct ath_softc {
207         struct completion paprd_complete;
208         wait_queue_head_t tx_wait;
209  
210 +       struct ath_gen_timer *p2p_ps_timer;
211 +       struct ath_vif *p2p_ps_vif;
212 +
213         unsigned long driver_data;
214  
215         u8 gtt_cnt;
216 --- a/drivers/net/wireless/ath/ath9k/init.c
217 +++ b/drivers/net/wireless/ath/ath9k/init.c
218 @@ -593,6 +593,9 @@ static int ath9k_init_softc(u16 devid, s
219         if (ret)
220                 goto err_btcoex;
221  
222 +       sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
223 +               NULL, sc, AR_FIRST_NDP_TIMER);
224 +
225         ath9k_cmn_init_crypto(sc->sc_ah);
226         ath9k_init_misc(sc);
227         ath_fill_led_pin(sc);
228 @@ -870,6 +873,9 @@ static void ath9k_deinit_softc(struct at
229  {
230         int i = 0;
231  
232 +       if (sc->p2p_ps_timer)
233 +               ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
234 +
235         ath9k_deinit_btcoex(sc);
236  
237         for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
238 --- a/drivers/net/wireless/ath/ath9k/recv.c
239 +++ b/drivers/net/wireless/ath/ath9k/recv.c
240 @@ -539,6 +539,9 @@ static void ath_rx_ps_beacon(struct ath_
241                 ath_dbg(common, PS,
242                         "Reconfigure beacon timers based on synchronized timestamp\n");
243                 ath9k_set_beacon(sc);
244 +
245 +               if (sc->p2p_ps_vif)
246 +                       ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
247         }
248  
249         if (ath_beacon_dtim_pending_cab(skb)) {