ath9k: add a missing null pointer check and some further cleanup
[openwrt.git] / package / mac80211 / patches / 530-ath9k_nf_sanitize.patch
1 --- a/drivers/net/wireless/ath/ath9k/ar5008_phy.c
2 +++ b/drivers/net/wireless/ath/ath9k/ar5008_phy.c
3 @@ -1678,6 +1678,15 @@ static void ar5008_hw_ani_cache_ini_regs
4         aniState->cycleCount = 0;
5  }
6  
7 +static void ar5008_hw_set_nf_limits(struct ath_hw *ah)
8 +{
9 +       ah->nf_2g.max = AR_PHY_CCA_MAX_GOOD_VAL_5416_2GHZ;
10 +       ah->nf_2g.min = AR_PHY_CCA_MIN_GOOD_VAL_5416_2GHZ;
11 +       ah->nf_2g.nominal = AR_PHY_CCA_NOM_VAL_5416_2GHZ;
12 +       ah->nf_5g.max = AR_PHY_CCA_MAX_GOOD_VAL_5416_5GHZ;
13 +       ah->nf_5g.min = AR_PHY_CCA_MIN_GOOD_VAL_5416_5GHZ;
14 +       ah->nf_5g.nominal = AR_PHY_CCA_NOM_VAL_5416_5GHZ;
15 +}
16  
17  void ar5008_hw_attach_phy_ops(struct ath_hw *ah)
18  {
19 @@ -1715,4 +1724,6 @@ void ar5008_hw_attach_phy_ops(struct ath
20                 priv_ops->compute_pll_control = ar9160_hw_compute_pll_control;
21         else
22                 priv_ops->compute_pll_control = ar5008_hw_compute_pll_control;
23 +
24 +       ar5008_hw_set_nf_limits(ah);
25  }
26 --- a/drivers/net/wireless/ath/ath9k/ar9002_phy.c
27 +++ b/drivers/net/wireless/ath/ath9k/ar9002_phy.c
28 @@ -481,9 +481,6 @@ static void ar9002_hw_do_getnf(struct at
29         ath_print(common, ATH_DBG_CALIBRATE,
30                   "NF calibrated [ctl] [chain 0] is %d\n", nf);
31  
32 -       if (AR_SREV_9271(ah) && (nf >= -114))
33 -               nf = -116;
34 -
35         nfarray[0] = nf;
36  
37         if (!AR_SREV_9285(ah) && !AR_SREV_9271(ah)) {
38 @@ -503,9 +500,6 @@ static void ar9002_hw_do_getnf(struct at
39         ath_print(common, ATH_DBG_CALIBRATE,
40                   "NF calibrated [ext] [chain 0] is %d\n", nf);
41  
42 -       if (AR_SREV_9271(ah) && (nf >= -114))
43 -               nf = -116;
44 -
45         nfarray[3] = nf;
46  
47         if (!AR_SREV_9285(ah) && !AR_SREV_9271(ah)) {
48 @@ -520,6 +514,30 @@ static void ar9002_hw_do_getnf(struct at
49         }
50  }
51  
52 +static void ar9002_hw_set_nf_limits(struct ath_hw *ah)
53 +{
54 +       if (AR_SREV_9285(ah)) {
55 +               ah->nf_2g.max = AR_PHY_CCA_MAX_GOOD_VAL_9285_2GHZ;
56 +               ah->nf_2g.min = AR_PHY_CCA_MIN_GOOD_VAL_9285_2GHZ;
57 +               ah->nf_2g.nominal = AR_PHY_CCA_NOM_VAL_9285_2GHZ;
58 +       } else if (AR_SREV_9287(ah)) {
59 +               ah->nf_2g.max = AR_PHY_CCA_MAX_GOOD_VAL_9287_2GHZ;
60 +               ah->nf_2g.min = AR_PHY_CCA_MIN_GOOD_VAL_9287_2GHZ;
61 +               ah->nf_2g.nominal = AR_PHY_CCA_NOM_VAL_9287_2GHZ;
62 +       } else if (AR_SREV_9271(ah)) {
63 +               ah->nf_2g.max = AR_PHY_CCA_MAX_GOOD_VAL_9271_2GHZ;
64 +               ah->nf_2g.min = AR_PHY_CCA_MIN_GOOD_VAL_9271_2GHZ;
65 +               ah->nf_2g.nominal = AR_PHY_CCA_NOM_VAL_9271_2GHZ;
66 +       } else {
67 +               ah->nf_2g.max = AR_PHY_CCA_MAX_GOOD_VAL_9280_2GHZ;
68 +               ah->nf_2g.min = AR_PHY_CCA_MIN_GOOD_VAL_9280_2GHZ;
69 +               ah->nf_2g.nominal = AR_PHY_CCA_NOM_VAL_9280_2GHZ;
70 +               ah->nf_5g.max = AR_PHY_CCA_MAX_GOOD_VAL_9280_5GHZ;
71 +               ah->nf_5g.min = AR_PHY_CCA_MIN_GOOD_VAL_9280_5GHZ;
72 +               ah->nf_5g.nominal = AR_PHY_CCA_NOM_VAL_9280_5GHZ;
73 +       }
74 +}
75 +
76  void ar9002_hw_attach_phy_ops(struct ath_hw *ah)
77  {
78         struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
79 @@ -532,4 +550,6 @@ void ar9002_hw_attach_phy_ops(struct ath
80         priv_ops->olc_init = ar9002_olc_init;
81         priv_ops->compute_pll_control = ar9002_hw_compute_pll_control;
82         priv_ops->do_getnf = ar9002_hw_do_getnf;
83 +
84 +       ar9002_hw_set_nf_limits(ah);
85  }
86 --- a/drivers/net/wireless/ath/ath9k/ar9002_phy.h
87 +++ b/drivers/net/wireless/ath/ath9k/ar9002_phy.h
88 @@ -576,4 +576,30 @@
89  #define AR_PHY_CH2_EXT_MINCCA_PWR   0xFF800000
90  #define AR_PHY_CH2_EXT_MINCCA_PWR_S 23
91  
92 +#define AR_PHY_CCA_NOM_VAL_5416_2GHZ            -90
93 +#define AR_PHY_CCA_NOM_VAL_5416_5GHZ            -100
94 +#define AR_PHY_CCA_MIN_GOOD_VAL_5416_2GHZ     -100
95 +#define AR_PHY_CCA_MIN_GOOD_VAL_5416_5GHZ     -110
96 +#define AR_PHY_CCA_MAX_GOOD_VAL_5416_2GHZ     -80
97 +#define AR_PHY_CCA_MAX_GOOD_VAL_5416_5GHZ     -90
98 +
99 +#define AR_PHY_CCA_NOM_VAL_9280_2GHZ         -112
100 +#define AR_PHY_CCA_NOM_VAL_9280_5GHZ         -112
101 +#define AR_PHY_CCA_MIN_GOOD_VAL_9280_2GHZ  -127
102 +#define AR_PHY_CCA_MIN_GOOD_VAL_9280_5GHZ  -122
103 +#define AR_PHY_CCA_MAX_GOOD_VAL_9280_2GHZ  -97
104 +#define AR_PHY_CCA_MAX_GOOD_VAL_9280_5GHZ  -102
105 +
106 +#define AR_PHY_CCA_NOM_VAL_9285_2GHZ           -118
107 +#define AR_PHY_CCA_MIN_GOOD_VAL_9285_2GHZ    -127
108 +#define AR_PHY_CCA_MAX_GOOD_VAL_9285_2GHZ    -108
109 +
110 +#define AR_PHY_CCA_NOM_VAL_9271_2GHZ             -118
111 +#define AR_PHY_CCA_MIN_GOOD_VAL_9271_2GHZ      -127
112 +#define AR_PHY_CCA_MAX_GOOD_VAL_9271_2GHZ      -116
113 +
114 +#define AR_PHY_CCA_NOM_VAL_9287_2GHZ           -120
115 +#define AR_PHY_CCA_MIN_GOOD_VAL_9287_2GHZ    -127
116 +#define AR_PHY_CCA_MAX_GOOD_VAL_9287_2GHZ    -110
117 +
118  #endif
119 --- a/drivers/net/wireless/ath/ath9k/ar9003_phy.c
120 +++ b/drivers/net/wireless/ath/ath9k/ar9003_phy.c
121 @@ -1015,52 +1015,6 @@ static bool ar9003_hw_ani_control(struct
122         return true;
123  }
124  
125 -static void ar9003_hw_nf_sanitize_2g(struct ath_hw *ah, s16 *nf)
126 -{
127 -       struct ath_common *common = ath9k_hw_common(ah);
128 -
129 -       if (*nf > ah->nf_2g_max) {
130 -               ath_print(common, ATH_DBG_CALIBRATE,
131 -                         "2 GHz NF (%d) > MAX (%d), "
132 -                         "correcting to MAX",
133 -                         *nf, ah->nf_2g_max);
134 -               *nf = ah->nf_2g_max;
135 -       } else if (*nf < ah->nf_2g_min) {
136 -               ath_print(common, ATH_DBG_CALIBRATE,
137 -                         "2 GHz NF (%d) < MIN (%d), "
138 -                         "correcting to MIN",
139 -                         *nf, ah->nf_2g_min);
140 -               *nf = ah->nf_2g_min;
141 -       }
142 -}
143 -
144 -static void ar9003_hw_nf_sanitize_5g(struct ath_hw *ah, s16 *nf)
145 -{
146 -       struct ath_common *common = ath9k_hw_common(ah);
147 -
148 -       if (*nf > ah->nf_5g_max) {
149 -               ath_print(common, ATH_DBG_CALIBRATE,
150 -                         "5 GHz NF (%d) > MAX (%d), "
151 -                         "correcting to MAX",
152 -                         *nf, ah->nf_5g_max);
153 -               *nf = ah->nf_5g_max;
154 -       } else if (*nf < ah->nf_5g_min) {
155 -               ath_print(common, ATH_DBG_CALIBRATE,
156 -                         "5 GHz NF (%d) < MIN (%d), "
157 -                         "correcting to MIN",
158 -                         *nf, ah->nf_5g_min);
159 -               *nf = ah->nf_5g_min;
160 -       }
161 -}
162 -
163 -static void ar9003_hw_nf_sanitize(struct ath_hw *ah, s16 *nf)
164 -{
165 -       if (IS_CHAN_2GHZ(ah->curchan))
166 -               ar9003_hw_nf_sanitize_2g(ah, nf);
167 -       else
168 -               ar9003_hw_nf_sanitize_5g(ah, nf);
169 -}
170 -
171  static void ar9003_hw_do_getnf(struct ath_hw *ah,
172                               int16_t nfarray[NUM_NF_READINGS])
173  {
174 @@ -1070,7 +1024,6 @@ static void ar9003_hw_do_getnf(struct at
175         nf = MS(REG_READ(ah, AR_PHY_CCA_0), AR_PHY_MINCCA_PWR);
176         if (nf & 0x100)
177                 nf = 0 - ((nf ^ 0x1ff) + 1);
178 -       ar9003_hw_nf_sanitize(ah, &nf);
179         ath_print(common, ATH_DBG_CALIBRATE,
180                   "NF calibrated [ctl] [chain 0] is %d\n", nf);
181         nfarray[0] = nf;
182 @@ -1078,7 +1031,6 @@ static void ar9003_hw_do_getnf(struct at
183         nf = MS(REG_READ(ah, AR_PHY_CCA_1), AR_PHY_CH1_MINCCA_PWR);
184         if (nf & 0x100)
185                 nf = 0 - ((nf ^ 0x1ff) + 1);
186 -       ar9003_hw_nf_sanitize(ah, &nf);
187         ath_print(common, ATH_DBG_CALIBRATE,
188                   "NF calibrated [ctl] [chain 1] is %d\n", nf);
189         nfarray[1] = nf;
190 @@ -1086,7 +1038,6 @@ static void ar9003_hw_do_getnf(struct at
191         nf = MS(REG_READ(ah, AR_PHY_CCA_2), AR_PHY_CH2_MINCCA_PWR);
192         if (nf & 0x100)
193                 nf = 0 - ((nf ^ 0x1ff) + 1);
194 -       ar9003_hw_nf_sanitize(ah, &nf);
195         ath_print(common, ATH_DBG_CALIBRATE,
196                   "NF calibrated [ctl] [chain 2] is %d\n", nf);
197         nfarray[2] = nf;
198 @@ -1094,7 +1045,6 @@ static void ar9003_hw_do_getnf(struct at
199         nf = MS(REG_READ(ah, AR_PHY_EXT_CCA), AR_PHY_EXT_MINCCA_PWR);
200         if (nf & 0x100)
201                 nf = 0 - ((nf ^ 0x1ff) + 1);
202 -       ar9003_hw_nf_sanitize(ah, &nf);
203         ath_print(common, ATH_DBG_CALIBRATE,
204                   "NF calibrated [ext] [chain 0] is %d\n", nf);
205         nfarray[3] = nf;
206 @@ -1102,7 +1052,6 @@ static void ar9003_hw_do_getnf(struct at
207         nf = MS(REG_READ(ah, AR_PHY_EXT_CCA_1), AR_PHY_CH1_EXT_MINCCA_PWR);
208         if (nf & 0x100)
209                 nf = 0 - ((nf ^ 0x1ff) + 1);
210 -       ar9003_hw_nf_sanitize(ah, &nf);
211         ath_print(common, ATH_DBG_CALIBRATE,
212                   "NF calibrated [ext] [chain 1] is %d\n", nf);
213         nfarray[4] = nf;
214 @@ -1110,18 +1059,19 @@ static void ar9003_hw_do_getnf(struct at
215         nf = MS(REG_READ(ah, AR_PHY_EXT_CCA_2), AR_PHY_CH2_EXT_MINCCA_PWR);
216         if (nf & 0x100)
217                 nf = 0 - ((nf ^ 0x1ff) + 1);
218 -       ar9003_hw_nf_sanitize(ah, &nf);
219         ath_print(common, ATH_DBG_CALIBRATE,
220                   "NF calibrated [ext] [chain 2] is %d\n", nf);
221         nfarray[5] = nf;
222  }
223  
224 -void ar9003_hw_set_nf_limits(struct ath_hw *ah)
225 +static void ar9003_hw_set_nf_limits(struct ath_hw *ah)
226  {
227 -       ah->nf_2g_max = AR_PHY_CCA_MAX_GOOD_VAL_9300_2GHZ;
228 -       ah->nf_2g_min = AR_PHY_CCA_MIN_GOOD_VAL_9300_2GHZ;
229 -       ah->nf_5g_max = AR_PHY_CCA_MAX_GOOD_VAL_9300_5GHZ;
230 -       ah->nf_5g_min = AR_PHY_CCA_MIN_GOOD_VAL_9300_5GHZ;
231 +       ah->nf_2g.max = AR_PHY_CCA_MAX_GOOD_VAL_9300_2GHZ;
232 +       ah->nf_2g.min = AR_PHY_CCA_MIN_GOOD_VAL_9300_2GHZ;
233 +       ah->nf_2g.nominal = AR_PHY_CCA_NOM_VAL_9300_2GHZ;
234 +       ah->nf_5g.max = AR_PHY_CCA_MAX_GOOD_VAL_9300_5GHZ;
235 +       ah->nf_5g.min = AR_PHY_CCA_MIN_GOOD_VAL_9300_5GHZ;
236 +       ah->nf_5g.nominal = AR_PHY_CCA_NOM_VAL_9300_5GHZ;
237  }
238  
239  /*
240 @@ -1309,6 +1259,8 @@ void ar9003_hw_attach_phy_ops(struct ath
241         priv_ops->do_getnf = ar9003_hw_do_getnf;
242         priv_ops->loadnf = ar9003_hw_loadnf;
243         priv_ops->ani_cache_ini_regs = ar9003_hw_ani_cache_ini_regs;
244 +
245 +       ar9003_hw_set_nf_limits(ah);
246  }
247  
248  void ar9003_hw_bb_watchdog_config(struct ath_hw *ah)
249 --- a/drivers/net/wireless/ath/ath9k/calib.c
250 +++ b/drivers/net/wireless/ath/ath9k/calib.c
251 @@ -74,13 +74,8 @@ static void ath9k_hw_update_nfcal_hist_b
252                         h[i].currIndex = 0;
253  
254                 if (h[i].invalidNFcount > 0) {
255 -                       if (nfarray[i] < AR_PHY_CCA_MIN_BAD_VALUE ||
256 -                           nfarray[i] > AR_PHY_CCA_MAX_HIGH_VALUE) {
257 -                               h[i].invalidNFcount = ATH9K_NF_CAL_HIST_MAX;
258 -                       } else {
259 -                               h[i].invalidNFcount--;
260 -                               h[i].privNF = nfarray[i];
261 -                       }
262 +                       h[i].invalidNFcount--;
263 +                       h[i].privNF = nfarray[i];
264                 } else {
265                         h[i].privNF =
266                                 ath9k_hw_get_nf_hist_mid(h[i].nfCalBuffer);
267 @@ -172,6 +167,35 @@ void ath9k_hw_start_nfcal(struct ath_hw 
268         REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
269  }
270  
271 +static void ath9k_hw_nf_sanitize(struct ath_hw *ah, s16 *nf)
272 +{
273 +       struct ath_common *common = ath9k_hw_common(ah);
274 +       struct ath_nf_limits *limit;
275 +       int i;
276 +
277 +       if (IS_CHAN_2GHZ(ah->curchan))
278 +               limit = &ah->nf_2g;
279 +       else
280 +               limit = &ah->nf_5g;
281 +
282 +       for (i = 0; i < NUM_NF_READINGS; i++) {
283 +               if (!nf[i])
284 +                       continue;
285 +
286 +               if (nf[i] > limit->max) {
287 +                       ath_print(common, ATH_DBG_CALIBRATE,
288 +                                 "NF[%d] (%d) > MAX (%d), correcting to MAX",
289 +                                 i, nf[i], limit->max);
290 +                       nf[i] = limit->max;
291 +               } else if (nf[i] < limit->min) {
292 +                       ath_print(common, ATH_DBG_CALIBRATE,
293 +                                 "NF[%d] (%d) < MIN (%d), correcting to NOM",
294 +                                 i, nf[i], limit->min);
295 +                       nf[i] = limit->nominal;
296 +               }
297 +       }
298 +}
299 +
300  int16_t ath9k_hw_getnf(struct ath_hw *ah,
301                        struct ath9k_channel *chan)
302  {
303 @@ -190,6 +214,7 @@ int16_t ath9k_hw_getnf(struct ath_hw *ah
304                 return chan->rawNoiseFloor;
305         } else {
306                 ath9k_hw_do_getnf(ah, nfarray);
307 +               ath9k_hw_nf_sanitize(ah, nfarray);
308                 nf = nfarray[0];
309                 if (ath9k_hw_get_nf_thresh(ah, c->band, &nfThresh)
310                     && nf > nfThresh) {
311 @@ -211,25 +236,21 @@ int16_t ath9k_hw_getnf(struct ath_hw *ah
312  
313  void ath9k_init_nfcal_hist_buffer(struct ath_hw *ah)
314  {
315 +       struct ath_nf_limits *limit;
316         int i, j;
317 -       s16 noise_floor;
318  
319 -       if (AR_SREV_9280(ah))
320 -               noise_floor = AR_PHY_CCA_MAX_AR9280_GOOD_VALUE;
321 -       else if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
322 -               noise_floor = AR_PHY_CCA_MAX_AR9285_GOOD_VALUE;
323 -       else if (AR_SREV_9287(ah))
324 -               noise_floor = AR_PHY_CCA_MAX_AR9287_GOOD_VALUE;
325 +       if (!ah->curchan || IS_CHAN_2GHZ(ah->curchan))
326 +               limit = &ah->nf_2g;
327         else
328 -               noise_floor = AR_PHY_CCA_MAX_AR5416_GOOD_VALUE;
329 +               limit = &ah->nf_5g;
330  
331         for (i = 0; i < NUM_NF_READINGS; i++) {
332                 ah->nfCalHist[i].currIndex = 0;
333 -               ah->nfCalHist[i].privNF = noise_floor;
334 +               ah->nfCalHist[i].privNF = limit->nominal;
335                 ah->nfCalHist[i].invalidNFcount =
336                         AR_PHY_CCA_FILTERWINDOW_LENGTH;
337                 for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) {
338 -                       ah->nfCalHist[i].nfCalBuffer[j] = noise_floor;
339 +                       ah->nfCalHist[i].nfCalBuffer[j] = limit->nominal;
340                 }
341         }
342  }
343 --- a/drivers/net/wireless/ath/ath9k/calib.h
344 +++ b/drivers/net/wireless/ath/ath9k/calib.h
345 @@ -19,12 +19,6 @@
346  
347  #include "hw.h"
348  
349 -#define AR_PHY_CCA_MAX_AR5416_GOOD_VALUE       -85
350 -#define AR_PHY_CCA_MAX_AR9280_GOOD_VALUE       -112
351 -#define AR_PHY_CCA_MAX_AR9285_GOOD_VALUE       -118
352 -#define AR_PHY_CCA_MAX_AR9287_GOOD_VALUE       -118
353 -#define AR_PHY_CCA_MAX_HIGH_VALUE                      -62
354 -#define AR_PHY_CCA_MIN_BAD_VALUE                       -140
355  #define AR_PHY_CCA_FILTERWINDOW_LENGTH_INIT     3
356  #define AR_PHY_CCA_FILTERWINDOW_LENGTH          5
357  
358 --- a/drivers/net/wireless/ath/ath9k/hw.c
359 +++ b/drivers/net/wireless/ath/ath9k/hw.c
360 @@ -621,9 +621,6 @@ static int __ath9k_hw_init(struct ath_hw
361         else
362                 ah->tx_trig_level = (AR_FTRIG_512B >> AR_FTRIG_S);
363  
364 -       if (AR_SREV_9300_20_OR_LATER(ah))
365 -               ar9003_hw_set_nf_limits(ah);
366 -
367         ath9k_init_nfcal_hist_buffer(ah);
368         ah->bb_watchdog_timeout_ms = 25;
369  
370 --- a/drivers/net/wireless/ath/ath9k/hw.h
371 +++ b/drivers/net/wireless/ath/ath9k/hw.h
372 @@ -630,6 +630,12 @@ struct ath_hw_ops {
373         void (*ani_monitor)(struct ath_hw *ah, struct ath9k_channel *chan);
374  };
375  
376 +struct ath_nf_limits {
377 +       s16 max;
378 +       s16 min;
379 +       s16 nominal;
380 +};
381 +
382  struct ath_hw {
383         struct ieee80211_hw *hw;
384         struct ath_common common;
385 @@ -651,10 +657,9 @@ struct ath_hw {
386         bool is_pciexpress;
387         bool need_an_top2_fixup;
388         u16 tx_trig_level;
389 -       s16 nf_2g_max;
390 -       s16 nf_2g_min;
391 -       s16 nf_5g_max;
392 -       s16 nf_5g_min;
393 +
394 +       struct ath_nf_limits nf_2g;
395 +       struct ath_nf_limits nf_5g;
396         u16 rfsilent;
397         u32 rfkill_gpio;
398         u32 rfkill_polarity;
399 @@ -945,7 +950,6 @@ void ar9002_hw_enable_wep_aggregation(st
400   * Code specific to AR9003, we stuff these here to avoid callbacks
401   * for older families
402   */
403 -void ar9003_hw_set_nf_limits(struct ath_hw *ah);
404  void ar9003_hw_bb_watchdog_config(struct ath_hw *ah);
405  void ar9003_hw_bb_watchdog_read(struct ath_hw *ah);
406  void ar9003_hw_bb_watchdog_dbg_info(struct ath_hw *ah);