]> Pileus Git - ~andy/linux/blob - drivers/net/wireless/iwlwifi/iwl-agn-rx.c
iwlagn: add statistic notification structure for WiFi/BT devices
[~andy/linux] / drivers / net / wireless / iwlwifi / iwl-agn-rx.c
1 /******************************************************************************
2  *
3  * GPL LICENSE SUMMARY
4  *
5  * Copyright(c) 2008 - 2010 Intel Corporation. All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of version 2 of the GNU General Public License as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19  * USA
20  *
21  * The full GNU General Public License is included in this distribution
22  * in the file called LICENSE.GPL.
23  *
24  * Contact Information:
25  *  Intel Linux Wireless <ilw@linux.intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *
28  *****************************************************************************/
29
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/sched.h>
34
35 #include "iwl-dev.h"
36 #include "iwl-core.h"
37 #include "iwl-calib.h"
38 #include "iwl-sta.h"
39 #include "iwl-io.h"
40 #include "iwl-helpers.h"
41 #include "iwl-agn-hw.h"
42 #include "iwl-agn.h"
43
44 void iwl_rx_missed_beacon_notif(struct iwl_priv *priv,
45                                 struct iwl_rx_mem_buffer *rxb)
46
47 {
48         struct iwl_rx_packet *pkt = rxb_addr(rxb);
49         struct iwl_missed_beacon_notif *missed_beacon;
50
51         missed_beacon = &pkt->u.missed_beacon;
52         if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
53             priv->missed_beacon_threshold) {
54                 IWL_DEBUG_CALIB(priv,
55                     "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
56                     le32_to_cpu(missed_beacon->consecutive_missed_beacons),
57                     le32_to_cpu(missed_beacon->total_missed_becons),
58                     le32_to_cpu(missed_beacon->num_recvd_beacons),
59                     le32_to_cpu(missed_beacon->num_expected_beacons));
60                 if (!test_bit(STATUS_SCANNING, &priv->status))
61                         iwl_init_sensitivity(priv);
62         }
63 }
64
65 /* Calculate noise level, based on measurements during network silence just
66  *   before arriving beacon.  This measurement can be done only if we know
67  *   exactly when to expect beacons, therefore only when we're associated. */
68 static void iwl_rx_calc_noise(struct iwl_priv *priv)
69 {
70         struct statistics_rx_non_phy *rx_info
71                                 = &(priv->_agn.statistics.rx.general);
72         int num_active_rx = 0;
73         int total_silence = 0;
74         int bcn_silence_a =
75                 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
76         int bcn_silence_b =
77                 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
78         int bcn_silence_c =
79                 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
80         int last_rx_noise;
81
82         if (bcn_silence_a) {
83                 total_silence += bcn_silence_a;
84                 num_active_rx++;
85         }
86         if (bcn_silence_b) {
87                 total_silence += bcn_silence_b;
88                 num_active_rx++;
89         }
90         if (bcn_silence_c) {
91                 total_silence += bcn_silence_c;
92                 num_active_rx++;
93         }
94
95         /* Average among active antennas */
96         if (num_active_rx)
97                 last_rx_noise = (total_silence / num_active_rx) - 107;
98         else
99                 last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
100
101         IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
102                         bcn_silence_a, bcn_silence_b, bcn_silence_c,
103                         last_rx_noise);
104 }
105
106 #ifdef CONFIG_IWLWIFI_DEBUGFS
107 /*
108  *  based on the assumption of all statistics counter are in DWORD
109  *  FIXME: This function is for debugging, do not deal with
110  *  the case of counters roll-over.
111  */
112 static void iwl_accumulative_statistics(struct iwl_priv *priv,
113                                         __le32 *stats)
114 {
115         int i;
116         __le32 *prev_stats;
117         u32 *accum_stats;
118         u32 *delta, *max_delta;
119
120         prev_stats = (__le32 *)&priv->_agn.statistics;
121         accum_stats = (u32 *)&priv->_agn.accum_statistics;
122         delta = (u32 *)&priv->_agn.delta_statistics;
123         max_delta = (u32 *)&priv->_agn.max_delta;
124
125         for (i = sizeof(__le32); i < sizeof(struct iwl_notif_statistics);
126              i += sizeof(__le32), stats++, prev_stats++, delta++,
127              max_delta++, accum_stats++) {
128                 if (le32_to_cpu(*stats) > le32_to_cpu(*prev_stats)) {
129                         *delta = (le32_to_cpu(*stats) -
130                                 le32_to_cpu(*prev_stats));
131                         *accum_stats += *delta;
132                         if (*delta > *max_delta)
133                                 *max_delta = *delta;
134                 }
135         }
136
137         /* reset accumulative statistics for "no-counter" type statistics */
138         priv->_agn.accum_statistics.general.common.temperature =
139                 priv->_agn.statistics.general.common.temperature;
140         priv->_agn.accum_statistics.general.common.temperature_m =
141                 priv->_agn.statistics.general.common.temperature_m;
142         priv->_agn.accum_statistics.general.common.ttl_timestamp =
143                 priv->_agn.statistics.general.common.ttl_timestamp;
144         priv->_agn.accum_statistics.tx.tx_power.ant_a =
145                 priv->_agn.statistics.tx.tx_power.ant_a;
146         priv->_agn.accum_statistics.tx.tx_power.ant_b =
147                 priv->_agn.statistics.tx.tx_power.ant_b;
148         priv->_agn.accum_statistics.tx.tx_power.ant_c =
149                 priv->_agn.statistics.tx.tx_power.ant_c;
150 }
151 #endif
152
153 #define REG_RECALIB_PERIOD (60)
154
155 /**
156  * iwl_good_plcp_health - checks for plcp error.
157  *
158  * When the plcp error is exceeding the thresholds, reset the radio
159  * to improve the throughput.
160  */
161 bool iwl_good_plcp_health(struct iwl_priv *priv,
162                                 struct iwl_rx_packet *pkt)
163 {
164         bool rc = true;
165         int combined_plcp_delta;
166         unsigned int plcp_msec;
167         unsigned long plcp_received_jiffies;
168
169         if (priv->cfg->plcp_delta_threshold ==
170             IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE) {
171                 IWL_DEBUG_RADIO(priv, "plcp_err check disabled\n");
172                 return rc;
173         }
174
175         /*
176          * check for plcp_err and trigger radio reset if it exceeds
177          * the plcp error threshold plcp_delta.
178          */
179         plcp_received_jiffies = jiffies;
180         plcp_msec = jiffies_to_msecs((long) plcp_received_jiffies -
181                                         (long) priv->plcp_jiffies);
182         priv->plcp_jiffies = plcp_received_jiffies;
183         /*
184          * check to make sure plcp_msec is not 0 to prevent division
185          * by zero.
186          */
187         if (plcp_msec) {
188                 combined_plcp_delta =
189                         (le32_to_cpu(pkt->u.stats.rx.ofdm.plcp_err) -
190                         le32_to_cpu(priv->_agn.statistics.rx.ofdm.plcp_err)) +
191                         (le32_to_cpu(pkt->u.stats.rx.ofdm_ht.plcp_err) -
192                         le32_to_cpu(priv->_agn.statistics.rx.ofdm_ht.plcp_err));
193
194                 if ((combined_plcp_delta > 0) &&
195                     ((combined_plcp_delta * 100) / plcp_msec) >
196                         priv->cfg->plcp_delta_threshold) {
197                         /*
198                          * if plcp_err exceed the threshold,
199                          * the following data is printed in csv format:
200                          *    Text: plcp_err exceeded %d,
201                          *    Received ofdm.plcp_err,
202                          *    Current ofdm.plcp_err,
203                          *    Received ofdm_ht.plcp_err,
204                          *    Current ofdm_ht.plcp_err,
205                          *    combined_plcp_delta,
206                          *    plcp_msec
207                          */
208                         IWL_DEBUG_RADIO(priv, "plcp_err exceeded %u, "
209                                 "%u, %u, %u, %u, %d, %u mSecs\n",
210                                 priv->cfg->plcp_delta_threshold,
211                                 le32_to_cpu(pkt->u.stats.rx.ofdm.plcp_err),
212                                 le32_to_cpu(
213                                        priv->_agn.statistics.rx.ofdm.plcp_err),
214                                 le32_to_cpu(pkt->u.stats.rx.ofdm_ht.plcp_err),
215                                 le32_to_cpu(
216                                   priv->_agn.statistics.rx.ofdm_ht.plcp_err),
217                                 combined_plcp_delta, plcp_msec);
218                         rc = false;
219                 }
220         }
221         return rc;
222 }
223
224 void iwl_rx_statistics(struct iwl_priv *priv,
225                               struct iwl_rx_mem_buffer *rxb)
226 {
227         int change;
228         struct iwl_rx_packet *pkt = rxb_addr(rxb);
229
230
231         IWL_DEBUG_RX(priv, "Statistics notification received (%d vs %d).\n",
232                      (int)sizeof(priv->_agn.statistics),
233                      le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK);
234
235         change = ((priv->_agn.statistics.general.common.temperature !=
236                    pkt->u.stats.general.common.temperature) ||
237                   ((priv->_agn.statistics.flag &
238                     STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
239                    (pkt->u.stats.flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK)));
240
241 #ifdef CONFIG_IWLWIFI_DEBUGFS
242         iwl_accumulative_statistics(priv, (__le32 *)&pkt->u.stats);
243 #endif
244         iwl_recover_from_statistics(priv, pkt);
245
246         memcpy(&priv->_agn.statistics, &pkt->u.stats,
247                sizeof(priv->_agn.statistics));
248
249         set_bit(STATUS_STATISTICS, &priv->status);
250
251         /* Reschedule the statistics timer to occur in
252          * REG_RECALIB_PERIOD seconds to ensure we get a
253          * thermal update even if the uCode doesn't give
254          * us one */
255         mod_timer(&priv->statistics_periodic, jiffies +
256                   msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
257
258         if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
259             (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
260                 iwl_rx_calc_noise(priv);
261                 queue_work(priv->workqueue, &priv->run_time_calib_work);
262         }
263         if (priv->cfg->ops->lib->temp_ops.temperature && change)
264                 priv->cfg->ops->lib->temp_ops.temperature(priv);
265 }
266
267 void iwl_reply_statistics(struct iwl_priv *priv,
268                               struct iwl_rx_mem_buffer *rxb)
269 {
270         struct iwl_rx_packet *pkt = rxb_addr(rxb);
271
272         if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATISTICS_CLEAR_MSK) {
273 #ifdef CONFIG_IWLWIFI_DEBUGFS
274                 memset(&priv->_agn.accum_statistics, 0,
275                         sizeof(struct iwl_notif_statistics));
276                 memset(&priv->_agn.delta_statistics, 0,
277                         sizeof(struct iwl_notif_statistics));
278                 memset(&priv->_agn.max_delta, 0,
279                         sizeof(struct iwl_notif_statistics));
280 #endif
281                 IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
282         }
283         iwl_rx_statistics(priv, rxb);
284 }