blob: bcd7f64683aa02c06ebd1af0e8650ef39d8fdd11 [file] [log] [blame]
Tomas Winklera55360e2008-05-05 10:22:28 +08001/******************************************************************************
2 *
Wey-Yi Guy901069c2011-04-05 09:42:00 -07003 * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
Tomas Winklera55360e2008-05-05 10:22:28 +08004 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
Winkler, Tomas759ef892008-12-09 11:28:58 -080025 * Intel Linux Wireless <ilw@linux.intel.com>
Tomas Winklera55360e2008-05-05 10:22:28 +080026 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
Emmanuel Grumbach1781a072008-06-30 17:23:09 +080030#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Stanislaw Gruszka118253c2011-03-07 09:22:24 +010032#include <linux/sched.h>
Tomas Winklera55360e2008-05-05 10:22:28 +080033#include <net/mac80211.h>
Tomas Winklera05ffd32008-07-10 14:28:42 +030034#include <asm/unaligned.h>
Tomas Winklera55360e2008-05-05 10:22:28 +080035#include "iwl-eeprom.h"
36#include "iwl-dev.h"
37#include "iwl-core.h"
38#include "iwl-sta.h"
39#include "iwl-io.h"
40#include "iwl-helpers.h"
Stanislaw Gruszka67289942011-02-28 14:33:17 +010041#include "iwl-agn-calib.h"
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +010042#include "iwl-agn.h"
Emmanuel Grumbach48f20d32011-08-25 23:10:36 -070043#include "iwl-shared.h"
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +010044
Emmanuel Grumbach522376d2011-09-06 09:31:19 -070045const char *get_cmd_string(u8 cmd)
46{
47 switch (cmd) {
48 IWL_CMD(REPLY_ALIVE);
49 IWL_CMD(REPLY_ERROR);
50 IWL_CMD(REPLY_RXON);
51 IWL_CMD(REPLY_RXON_ASSOC);
52 IWL_CMD(REPLY_QOS_PARAM);
53 IWL_CMD(REPLY_RXON_TIMING);
54 IWL_CMD(REPLY_ADD_STA);
55 IWL_CMD(REPLY_REMOVE_STA);
56 IWL_CMD(REPLY_REMOVE_ALL_STA);
57 IWL_CMD(REPLY_TXFIFO_FLUSH);
58 IWL_CMD(REPLY_WEPKEY);
59 IWL_CMD(REPLY_TX);
60 IWL_CMD(REPLY_LEDS_CMD);
61 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
62 IWL_CMD(COEX_PRIORITY_TABLE_CMD);
63 IWL_CMD(COEX_MEDIUM_NOTIFICATION);
64 IWL_CMD(COEX_EVENT_CMD);
65 IWL_CMD(REPLY_QUIET_CMD);
66 IWL_CMD(REPLY_CHANNEL_SWITCH);
67 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
68 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
69 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
70 IWL_CMD(POWER_TABLE_CMD);
71 IWL_CMD(PM_SLEEP_NOTIFICATION);
72 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
73 IWL_CMD(REPLY_SCAN_CMD);
74 IWL_CMD(REPLY_SCAN_ABORT_CMD);
75 IWL_CMD(SCAN_START_NOTIFICATION);
76 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
77 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
78 IWL_CMD(BEACON_NOTIFICATION);
79 IWL_CMD(REPLY_TX_BEACON);
80 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
81 IWL_CMD(QUIET_NOTIFICATION);
82 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
83 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
84 IWL_CMD(REPLY_BT_CONFIG);
85 IWL_CMD(REPLY_STATISTICS_CMD);
86 IWL_CMD(STATISTICS_NOTIFICATION);
87 IWL_CMD(REPLY_CARD_STATE_CMD);
88 IWL_CMD(CARD_STATE_NOTIFICATION);
89 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
90 IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
91 IWL_CMD(SENSITIVITY_CMD);
92 IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
93 IWL_CMD(REPLY_RX_PHY_CMD);
94 IWL_CMD(REPLY_RX_MPDU_CMD);
95 IWL_CMD(REPLY_RX);
96 IWL_CMD(REPLY_COMPRESSED_BA);
97 IWL_CMD(CALIBRATION_CFG_CMD);
98 IWL_CMD(CALIBRATION_RES_NOTIFICATION);
99 IWL_CMD(CALIBRATION_COMPLETE_NOTIFICATION);
100 IWL_CMD(REPLY_TX_POWER_DBM_CMD);
101 IWL_CMD(TEMPERATURE_NOTIFICATION);
102 IWL_CMD(TX_ANT_CONFIGURATION_CMD);
103 IWL_CMD(REPLY_BT_COEX_PROFILE_NOTIF);
104 IWL_CMD(REPLY_BT_COEX_PRIO_TABLE);
105 IWL_CMD(REPLY_BT_COEX_PROT_ENV);
106 IWL_CMD(REPLY_WIPAN_PARAMS);
107 IWL_CMD(REPLY_WIPAN_RXON);
108 IWL_CMD(REPLY_WIPAN_RXON_TIMING);
109 IWL_CMD(REPLY_WIPAN_RXON_ASSOC);
110 IWL_CMD(REPLY_WIPAN_QOS_PARAM);
111 IWL_CMD(REPLY_WIPAN_WEPKEY);
112 IWL_CMD(REPLY_WIPAN_P2P_CHANNEL_SWITCH);
113 IWL_CMD(REPLY_WIPAN_NOA_NOTIFICATION);
114 IWL_CMD(REPLY_WIPAN_DEACTIVATION_COMPLETE);
115 IWL_CMD(REPLY_WOWLAN_PATTERNS);
116 IWL_CMD(REPLY_WOWLAN_WAKEUP_FILTER);
117 IWL_CMD(REPLY_WOWLAN_TSC_RSC_PARAMS);
118 IWL_CMD(REPLY_WOWLAN_TKIP_PARAMS);
119 IWL_CMD(REPLY_WOWLAN_KEK_KCK_MATERIAL);
120 IWL_CMD(REPLY_WOWLAN_GET_STATUS);
121 default:
122 return "UNKNOWN";
123
124 }
125}
Tomas Winklera55360e2008-05-05 10:22:28 +0800126
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100127/******************************************************************************
128 *
129 * Generic RX handler implementations
130 *
131 ******************************************************************************/
132
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700133static int iwl_rx_reply_error(struct iwl_priv *priv,
134 struct iwl_rx_mem_buffer *rxb,
135 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100136{
137 struct iwl_rx_packet *pkt = rxb_addr(rxb);
138
139 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
140 "seq 0x%04X ser 0x%08X\n",
141 le32_to_cpu(pkt->u.err_resp.error_type),
142 get_cmd_string(pkt->u.err_resp.cmd_id),
143 pkt->u.err_resp.cmd_id,
144 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
145 le32_to_cpu(pkt->u.err_resp.error_info));
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700146 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100147}
148
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700149static int iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb,
150 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100151{
152 struct iwl_rx_packet *pkt = rxb_addr(rxb);
153 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
154 /*
155 * MULTI-FIXME
156 * See iwl_mac_channel_switch.
157 */
158 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
159 struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
160
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700161 if (!test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status))
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700162 return 0;
Stanislaw Gruszka6f213ff2011-06-02 18:17:15 +0200163
164 if (!le32_to_cpu(csa->status) && csa->channel == priv->switch_channel) {
165 rxon->channel = csa->channel;
166 ctx->staging.channel = csa->channel;
167 IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100168 le16_to_cpu(csa->channel));
Stanislaw Gruszka6f213ff2011-06-02 18:17:15 +0200169 iwl_chswitch_done(priv, true);
170 } else {
171 IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
172 le16_to_cpu(csa->channel));
173 iwl_chswitch_done(priv, false);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100174 }
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700175 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100176}
177
178
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700179static int iwl_rx_spectrum_measure_notif(struct iwl_priv *priv,
180 struct iwl_rx_mem_buffer *rxb,
181 struct iwl_device_cmd *cmd)
Reinette Chatre81963d62010-01-22 14:22:57 -0800182{
183 struct iwl_rx_packet *pkt = rxb_addr(rxb);
184 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
185
186 if (!report->state) {
187 IWL_DEBUG_11H(priv,
188 "Spectrum Measure Notification: Start\n");
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700189 return 0;
Reinette Chatre81963d62010-01-22 14:22:57 -0800190 }
191
192 memcpy(&priv->measure_report, report, sizeof(*report));
193 priv->measurement_status |= MEASUREMENT_READY;
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700194 return 0;
Reinette Chatre81963d62010-01-22 14:22:57 -0800195}
Reinette Chatre81963d62010-01-22 14:22:57 -0800196
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700197static int iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
198 struct iwl_rx_mem_buffer *rxb,
199 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100200{
201#ifdef CONFIG_IWLWIFI_DEBUG
202 struct iwl_rx_packet *pkt = rxb_addr(rxb);
203 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
204 IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
205 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
206#endif
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700207 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100208}
209
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700210static int iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
211 struct iwl_rx_mem_buffer *rxb,
212 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100213{
214 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Emmanuel Grumbach44856c62011-08-25 23:10:45 -0700215 u32 __maybe_unused len =
216 le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100217 IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
218 "notification for %s:\n", len,
219 get_cmd_string(pkt->hdr.cmd));
220 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, len);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700221 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100222}
223
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700224static int iwl_rx_beacon_notif(struct iwl_priv *priv,
225 struct iwl_rx_mem_buffer *rxb,
226 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100227{
228 struct iwl_rx_packet *pkt = rxb_addr(rxb);
229 struct iwlagn_beacon_notif *beacon = (void *)pkt->u.raw;
230#ifdef CONFIG_IWLWIFI_DEBUG
231 u16 status = le16_to_cpu(beacon->beacon_notify_hdr.status.status);
232 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
233
234 IWL_DEBUG_RX(priv, "beacon status %#x, retries:%d ibssmgr:%d "
235 "tsf:0x%.8x%.8x rate:%d\n",
236 status & TX_STATUS_MSK,
237 beacon->beacon_notify_hdr.failure_frame,
238 le32_to_cpu(beacon->ibss_mgr_status),
239 le32_to_cpu(beacon->high_tsf),
240 le32_to_cpu(beacon->low_tsf), rate);
241#endif
242
243 priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
244
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700245 if (!test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Emmanuel Grumbach74e28e42011-08-25 23:10:41 -0700246 queue_work(priv->shrd->workqueue, &priv->beacon_update);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700247 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100248}
249
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100250/* the threshold ratio of actual_ack_cnt to expected_ack_cnt in percent */
251#define ACK_CNT_RATIO (50)
252#define BA_TIMEOUT_CNT (5)
253#define BA_TIMEOUT_MAX (16)
254
255/**
256 * iwl_good_ack_health - checks for ACK count ratios, BA timeout retries.
257 *
258 * When the ACK count ratio is low and aggregated BA timeout retries exceeding
259 * the BA_TIMEOUT_MAX, reload firmware and bring system back to normal
260 * operation state.
261 */
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700262static bool iwl_good_ack_health(struct iwl_priv *priv,
263 struct statistics_tx *cur)
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100264{
265 int actual_delta, expected_delta, ba_timeout_delta;
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700266 struct statistics_tx *old;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100267
Wey-Yi Guy898ed672011-07-13 08:38:57 -0700268 if (priv->agg_tids_count)
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100269 return true;
270
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700271 old = &priv->statistics.tx;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100272
273 actual_delta = le32_to_cpu(cur->actual_ack_cnt) -
274 le32_to_cpu(old->actual_ack_cnt);
275 expected_delta = le32_to_cpu(cur->expected_ack_cnt) -
276 le32_to_cpu(old->expected_ack_cnt);
277
278 /* Values should not be negative, but we do not trust the firmware */
279 if (actual_delta <= 0 || expected_delta <= 0)
280 return true;
281
282 ba_timeout_delta = le32_to_cpu(cur->agg.ba_timeout) -
283 le32_to_cpu(old->agg.ba_timeout);
284
285 if ((actual_delta * 100 / expected_delta) < ACK_CNT_RATIO &&
286 ba_timeout_delta > BA_TIMEOUT_CNT) {
287 IWL_DEBUG_RADIO(priv, "deltas: actual %d expected %d ba_timeout %d\n",
288 actual_delta, expected_delta, ba_timeout_delta);
289
290#ifdef CONFIG_IWLWIFI_DEBUGFS
291 /*
292 * This is ifdef'ed on DEBUGFS because otherwise the
293 * statistics aren't available. If DEBUGFS is set but
294 * DEBUG is not, these will just compile out.
295 */
296 IWL_DEBUG_RADIO(priv, "rx_detected_cnt delta %d\n",
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700297 priv->delta_stats.tx.rx_detected_cnt);
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100298 IWL_DEBUG_RADIO(priv,
299 "ack_or_ba_timeout_collision delta %d\n",
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700300 priv->delta_stats.tx.ack_or_ba_timeout_collision);
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100301#endif
302
303 if (ba_timeout_delta >= BA_TIMEOUT_MAX)
304 return false;
305 }
306
307 return true;
308}
309
310/**
311 * iwl_good_plcp_health - checks for plcp error.
312 *
313 * When the plcp error is exceeding the thresholds, reset the radio
314 * to improve the throughput.
315 */
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100316static bool iwl_good_plcp_health(struct iwl_priv *priv,
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700317 struct statistics_rx_phy *cur_ofdm,
318 struct statistics_rx_ht_phy *cur_ofdm_ht,
319 unsigned int msecs)
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100320{
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100321 int delta;
322 int threshold = priv->cfg->base_params->plcp_delta_threshold;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100323
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100324 if (threshold == IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE) {
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100325 IWL_DEBUG_RADIO(priv, "plcp_err check disabled\n");
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100326 return true;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100327 }
328
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700329 delta = le32_to_cpu(cur_ofdm->plcp_err) -
330 le32_to_cpu(priv->statistics.rx_ofdm.plcp_err) +
331 le32_to_cpu(cur_ofdm_ht->plcp_err) -
332 le32_to_cpu(priv->statistics.rx_ofdm_ht.plcp_err);
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100333
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700334 /* Can be negative if firmware reset statistics */
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100335 if (delta <= 0)
336 return true;
337
338 if ((delta * 100 / msecs) > threshold) {
339 IWL_DEBUG_RADIO(priv,
340 "plcp health threshold %u delta %d msecs %u\n",
341 threshold, delta, msecs);
342 return false;
343 }
344
345 return true;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100346}
347
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100348static void iwl_recover_from_statistics(struct iwl_priv *priv,
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700349 struct statistics_rx_phy *cur_ofdm,
350 struct statistics_rx_ht_phy *cur_ofdm_ht,
351 struct statistics_tx *tx,
352 unsigned long stamp)
Wey-Yi Guyfa8f130c2010-03-05 14:22:46 -0800353{
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100354 unsigned int msecs;
Stanislaw Gruszkab7977ff2011-02-28 14:33:15 +0100355
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700356 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100357 return;
358
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100359 msecs = jiffies_to_msecs(stamp - priv->rx_statistics_jiffies);
360
361 /* Only gather statistics and update time stamp when not associated */
362 if (!iwl_is_any_associated(priv))
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700363 return;
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100364
365 /* Do not check/recover when do not have enough statistics data */
366 if (msecs < 99)
Wey-Yi Guyfa8f130c2010-03-05 14:22:46 -0800367 return;
Stanislaw Gruszkaca3d9382011-02-08 09:31:55 +0100368
Don Fry9d143e92011-04-20 15:23:57 -0700369 if (iwlagn_mod_params.ack_check && !iwl_good_ack_health(priv, tx)) {
Stanislaw Gruszkaca3d9382011-02-08 09:31:55 +0100370 IWL_ERR(priv, "low ack count detected, restart firmware\n");
371 if (!iwl_force_reset(priv, IWL_FW_RESET, false))
372 return;
Trieu 'Andrew' Nguyen3e4fb5f2010-01-22 14:22:46 -0800373 }
Stanislaw Gruszkaca3d9382011-02-08 09:31:55 +0100374
Don Fry9d143e92011-04-20 15:23:57 -0700375 if (iwlagn_mod_params.plcp_check &&
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700376 !iwl_good_plcp_health(priv, cur_ofdm, cur_ofdm_ht, msecs))
Stanislaw Gruszkaca3d9382011-02-08 09:31:55 +0100377 iwl_force_reset(priv, IWL_RF_RESET, false);
Wey-Yi Guybeac5492010-03-04 13:38:58 -0800378}
Wey-Yi Guybeac5492010-03-04 13:38:58 -0800379
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100380/* Calculate noise level, based on measurements during network silence just
381 * before arriving beacon. This measurement can be done only if we know
382 * exactly when to expect beacons, therefore only when we're associated. */
383static void iwl_rx_calc_noise(struct iwl_priv *priv)
384{
385 struct statistics_rx_non_phy *rx_info;
386 int num_active_rx = 0;
387 int total_silence = 0;
388 int bcn_silence_a, bcn_silence_b, bcn_silence_c;
389 int last_rx_noise;
390
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700391 rx_info = &priv->statistics.rx_non_phy;
392
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100393 bcn_silence_a =
394 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
395 bcn_silence_b =
396 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
397 bcn_silence_c =
398 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
399
400 if (bcn_silence_a) {
401 total_silence += bcn_silence_a;
402 num_active_rx++;
403 }
404 if (bcn_silence_b) {
405 total_silence += bcn_silence_b;
406 num_active_rx++;
407 }
408 if (bcn_silence_c) {
409 total_silence += bcn_silence_c;
410 num_active_rx++;
411 }
412
413 /* Average among active antennas */
414 if (num_active_rx)
415 last_rx_noise = (total_silence / num_active_rx) - 107;
416 else
417 last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
418
419 IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
420 bcn_silence_a, bcn_silence_b, bcn_silence_c,
421 last_rx_noise);
422}
423
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700424#ifdef CONFIG_IWLWIFI_DEBUGFS
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100425/*
426 * based on the assumption of all statistics counter are in DWORD
427 * FIXME: This function is for debugging, do not deal with
428 * the case of counters roll-over.
429 */
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700430static void accum_stats(__le32 *prev, __le32 *cur, __le32 *delta,
431 __le32 *max_delta, __le32 *accum, int size)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100432{
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700433 int i;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100434
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700435 for (i = 0;
436 i < size / sizeof(__le32);
437 i++, prev++, cur++, delta++, max_delta++, accum++) {
438 if (le32_to_cpu(*cur) > le32_to_cpu(*prev)) {
439 *delta = cpu_to_le32(
440 le32_to_cpu(*cur) - le32_to_cpu(*prev));
441 le32_add_cpu(accum, le32_to_cpu(*delta));
442 if (le32_to_cpu(*delta) > le32_to_cpu(*max_delta))
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100443 *max_delta = *delta;
444 }
445 }
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100446}
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100447
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700448static void
449iwl_accumulative_statistics(struct iwl_priv *priv,
450 struct statistics_general_common *common,
451 struct statistics_rx_non_phy *rx_non_phy,
452 struct statistics_rx_phy *rx_ofdm,
453 struct statistics_rx_ht_phy *rx_ofdm_ht,
454 struct statistics_rx_phy *rx_cck,
455 struct statistics_tx *tx,
456 struct statistics_bt_activity *bt_activity)
457{
458#define ACCUM(_name) \
459 accum_stats((__le32 *)&priv->statistics._name, \
460 (__le32 *)_name, \
461 (__le32 *)&priv->delta_stats._name, \
462 (__le32 *)&priv->max_delta_stats._name, \
463 (__le32 *)&priv->accum_stats._name, \
464 sizeof(*_name));
465
466 ACCUM(common);
467 ACCUM(rx_non_phy);
468 ACCUM(rx_ofdm);
469 ACCUM(rx_ofdm_ht);
470 ACCUM(rx_cck);
471 ACCUM(tx);
472 if (bt_activity)
473 ACCUM(bt_activity);
474#undef ACCUM
475}
476#else
477static inline void
478iwl_accumulative_statistics(struct iwl_priv *priv,
479 struct statistics_general_common *common,
480 struct statistics_rx_non_phy *rx_non_phy,
481 struct statistics_rx_phy *rx_ofdm,
482 struct statistics_rx_ht_phy *rx_ofdm_ht,
483 struct statistics_rx_phy *rx_cck,
484 struct statistics_tx *tx,
485 struct statistics_bt_activity *bt_activity)
486{
487}
488#endif
489
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700490static int iwl_rx_statistics(struct iwl_priv *priv,
491 struct iwl_rx_mem_buffer *rxb,
492 struct iwl_device_cmd *cmd)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100493{
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700494 unsigned long stamp = jiffies;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100495 const int reg_recalib_period = 60;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100496 int change;
497 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700498 u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
499 __le32 *flag;
500 struct statistics_general_common *common;
501 struct statistics_rx_non_phy *rx_non_phy;
502 struct statistics_rx_phy *rx_ofdm;
503 struct statistics_rx_ht_phy *rx_ofdm_ht;
504 struct statistics_rx_phy *rx_cck;
505 struct statistics_tx *tx;
506 struct statistics_bt_activity *bt_activity;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100507
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700508 len -= sizeof(struct iwl_cmd_header); /* skip header */
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100509
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700510 IWL_DEBUG_RX(priv, "Statistics notification received (%d bytes).\n",
511 len);
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100512
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700513 if (len == sizeof(struct iwl_bt_notif_statistics)) {
514 struct iwl_bt_notif_statistics *stats;
515 stats = &pkt->u.stats_bt;
516 flag = &stats->flag;
517 common = &stats->general.common;
518 rx_non_phy = &stats->rx.general.common;
519 rx_ofdm = &stats->rx.ofdm;
520 rx_ofdm_ht = &stats->rx.ofdm_ht;
521 rx_cck = &stats->rx.cck;
522 tx = &stats->tx;
523 bt_activity = &stats->general.activity;
524
525#ifdef CONFIG_IWLWIFI_DEBUGFS
526 /* handle this exception directly */
527 priv->statistics.num_bt_kills = stats->rx.general.num_bt_kills;
528 le32_add_cpu(&priv->statistics.accum_num_bt_kills,
529 le32_to_cpu(stats->rx.general.num_bt_kills));
530#endif
531 } else if (len == sizeof(struct iwl_notif_statistics)) {
532 struct iwl_notif_statistics *stats;
533 stats = &pkt->u.stats;
534 flag = &stats->flag;
535 common = &stats->general.common;
536 rx_non_phy = &stats->rx.general;
537 rx_ofdm = &stats->rx.ofdm;
538 rx_ofdm_ht = &stats->rx.ofdm_ht;
539 rx_cck = &stats->rx.cck;
540 tx = &stats->tx;
541 bt_activity = NULL;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100542 } else {
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700543 WARN_ONCE(1, "len %d doesn't match BT (%zu) or normal (%zu)\n",
544 len, sizeof(struct iwl_bt_notif_statistics),
545 sizeof(struct iwl_notif_statistics));
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700546 return 0;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100547 }
548
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700549 change = common->temperature != priv->statistics.common.temperature ||
550 (*flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
551 (priv->statistics.flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK);
552
553 iwl_accumulative_statistics(priv, common, rx_non_phy, rx_ofdm,
554 rx_ofdm_ht, rx_cck, tx, bt_activity);
555
556 iwl_recover_from_statistics(priv, rx_ofdm, rx_ofdm_ht, tx, stamp);
557
558 priv->statistics.flag = *flag;
559 memcpy(&priv->statistics.common, common, sizeof(*common));
560 memcpy(&priv->statistics.rx_non_phy, rx_non_phy, sizeof(*rx_non_phy));
561 memcpy(&priv->statistics.rx_ofdm, rx_ofdm, sizeof(*rx_ofdm));
562 memcpy(&priv->statistics.rx_ofdm_ht, rx_ofdm_ht, sizeof(*rx_ofdm_ht));
563 memcpy(&priv->statistics.rx_cck, rx_cck, sizeof(*rx_cck));
564 memcpy(&priv->statistics.tx, tx, sizeof(*tx));
565#ifdef CONFIG_IWLWIFI_DEBUGFS
566 if (bt_activity)
567 memcpy(&priv->statistics.bt_activity, bt_activity,
568 sizeof(*bt_activity));
569#endif
570
571 priv->rx_statistics_jiffies = stamp;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100572
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700573 set_bit(STATUS_STATISTICS, &priv->shrd->status);
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100574
575 /* Reschedule the statistics timer to occur in
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100576 * reg_recalib_period seconds to ensure we get a
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100577 * thermal update even if the uCode doesn't give
578 * us one */
579 mod_timer(&priv->statistics_periodic, jiffies +
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100580 msecs_to_jiffies(reg_recalib_period * 1000));
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100581
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700582 if (unlikely(!test_bit(STATUS_SCANNING, &priv->shrd->status)) &&
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100583 (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
584 iwl_rx_calc_noise(priv);
Emmanuel Grumbach74e28e42011-08-25 23:10:41 -0700585 queue_work(priv->shrd->workqueue, &priv->run_time_calib_work);
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100586 }
Wey-Yi Guy90c300c2011-07-07 08:33:01 -0700587 if (priv->cfg->lib->temperature && change)
588 priv->cfg->lib->temperature(priv);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700589 return 0;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100590}
591
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700592static int iwl_rx_reply_statistics(struct iwl_priv *priv,
593 struct iwl_rx_mem_buffer *rxb,
594 struct iwl_device_cmd *cmd)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100595{
596 struct iwl_rx_packet *pkt = rxb_addr(rxb);
597
598 if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATISTICS_CLEAR_MSK) {
599#ifdef CONFIG_IWLWIFI_DEBUGFS
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700600 memset(&priv->accum_stats, 0,
601 sizeof(priv->accum_stats));
602 memset(&priv->delta_stats, 0,
603 sizeof(priv->delta_stats));
604 memset(&priv->max_delta_stats, 0,
605 sizeof(priv->max_delta_stats));
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100606#endif
607 IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
608 }
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700609 iwl_rx_statistics(priv, rxb, cmd);
610 return 0;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100611}
612
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100613/* Handle notification from uCode that card's power state is changing
614 * due to software, hardware, or critical temperature RFKILL */
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700615static int iwl_rx_card_state_notif(struct iwl_priv *priv,
616 struct iwl_rx_mem_buffer *rxb,
617 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100618{
619 struct iwl_rx_packet *pkt = rxb_addr(rxb);
620 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700621 unsigned long status = priv->shrd->status;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100622
623 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
624 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
625 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
626 (flags & CT_CARD_DISABLED) ?
627 "Reached" : "Not reached");
628
629 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
630 CT_CARD_DISABLED)) {
631
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -0700632 iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_SET,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100633 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
634
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -0700635 iwl_write_direct32(bus(priv), HBUS_TARG_MBX_C,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100636 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
637
638 if (!(flags & RXON_CARD_DISABLED)) {
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -0700639 iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_CLR,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100640 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -0700641 iwl_write_direct32(bus(priv), HBUS_TARG_MBX_C,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100642 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
643 }
644 if (flags & CT_CARD_DISABLED)
645 iwl_tt_enter_ct_kill(priv);
646 }
647 if (!(flags & CT_CARD_DISABLED))
648 iwl_tt_exit_ct_kill(priv);
649
650 if (flags & HW_CARD_DISABLED)
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700651 set_bit(STATUS_RF_KILL_HW, &priv->shrd->status);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100652 else
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700653 clear_bit(STATUS_RF_KILL_HW, &priv->shrd->status);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100654
655
656 if (!(flags & RXON_CARD_DISABLED))
657 iwl_scan_cancel(priv);
658
659 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700660 test_bit(STATUS_RF_KILL_HW, &priv->shrd->status)))
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100661 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700662 test_bit(STATUS_RF_KILL_HW, &priv->shrd->status));
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100663 else
Johannes Bergeffd4d92011-09-15 11:46:52 -0700664 wake_up(&priv->shrd->wait_command_queue);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700665 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100666}
667
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700668static int iwl_rx_missed_beacon_notif(struct iwl_priv *priv,
669 struct iwl_rx_mem_buffer *rxb,
670 struct iwl_device_cmd *cmd)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100671
672{
673 struct iwl_rx_packet *pkt = rxb_addr(rxb);
674 struct iwl_missed_beacon_notif *missed_beacon;
675
676 missed_beacon = &pkt->u.missed_beacon;
677 if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
678 priv->missed_beacon_threshold) {
679 IWL_DEBUG_CALIB(priv,
680 "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
681 le32_to_cpu(missed_beacon->consecutive_missed_beacons),
682 le32_to_cpu(missed_beacon->total_missed_becons),
683 le32_to_cpu(missed_beacon->num_recvd_beacons),
684 le32_to_cpu(missed_beacon->num_expected_beacons));
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700685 if (!test_bit(STATUS_SCANNING, &priv->shrd->status))
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100686 iwl_init_sensitivity(priv);
687 }
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700688 return 0;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100689}
690
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100691/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
692 * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700693static int iwl_rx_reply_rx_phy(struct iwl_priv *priv,
694 struct iwl_rx_mem_buffer *rxb,
695 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100696{
697 struct iwl_rx_packet *pkt = rxb_addr(rxb);
698
Wey-Yi Guy898ed672011-07-13 08:38:57 -0700699 priv->last_phy_res_valid = true;
700 memcpy(&priv->last_phy_res, pkt->u.raw,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100701 sizeof(struct iwl_rx_phy_res));
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700702 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100703}
704
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800705/*
706 * returns non-zero if packet should be dropped
707 */
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100708static int iwl_set_decrypted_flag(struct iwl_priv *priv,
709 struct ieee80211_hdr *hdr,
710 u32 decrypt_res,
711 struct ieee80211_rx_status *stats)
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800712{
713 u16 fc = le16_to_cpu(hdr->frame_control);
714
Johannes Berg246ed352010-08-23 10:46:32 +0200715 /*
716 * All contexts have the same setting here due to it being
717 * a module parameter, so OK to check any context.
718 */
719 if (priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags &
720 RXON_FILTER_DIS_DECRYPT_MSK)
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800721 return 0;
722
723 if (!(fc & IEEE80211_FCTL_PROTECTED))
724 return 0;
725
Tomas Winklere1623442009-01-27 14:27:56 -0800726 IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800727 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
728 case RX_RES_STATUS_SEC_TYPE_TKIP:
729 /* The uCode has got a bad phase 1 Key, pushes the packet.
730 * Decryption will be done in SW. */
731 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
732 RX_RES_STATUS_BAD_KEY_TTAK)
733 break;
734
735 case RX_RES_STATUS_SEC_TYPE_WEP:
736 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
737 RX_RES_STATUS_BAD_ICV_MIC) {
738 /* bad ICV, the packet is destroyed since the
739 * decryption is inplace, drop it */
Tomas Winklere1623442009-01-27 14:27:56 -0800740 IWL_DEBUG_RX(priv, "Packet destroyed\n");
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800741 return -1;
742 }
743 case RX_RES_STATUS_SEC_TYPE_CCMP:
744 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
745 RX_RES_STATUS_DECRYPT_OK) {
Tomas Winklere1623442009-01-27 14:27:56 -0800746 IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800747 stats->flag |= RX_FLAG_DECRYPTED;
748 }
749 break;
750
751 default:
752 break;
753 }
754 return 0;
755}
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100756
757static void iwl_pass_packet_to_mac80211(struct iwl_priv *priv,
758 struct ieee80211_hdr *hdr,
759 u16 len,
760 u32 ampdu_status,
761 struct iwl_rx_mem_buffer *rxb,
762 struct ieee80211_rx_status *stats)
763{
764 struct sk_buff *skb;
765 __le16 fc = hdr->frame_control;
Garen Tamrazian68b99312011-03-30 02:29:32 -0700766 struct iwl_rxon_context *ctx;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100767
768 /* We only process data packets if the interface is open */
769 if (unlikely(!priv->is_open)) {
770 IWL_DEBUG_DROP_LIMIT(priv,
771 "Dropping packet while interface is not open.\n");
772 return;
773 }
774
775 /* In case of HW accelerated crypto and bad decryption, drop */
Don Fry9d143e92011-04-20 15:23:57 -0700776 if (!iwlagn_mod_params.sw_crypto &&
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100777 iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
778 return;
779
780 skb = dev_alloc_skb(128);
781 if (!skb) {
782 IWL_ERR(priv, "dev_alloc_skb failed\n");
783 return;
784 }
785
786 skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
787
788 iwl_update_stats(priv, false, fc, len);
Garen Tamrazian68b99312011-03-30 02:29:32 -0700789
790 /*
791 * Wake any queues that were stopped due to a passive channel tx
792 * failure. This can happen because the regulatory enforcement in
793 * the device waits for a beacon before allowing transmission,
794 * sometimes even after already having transmitted frames for the
795 * association because the new RXON may reset the information.
796 */
797 if (unlikely(ieee80211_is_beacon(fc))) {
798 for_each_context(priv, ctx) {
799 if (!ctx->last_tx_rejected)
800 continue;
801 if (compare_ether_addr(hdr->addr3,
802 ctx->active.bssid_addr))
803 continue;
804 ctx->last_tx_rejected = false;
Emmanuel Grumbache13c0c52011-08-25 23:11:24 -0700805 iwl_trans_wake_any_queue(trans(priv), ctx->ctxid);
Garen Tamrazian68b99312011-03-30 02:29:32 -0700806 }
807 }
808
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100809 memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
810
811 ieee80211_rx(priv->hw, skb);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100812 rxb->page = NULL;
813}
814
815static u32 iwl_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
816{
817 u32 decrypt_out = 0;
818
819 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
820 RX_RES_STATUS_STATION_FOUND)
821 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
822 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
823
824 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
825
826 /* packet was not encrypted */
827 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
828 RX_RES_STATUS_SEC_TYPE_NONE)
829 return decrypt_out;
830
831 /* packet was encrypted with unknown alg */
832 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
833 RX_RES_STATUS_SEC_TYPE_ERR)
834 return decrypt_out;
835
836 /* decryption was not done in HW */
837 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
838 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
839 return decrypt_out;
840
841 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
842
843 case RX_RES_STATUS_SEC_TYPE_CCMP:
844 /* alg is CCM: check MIC only */
845 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
846 /* Bad MIC */
847 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
848 else
849 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
850
851 break;
852
853 case RX_RES_STATUS_SEC_TYPE_TKIP:
854 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
855 /* Bad TTAK */
856 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
857 break;
858 }
859 /* fall through if TTAK OK */
860 default:
861 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
862 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
863 else
864 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
865 break;
866 }
867
868 IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
869 decrypt_in, decrypt_out);
870
871 return decrypt_out;
872}
873
Don Fry5c3d29f2011-07-08 08:46:29 -0700874/* Calc max signal level (dBm) among 3 possible receivers */
875static int iwlagn_calc_rssi(struct iwl_priv *priv,
876 struct iwl_rx_phy_res *rx_resp)
877{
878 /* data from PHY/DSP regarding signal strength, etc.,
879 * contents are always there, not configurable by host
880 */
881 struct iwlagn_non_cfg_phy *ncphy =
882 (struct iwlagn_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
883 u32 val, rssi_a, rssi_b, rssi_c, max_rssi;
884 u8 agc;
885
886 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_AGC_IDX]);
887 agc = (val & IWLAGN_OFDM_AGC_MSK) >> IWLAGN_OFDM_AGC_BIT_POS;
888
889 /* Find max rssi among 3 possible receivers.
890 * These values are measured by the digital signal processor (DSP).
891 * They should stay fairly constant even as the signal strength varies,
892 * if the radio's automatic gain control (AGC) is working right.
893 * AGC value (see below) will provide the "interesting" info.
894 */
895 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_AB_IDX]);
896 rssi_a = (val & IWLAGN_OFDM_RSSI_INBAND_A_BITMSK) >>
897 IWLAGN_OFDM_RSSI_A_BIT_POS;
898 rssi_b = (val & IWLAGN_OFDM_RSSI_INBAND_B_BITMSK) >>
899 IWLAGN_OFDM_RSSI_B_BIT_POS;
900 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_C_IDX]);
901 rssi_c = (val & IWLAGN_OFDM_RSSI_INBAND_C_BITMSK) >>
902 IWLAGN_OFDM_RSSI_C_BIT_POS;
903
904 max_rssi = max_t(u32, rssi_a, rssi_b);
905 max_rssi = max_t(u32, max_rssi, rssi_c);
906
907 IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
908 rssi_a, rssi_b, rssi_c, max_rssi, agc);
909
910 /* dBm = max_rssi dB - agc dB - constant.
911 * Higher AGC (higher radio gain) means lower signal. */
912 return max_rssi - agc - IWLAGN_RSSI_OFFSET;
913}
914
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100915/* Called for REPLY_RX (legacy ABG frames), or
916 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700917static int iwl_rx_reply_rx(struct iwl_priv *priv,
918 struct iwl_rx_mem_buffer *rxb,
919 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100920{
921 struct ieee80211_hdr *header;
922 struct ieee80211_rx_status rx_status;
923 struct iwl_rx_packet *pkt = rxb_addr(rxb);
924 struct iwl_rx_phy_res *phy_res;
925 __le32 rx_pkt_status;
926 struct iwl_rx_mpdu_res_start *amsdu;
927 u32 len;
928 u32 ampdu_status;
929 u32 rate_n_flags;
930
931 /**
932 * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
933 * REPLY_RX: physical layer info is in this buffer
934 * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
935 * command and cached in priv->last_phy_res
936 *
937 * Here we set up local variables depending on which command is
938 * received.
939 */
940 if (pkt->hdr.cmd == REPLY_RX) {
941 phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
942 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
943 + phy_res->cfg_phy_cnt);
944
945 len = le16_to_cpu(phy_res->byte_count);
946 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
947 phy_res->cfg_phy_cnt + len);
948 ampdu_status = le32_to_cpu(rx_pkt_status);
949 } else {
Wey-Yi Guy898ed672011-07-13 08:38:57 -0700950 if (!priv->last_phy_res_valid) {
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100951 IWL_ERR(priv, "MPDU frame without cached PHY data\n");
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700952 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100953 }
Wey-Yi Guy898ed672011-07-13 08:38:57 -0700954 phy_res = &priv->last_phy_res;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100955 amsdu = (struct iwl_rx_mpdu_res_start *)pkt->u.raw;
956 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
957 len = le16_to_cpu(amsdu->byte_count);
958 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
959 ampdu_status = iwl_translate_rx_status(priv,
960 le32_to_cpu(rx_pkt_status));
961 }
962
963 if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
964 IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
965 phy_res->cfg_phy_cnt);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700966 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100967 }
968
969 if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
970 !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
971 IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
972 le32_to_cpu(rx_pkt_status));
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700973 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100974 }
975
976 /* This will be used in several places later */
977 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
978
979 /* rx_status carries information about the packet to mac80211 */
980 rx_status.mactime = le64_to_cpu(phy_res->timestamp);
981 rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
982 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
983 rx_status.freq =
984 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
985 rx_status.band);
986 rx_status.rate_idx =
987 iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
988 rx_status.flag = 0;
989
990 /* TSF isn't reliable. In order to allow smooth user experience,
991 * this W/A doesn't propagate it to the mac80211 */
992 /*rx_status.flag |= RX_FLAG_MACTIME_MPDU;*/
993
994 priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
995
996 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
Don Fry5c3d29f2011-07-08 08:46:29 -0700997 rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100998
999 iwl_dbg_log_rx_data_frame(priv, len, header);
1000 IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
1001 rx_status.signal, (unsigned long long)rx_status.mactime);
1002
1003 /*
1004 * "antenna number"
1005 *
1006 * It seems that the antenna field in the phy flags value
1007 * is actually a bit field. This is undefined by radiotap,
1008 * it wants an actual antenna number but I always get "7"
1009 * for most legacy frames I receive indicating that the
1010 * same frame was received on all three RX chains.
1011 *
1012 * I think this field should be removed in favor of a
1013 * new 802.11n radiotap field "RX chains" that is defined
1014 * as a bitmask.
1015 */
1016 rx_status.antenna =
1017 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
1018 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
1019
1020 /* set the preamble flag if appropriate */
1021 if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
1022 rx_status.flag |= RX_FLAG_SHORTPRE;
1023
1024 /* Set up the HT phy flags */
1025 if (rate_n_flags & RATE_MCS_HT_MSK)
1026 rx_status.flag |= RX_FLAG_HT;
1027 if (rate_n_flags & RATE_MCS_HT40_MSK)
1028 rx_status.flag |= RX_FLAG_40MHZ;
1029 if (rate_n_flags & RATE_MCS_SGI_MSK)
1030 rx_status.flag |= RX_FLAG_SHORT_GI;
1031
1032 iwl_pass_packet_to_mac80211(priv, header, len, ampdu_status,
1033 rxb, &rx_status);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001034 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001035}
1036
1037/**
1038 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
1039 *
1040 * Setup the RX handlers for each of the reply types sent from the uCode
1041 * to the host.
1042 */
1043void iwl_setup_rx_handlers(struct iwl_priv *priv)
1044{
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001045 int (**handlers)(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb,
1046 struct iwl_device_cmd *cmd);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001047
1048 handlers = priv->rx_handlers;
1049
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001050 handlers[REPLY_ERROR] = iwl_rx_reply_error;
1051 handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
1052 handlers[SPECTRUM_MEASURE_NOTIFICATION] = iwl_rx_spectrum_measure_notif;
1053 handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
1054 handlers[PM_DEBUG_STATISTIC_NOTIFIC] = iwl_rx_pm_debug_statistics_notif;
1055 handlers[BEACON_NOTIFICATION] = iwl_rx_beacon_notif;
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001056 handlers[REPLY_ADD_STA] = iwl_add_sta_callback;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001057
1058 /*
1059 * The same handler is used for both the REPLY to a discrete
1060 * statistics request from the host as well as for the periodic
1061 * statistics notifications (after received beacons) from the uCode.
1062 */
1063 handlers[REPLY_STATISTICS_CMD] = iwl_rx_reply_statistics;
1064 handlers[STATISTICS_NOTIFICATION] = iwl_rx_statistics;
1065
1066 iwl_setup_rx_scan_handlers(priv);
1067
1068 handlers[CARD_STATE_NOTIFICATION] = iwl_rx_card_state_notif;
1069 handlers[MISSED_BEACONS_NOTIFICATION] = iwl_rx_missed_beacon_notif;
1070
1071 /* Rx handlers */
1072 handlers[REPLY_RX_PHY_CMD] = iwl_rx_reply_rx_phy;
1073 handlers[REPLY_RX_MPDU_CMD] = iwl_rx_reply_rx;
1074
1075 /* block ack */
1076 handlers[REPLY_COMPRESSED_BA] = iwlagn_rx_reply_compressed_ba;
1077
Emmanuel Grumbach8d301192011-07-03 15:45:34 +03001078 /* init calibration handlers */
1079 priv->rx_handlers[CALIBRATION_RES_NOTIFICATION] =
1080 iwlagn_rx_calib_result;
1081 priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
1082
1083 /* set up notification wait support */
Wey-Yi Guy898ed672011-07-13 08:38:57 -07001084 spin_lock_init(&priv->notif_wait_lock);
1085 INIT_LIST_HEAD(&priv->notif_waits);
1086 init_waitqueue_head(&priv->notif_waitq);
Emmanuel Grumbach8d301192011-07-03 15:45:34 +03001087
1088 /* Set up BT Rx handlers */
1089 if (priv->cfg->lib->bt_rx_handler_setup)
1090 priv->cfg->lib->bt_rx_handler_setup(priv);
1091
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001092}
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001093
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001094int iwl_rx_dispatch(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb,
1095 struct iwl_device_cmd *cmd)
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001096{
1097 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001098 int err = 0;
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001099
1100 /*
1101 * Do the notification wait before RX handlers so
1102 * even if the RX handler consumes the RXB we have
1103 * access to it in the notification wait entry.
1104 */
Wey-Yi Guy898ed672011-07-13 08:38:57 -07001105 if (!list_empty(&priv->notif_waits)) {
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001106 struct iwl_notification_wait *w;
1107
Wey-Yi Guy898ed672011-07-13 08:38:57 -07001108 spin_lock(&priv->notif_wait_lock);
1109 list_for_each_entry(w, &priv->notif_waits, list) {
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001110 if (w->cmd != pkt->hdr.cmd)
1111 continue;
1112 IWL_DEBUG_RX(priv,
1113 "Notif: %s, 0x%02x - wake the callers up\n",
1114 get_cmd_string(pkt->hdr.cmd),
1115 pkt->hdr.cmd);
1116 w->triggered = true;
1117 if (w->fn)
1118 w->fn(priv, pkt, w->fn_data);
1119 }
Wey-Yi Guy898ed672011-07-13 08:38:57 -07001120 spin_unlock(&priv->notif_wait_lock);
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001121
Wey-Yi Guy898ed672011-07-13 08:38:57 -07001122 wake_up_all(&priv->notif_waitq);
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001123 }
1124
1125 if (priv->pre_rx_handler)
1126 priv->pre_rx_handler(priv, rxb);
1127
1128 /* Based on type of command response or notification,
1129 * handle those that need handling via function in
1130 * rx_handlers table. See iwl_setup_rx_handlers() */
1131 if (priv->rx_handlers[pkt->hdr.cmd]) {
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001132 priv->rx_handlers_stats[pkt->hdr.cmd]++;
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001133 err = priv->rx_handlers[pkt->hdr.cmd] (priv, rxb, cmd);
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001134 } else {
1135 /* No handling needed */
1136 IWL_DEBUG_RX(priv,
1137 "No handler needed for %s, 0x%02x\n",
1138 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
1139 }
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001140 return err;
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001141}