blob: bbd674061d2e6f93c05c3d46fb55802fa29ace67 [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);
Wey-Yi Guy7e4005c2011-10-10 07:26:51 -070050 IWL_CMD(REPLY_ECHO);
Emmanuel Grumbach522376d2011-09-06 09:31:19 -070051 IWL_CMD(REPLY_RXON);
52 IWL_CMD(REPLY_RXON_ASSOC);
53 IWL_CMD(REPLY_QOS_PARAM);
54 IWL_CMD(REPLY_RXON_TIMING);
55 IWL_CMD(REPLY_ADD_STA);
56 IWL_CMD(REPLY_REMOVE_STA);
57 IWL_CMD(REPLY_REMOVE_ALL_STA);
58 IWL_CMD(REPLY_TXFIFO_FLUSH);
59 IWL_CMD(REPLY_WEPKEY);
60 IWL_CMD(REPLY_TX);
61 IWL_CMD(REPLY_LEDS_CMD);
62 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
63 IWL_CMD(COEX_PRIORITY_TABLE_CMD);
64 IWL_CMD(COEX_MEDIUM_NOTIFICATION);
65 IWL_CMD(COEX_EVENT_CMD);
66 IWL_CMD(REPLY_QUIET_CMD);
67 IWL_CMD(REPLY_CHANNEL_SWITCH);
68 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
69 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
70 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
71 IWL_CMD(POWER_TABLE_CMD);
72 IWL_CMD(PM_SLEEP_NOTIFICATION);
73 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
74 IWL_CMD(REPLY_SCAN_CMD);
75 IWL_CMD(REPLY_SCAN_ABORT_CMD);
76 IWL_CMD(SCAN_START_NOTIFICATION);
77 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
78 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
79 IWL_CMD(BEACON_NOTIFICATION);
80 IWL_CMD(REPLY_TX_BEACON);
81 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
82 IWL_CMD(QUIET_NOTIFICATION);
83 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
84 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
85 IWL_CMD(REPLY_BT_CONFIG);
86 IWL_CMD(REPLY_STATISTICS_CMD);
87 IWL_CMD(STATISTICS_NOTIFICATION);
88 IWL_CMD(REPLY_CARD_STATE_CMD);
89 IWL_CMD(CARD_STATE_NOTIFICATION);
90 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
91 IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
92 IWL_CMD(SENSITIVITY_CMD);
93 IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
94 IWL_CMD(REPLY_RX_PHY_CMD);
95 IWL_CMD(REPLY_RX_MPDU_CMD);
96 IWL_CMD(REPLY_RX);
97 IWL_CMD(REPLY_COMPRESSED_BA);
98 IWL_CMD(CALIBRATION_CFG_CMD);
99 IWL_CMD(CALIBRATION_RES_NOTIFICATION);
100 IWL_CMD(CALIBRATION_COMPLETE_NOTIFICATION);
101 IWL_CMD(REPLY_TX_POWER_DBM_CMD);
102 IWL_CMD(TEMPERATURE_NOTIFICATION);
103 IWL_CMD(TX_ANT_CONFIGURATION_CMD);
104 IWL_CMD(REPLY_BT_COEX_PROFILE_NOTIF);
105 IWL_CMD(REPLY_BT_COEX_PRIO_TABLE);
106 IWL_CMD(REPLY_BT_COEX_PROT_ENV);
107 IWL_CMD(REPLY_WIPAN_PARAMS);
108 IWL_CMD(REPLY_WIPAN_RXON);
109 IWL_CMD(REPLY_WIPAN_RXON_TIMING);
110 IWL_CMD(REPLY_WIPAN_RXON_ASSOC);
111 IWL_CMD(REPLY_WIPAN_QOS_PARAM);
112 IWL_CMD(REPLY_WIPAN_WEPKEY);
113 IWL_CMD(REPLY_WIPAN_P2P_CHANNEL_SWITCH);
114 IWL_CMD(REPLY_WIPAN_NOA_NOTIFICATION);
115 IWL_CMD(REPLY_WIPAN_DEACTIVATION_COMPLETE);
116 IWL_CMD(REPLY_WOWLAN_PATTERNS);
117 IWL_CMD(REPLY_WOWLAN_WAKEUP_FILTER);
118 IWL_CMD(REPLY_WOWLAN_TSC_RSC_PARAMS);
119 IWL_CMD(REPLY_WOWLAN_TKIP_PARAMS);
120 IWL_CMD(REPLY_WOWLAN_KEK_KCK_MATERIAL);
121 IWL_CMD(REPLY_WOWLAN_GET_STATUS);
122 default:
123 return "UNKNOWN";
124
125 }
126}
Tomas Winklera55360e2008-05-05 10:22:28 +0800127
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100128/******************************************************************************
129 *
130 * Generic RX handler implementations
131 *
132 ******************************************************************************/
133
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700134static int iwl_rx_reply_error(struct iwl_priv *priv,
135 struct iwl_rx_mem_buffer *rxb,
136 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100137{
138 struct iwl_rx_packet *pkt = rxb_addr(rxb);
139
140 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
141 "seq 0x%04X ser 0x%08X\n",
142 le32_to_cpu(pkt->u.err_resp.error_type),
143 get_cmd_string(pkt->u.err_resp.cmd_id),
144 pkt->u.err_resp.cmd_id,
145 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
146 le32_to_cpu(pkt->u.err_resp.error_info));
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700147 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100148}
149
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700150static int iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb,
151 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100152{
153 struct iwl_rx_packet *pkt = rxb_addr(rxb);
154 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
155 /*
156 * MULTI-FIXME
157 * See iwl_mac_channel_switch.
158 */
159 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
160 struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
161
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700162 if (!test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status))
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700163 return 0;
Stanislaw Gruszka6f213ff2011-06-02 18:17:15 +0200164
165 if (!le32_to_cpu(csa->status) && csa->channel == priv->switch_channel) {
166 rxon->channel = csa->channel;
167 ctx->staging.channel = csa->channel;
168 IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100169 le16_to_cpu(csa->channel));
Stanislaw Gruszka6f213ff2011-06-02 18:17:15 +0200170 iwl_chswitch_done(priv, true);
171 } else {
172 IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
173 le16_to_cpu(csa->channel));
174 iwl_chswitch_done(priv, false);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100175 }
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700176 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100177}
178
179
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700180static int iwl_rx_spectrum_measure_notif(struct iwl_priv *priv,
181 struct iwl_rx_mem_buffer *rxb,
182 struct iwl_device_cmd *cmd)
Reinette Chatre81963d62010-01-22 14:22:57 -0800183{
184 struct iwl_rx_packet *pkt = rxb_addr(rxb);
185 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
186
187 if (!report->state) {
188 IWL_DEBUG_11H(priv,
189 "Spectrum Measure Notification: Start\n");
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700190 return 0;
Reinette Chatre81963d62010-01-22 14:22:57 -0800191 }
192
193 memcpy(&priv->measure_report, report, sizeof(*report));
194 priv->measurement_status |= MEASUREMENT_READY;
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700195 return 0;
Reinette Chatre81963d62010-01-22 14:22:57 -0800196}
Reinette Chatre81963d62010-01-22 14:22:57 -0800197
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700198static int iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
199 struct iwl_rx_mem_buffer *rxb,
200 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100201{
202#ifdef CONFIG_IWLWIFI_DEBUG
203 struct iwl_rx_packet *pkt = rxb_addr(rxb);
204 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
205 IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
206 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
207#endif
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700208 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100209}
210
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700211static int iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
212 struct iwl_rx_mem_buffer *rxb,
213 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100214{
215 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Emmanuel Grumbach44856c62011-08-25 23:10:45 -0700216 u32 __maybe_unused len =
217 le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100218 IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
219 "notification for %s:\n", len,
220 get_cmd_string(pkt->hdr.cmd));
221 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, len);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700222 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100223}
224
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700225static int iwl_rx_beacon_notif(struct iwl_priv *priv,
226 struct iwl_rx_mem_buffer *rxb,
227 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100228{
229 struct iwl_rx_packet *pkt = rxb_addr(rxb);
230 struct iwlagn_beacon_notif *beacon = (void *)pkt->u.raw;
231#ifdef CONFIG_IWLWIFI_DEBUG
232 u16 status = le16_to_cpu(beacon->beacon_notify_hdr.status.status);
233 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
234
235 IWL_DEBUG_RX(priv, "beacon status %#x, retries:%d ibssmgr:%d "
236 "tsf:0x%.8x%.8x rate:%d\n",
237 status & TX_STATUS_MSK,
238 beacon->beacon_notify_hdr.failure_frame,
239 le32_to_cpu(beacon->ibss_mgr_status),
240 le32_to_cpu(beacon->high_tsf),
241 le32_to_cpu(beacon->low_tsf), rate);
242#endif
243
244 priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
245
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700246 if (!test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Emmanuel Grumbach74e28e42011-08-25 23:10:41 -0700247 queue_work(priv->shrd->workqueue, &priv->beacon_update);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700248 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100249}
250
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100251/* the threshold ratio of actual_ack_cnt to expected_ack_cnt in percent */
252#define ACK_CNT_RATIO (50)
253#define BA_TIMEOUT_CNT (5)
254#define BA_TIMEOUT_MAX (16)
255
256/**
257 * iwl_good_ack_health - checks for ACK count ratios, BA timeout retries.
258 *
259 * When the ACK count ratio is low and aggregated BA timeout retries exceeding
260 * the BA_TIMEOUT_MAX, reload firmware and bring system back to normal
261 * operation state.
262 */
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700263static bool iwl_good_ack_health(struct iwl_priv *priv,
264 struct statistics_tx *cur)
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100265{
266 int actual_delta, expected_delta, ba_timeout_delta;
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700267 struct statistics_tx *old;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100268
Wey-Yi Guy898ed672011-07-13 08:38:57 -0700269 if (priv->agg_tids_count)
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100270 return true;
271
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700272 old = &priv->statistics.tx;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100273
274 actual_delta = le32_to_cpu(cur->actual_ack_cnt) -
275 le32_to_cpu(old->actual_ack_cnt);
276 expected_delta = le32_to_cpu(cur->expected_ack_cnt) -
277 le32_to_cpu(old->expected_ack_cnt);
278
279 /* Values should not be negative, but we do not trust the firmware */
280 if (actual_delta <= 0 || expected_delta <= 0)
281 return true;
282
283 ba_timeout_delta = le32_to_cpu(cur->agg.ba_timeout) -
284 le32_to_cpu(old->agg.ba_timeout);
285
286 if ((actual_delta * 100 / expected_delta) < ACK_CNT_RATIO &&
287 ba_timeout_delta > BA_TIMEOUT_CNT) {
288 IWL_DEBUG_RADIO(priv, "deltas: actual %d expected %d ba_timeout %d\n",
289 actual_delta, expected_delta, ba_timeout_delta);
290
291#ifdef CONFIG_IWLWIFI_DEBUGFS
292 /*
293 * This is ifdef'ed on DEBUGFS because otherwise the
294 * statistics aren't available. If DEBUGFS is set but
295 * DEBUG is not, these will just compile out.
296 */
297 IWL_DEBUG_RADIO(priv, "rx_detected_cnt delta %d\n",
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700298 priv->delta_stats.tx.rx_detected_cnt);
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100299 IWL_DEBUG_RADIO(priv,
300 "ack_or_ba_timeout_collision delta %d\n",
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700301 priv->delta_stats.tx.ack_or_ba_timeout_collision);
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100302#endif
303
304 if (ba_timeout_delta >= BA_TIMEOUT_MAX)
305 return false;
306 }
307
308 return true;
309}
310
311/**
312 * iwl_good_plcp_health - checks for plcp error.
313 *
314 * When the plcp error is exceeding the thresholds, reset the radio
315 * to improve the throughput.
316 */
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100317static bool iwl_good_plcp_health(struct iwl_priv *priv,
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700318 struct statistics_rx_phy *cur_ofdm,
319 struct statistics_rx_ht_phy *cur_ofdm_ht,
320 unsigned int msecs)
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100321{
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100322 int delta;
323 int threshold = priv->cfg->base_params->plcp_delta_threshold;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100324
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100325 if (threshold == IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE) {
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100326 IWL_DEBUG_RADIO(priv, "plcp_err check disabled\n");
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100327 return true;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100328 }
329
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700330 delta = le32_to_cpu(cur_ofdm->plcp_err) -
331 le32_to_cpu(priv->statistics.rx_ofdm.plcp_err) +
332 le32_to_cpu(cur_ofdm_ht->plcp_err) -
333 le32_to_cpu(priv->statistics.rx_ofdm_ht.plcp_err);
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100334
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700335 /* Can be negative if firmware reset statistics */
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100336 if (delta <= 0)
337 return true;
338
339 if ((delta * 100 / msecs) > threshold) {
340 IWL_DEBUG_RADIO(priv,
341 "plcp health threshold %u delta %d msecs %u\n",
342 threshold, delta, msecs);
343 return false;
344 }
345
346 return true;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100347}
348
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100349static void iwl_recover_from_statistics(struct iwl_priv *priv,
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700350 struct statistics_rx_phy *cur_ofdm,
351 struct statistics_rx_ht_phy *cur_ofdm_ht,
352 struct statistics_tx *tx,
353 unsigned long stamp)
Wey-Yi Guyfa8f130c2010-03-05 14:22:46 -0800354{
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100355 unsigned int msecs;
Stanislaw Gruszkab7977ff2011-02-28 14:33:15 +0100356
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700357 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100358 return;
359
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100360 msecs = jiffies_to_msecs(stamp - priv->rx_statistics_jiffies);
361
362 /* Only gather statistics and update time stamp when not associated */
363 if (!iwl_is_any_associated(priv))
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700364 return;
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100365
366 /* Do not check/recover when do not have enough statistics data */
367 if (msecs < 99)
Wey-Yi Guyfa8f130c2010-03-05 14:22:46 -0800368 return;
Stanislaw Gruszkaca3d9382011-02-08 09:31:55 +0100369
Don Fry9d143e92011-04-20 15:23:57 -0700370 if (iwlagn_mod_params.ack_check && !iwl_good_ack_health(priv, tx)) {
Stanislaw Gruszkaca3d9382011-02-08 09:31:55 +0100371 IWL_ERR(priv, "low ack count detected, restart firmware\n");
372 if (!iwl_force_reset(priv, IWL_FW_RESET, false))
373 return;
Trieu 'Andrew' Nguyen3e4fb5f2010-01-22 14:22:46 -0800374 }
Stanislaw Gruszkaca3d9382011-02-08 09:31:55 +0100375
Don Fry9d143e92011-04-20 15:23:57 -0700376 if (iwlagn_mod_params.plcp_check &&
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700377 !iwl_good_plcp_health(priv, cur_ofdm, cur_ofdm_ht, msecs))
Stanislaw Gruszkaca3d9382011-02-08 09:31:55 +0100378 iwl_force_reset(priv, IWL_RF_RESET, false);
Wey-Yi Guybeac5492010-03-04 13:38:58 -0800379}
Wey-Yi Guybeac5492010-03-04 13:38:58 -0800380
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100381/* Calculate noise level, based on measurements during network silence just
382 * before arriving beacon. This measurement can be done only if we know
383 * exactly when to expect beacons, therefore only when we're associated. */
384static void iwl_rx_calc_noise(struct iwl_priv *priv)
385{
386 struct statistics_rx_non_phy *rx_info;
387 int num_active_rx = 0;
388 int total_silence = 0;
389 int bcn_silence_a, bcn_silence_b, bcn_silence_c;
390 int last_rx_noise;
391
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700392 rx_info = &priv->statistics.rx_non_phy;
393
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100394 bcn_silence_a =
395 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
396 bcn_silence_b =
397 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
398 bcn_silence_c =
399 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
400
401 if (bcn_silence_a) {
402 total_silence += bcn_silence_a;
403 num_active_rx++;
404 }
405 if (bcn_silence_b) {
406 total_silence += bcn_silence_b;
407 num_active_rx++;
408 }
409 if (bcn_silence_c) {
410 total_silence += bcn_silence_c;
411 num_active_rx++;
412 }
413
414 /* Average among active antennas */
415 if (num_active_rx)
416 last_rx_noise = (total_silence / num_active_rx) - 107;
417 else
418 last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
419
420 IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
421 bcn_silence_a, bcn_silence_b, bcn_silence_c,
422 last_rx_noise);
423}
424
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700425#ifdef CONFIG_IWLWIFI_DEBUGFS
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100426/*
427 * based on the assumption of all statistics counter are in DWORD
428 * FIXME: This function is for debugging, do not deal with
429 * the case of counters roll-over.
430 */
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700431static void accum_stats(__le32 *prev, __le32 *cur, __le32 *delta,
432 __le32 *max_delta, __le32 *accum, int size)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100433{
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700434 int i;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100435
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700436 for (i = 0;
437 i < size / sizeof(__le32);
438 i++, prev++, cur++, delta++, max_delta++, accum++) {
439 if (le32_to_cpu(*cur) > le32_to_cpu(*prev)) {
440 *delta = cpu_to_le32(
441 le32_to_cpu(*cur) - le32_to_cpu(*prev));
442 le32_add_cpu(accum, le32_to_cpu(*delta));
443 if (le32_to_cpu(*delta) > le32_to_cpu(*max_delta))
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100444 *max_delta = *delta;
445 }
446 }
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100447}
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100448
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700449static void
450iwl_accumulative_statistics(struct iwl_priv *priv,
451 struct statistics_general_common *common,
452 struct statistics_rx_non_phy *rx_non_phy,
453 struct statistics_rx_phy *rx_ofdm,
454 struct statistics_rx_ht_phy *rx_ofdm_ht,
455 struct statistics_rx_phy *rx_cck,
456 struct statistics_tx *tx,
457 struct statistics_bt_activity *bt_activity)
458{
459#define ACCUM(_name) \
460 accum_stats((__le32 *)&priv->statistics._name, \
461 (__le32 *)_name, \
462 (__le32 *)&priv->delta_stats._name, \
463 (__le32 *)&priv->max_delta_stats._name, \
464 (__le32 *)&priv->accum_stats._name, \
465 sizeof(*_name));
466
467 ACCUM(common);
468 ACCUM(rx_non_phy);
469 ACCUM(rx_ofdm);
470 ACCUM(rx_ofdm_ht);
471 ACCUM(rx_cck);
472 ACCUM(tx);
473 if (bt_activity)
474 ACCUM(bt_activity);
475#undef ACCUM
476}
477#else
478static inline void
479iwl_accumulative_statistics(struct iwl_priv *priv,
480 struct statistics_general_common *common,
481 struct statistics_rx_non_phy *rx_non_phy,
482 struct statistics_rx_phy *rx_ofdm,
483 struct statistics_rx_ht_phy *rx_ofdm_ht,
484 struct statistics_rx_phy *rx_cck,
485 struct statistics_tx *tx,
486 struct statistics_bt_activity *bt_activity)
487{
488}
489#endif
490
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700491static int iwl_rx_statistics(struct iwl_priv *priv,
492 struct iwl_rx_mem_buffer *rxb,
493 struct iwl_device_cmd *cmd)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100494{
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700495 unsigned long stamp = jiffies;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100496 const int reg_recalib_period = 60;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100497 int change;
498 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700499 u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
500 __le32 *flag;
501 struct statistics_general_common *common;
502 struct statistics_rx_non_phy *rx_non_phy;
503 struct statistics_rx_phy *rx_ofdm;
504 struct statistics_rx_ht_phy *rx_ofdm_ht;
505 struct statistics_rx_phy *rx_cck;
506 struct statistics_tx *tx;
507 struct statistics_bt_activity *bt_activity;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100508
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700509 len -= sizeof(struct iwl_cmd_header); /* skip header */
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100510
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700511 IWL_DEBUG_RX(priv, "Statistics notification received (%d bytes).\n",
512 len);
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100513
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700514 if (len == sizeof(struct iwl_bt_notif_statistics)) {
515 struct iwl_bt_notif_statistics *stats;
516 stats = &pkt->u.stats_bt;
517 flag = &stats->flag;
518 common = &stats->general.common;
519 rx_non_phy = &stats->rx.general.common;
520 rx_ofdm = &stats->rx.ofdm;
521 rx_ofdm_ht = &stats->rx.ofdm_ht;
522 rx_cck = &stats->rx.cck;
523 tx = &stats->tx;
524 bt_activity = &stats->general.activity;
525
526#ifdef CONFIG_IWLWIFI_DEBUGFS
527 /* handle this exception directly */
528 priv->statistics.num_bt_kills = stats->rx.general.num_bt_kills;
529 le32_add_cpu(&priv->statistics.accum_num_bt_kills,
530 le32_to_cpu(stats->rx.general.num_bt_kills));
531#endif
532 } else if (len == sizeof(struct iwl_notif_statistics)) {
533 struct iwl_notif_statistics *stats;
534 stats = &pkt->u.stats;
535 flag = &stats->flag;
536 common = &stats->general.common;
537 rx_non_phy = &stats->rx.general;
538 rx_ofdm = &stats->rx.ofdm;
539 rx_ofdm_ht = &stats->rx.ofdm_ht;
540 rx_cck = &stats->rx.cck;
541 tx = &stats->tx;
542 bt_activity = NULL;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100543 } else {
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700544 WARN_ONCE(1, "len %d doesn't match BT (%zu) or normal (%zu)\n",
545 len, sizeof(struct iwl_bt_notif_statistics),
546 sizeof(struct iwl_notif_statistics));
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700547 return 0;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100548 }
549
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700550 change = common->temperature != priv->statistics.common.temperature ||
551 (*flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
552 (priv->statistics.flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK);
553
554 iwl_accumulative_statistics(priv, common, rx_non_phy, rx_ofdm,
555 rx_ofdm_ht, rx_cck, tx, bt_activity);
556
557 iwl_recover_from_statistics(priv, rx_ofdm, rx_ofdm_ht, tx, stamp);
558
559 priv->statistics.flag = *flag;
560 memcpy(&priv->statistics.common, common, sizeof(*common));
561 memcpy(&priv->statistics.rx_non_phy, rx_non_phy, sizeof(*rx_non_phy));
562 memcpy(&priv->statistics.rx_ofdm, rx_ofdm, sizeof(*rx_ofdm));
563 memcpy(&priv->statistics.rx_ofdm_ht, rx_ofdm_ht, sizeof(*rx_ofdm_ht));
564 memcpy(&priv->statistics.rx_cck, rx_cck, sizeof(*rx_cck));
565 memcpy(&priv->statistics.tx, tx, sizeof(*tx));
566#ifdef CONFIG_IWLWIFI_DEBUGFS
567 if (bt_activity)
568 memcpy(&priv->statistics.bt_activity, bt_activity,
569 sizeof(*bt_activity));
570#endif
571
572 priv->rx_statistics_jiffies = stamp;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100573
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700574 set_bit(STATUS_STATISTICS, &priv->shrd->status);
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100575
576 /* Reschedule the statistics timer to occur in
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100577 * reg_recalib_period seconds to ensure we get a
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100578 * thermal update even if the uCode doesn't give
579 * us one */
580 mod_timer(&priv->statistics_periodic, jiffies +
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100581 msecs_to_jiffies(reg_recalib_period * 1000));
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100582
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700583 if (unlikely(!test_bit(STATUS_SCANNING, &priv->shrd->status)) &&
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100584 (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
585 iwl_rx_calc_noise(priv);
Emmanuel Grumbach74e28e42011-08-25 23:10:41 -0700586 queue_work(priv->shrd->workqueue, &priv->run_time_calib_work);
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100587 }
Wey-Yi Guy90c300c2011-07-07 08:33:01 -0700588 if (priv->cfg->lib->temperature && change)
589 priv->cfg->lib->temperature(priv);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700590 return 0;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100591}
592
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700593static int iwl_rx_reply_statistics(struct iwl_priv *priv,
594 struct iwl_rx_mem_buffer *rxb,
595 struct iwl_device_cmd *cmd)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100596{
597 struct iwl_rx_packet *pkt = rxb_addr(rxb);
598
599 if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATISTICS_CLEAR_MSK) {
600#ifdef CONFIG_IWLWIFI_DEBUGFS
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700601 memset(&priv->accum_stats, 0,
602 sizeof(priv->accum_stats));
603 memset(&priv->delta_stats, 0,
604 sizeof(priv->delta_stats));
605 memset(&priv->max_delta_stats, 0,
606 sizeof(priv->max_delta_stats));
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100607#endif
608 IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
609 }
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700610 iwl_rx_statistics(priv, rxb, cmd);
611 return 0;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100612}
613
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100614/* Handle notification from uCode that card's power state is changing
615 * due to software, hardware, or critical temperature RFKILL */
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700616static int iwl_rx_card_state_notif(struct iwl_priv *priv,
617 struct iwl_rx_mem_buffer *rxb,
618 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100619{
620 struct iwl_rx_packet *pkt = rxb_addr(rxb);
621 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700622 unsigned long status = priv->shrd->status;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100623
624 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
625 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
626 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
627 (flags & CT_CARD_DISABLED) ?
628 "Reached" : "Not reached");
629
630 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
631 CT_CARD_DISABLED)) {
632
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -0700633 iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_SET,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100634 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
635
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -0700636 iwl_write_direct32(bus(priv), HBUS_TARG_MBX_C,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100637 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
638
639 if (!(flags & RXON_CARD_DISABLED)) {
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -0700640 iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_CLR,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100641 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Emmanuel Grumbach83ed9012011-08-25 23:11:14 -0700642 iwl_write_direct32(bus(priv), HBUS_TARG_MBX_C,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100643 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
644 }
645 if (flags & CT_CARD_DISABLED)
646 iwl_tt_enter_ct_kill(priv);
647 }
648 if (!(flags & CT_CARD_DISABLED))
649 iwl_tt_exit_ct_kill(priv);
650
651 if (flags & HW_CARD_DISABLED)
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700652 set_bit(STATUS_RF_KILL_HW, &priv->shrd->status);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100653 else
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700654 clear_bit(STATUS_RF_KILL_HW, &priv->shrd->status);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100655
656
657 if (!(flags & RXON_CARD_DISABLED))
658 iwl_scan_cancel(priv);
659
660 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700661 test_bit(STATUS_RF_KILL_HW, &priv->shrd->status)))
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100662 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700663 test_bit(STATUS_RF_KILL_HW, &priv->shrd->status));
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100664 else
Johannes Bergeffd4d92011-09-15 11:46:52 -0700665 wake_up(&priv->shrd->wait_command_queue);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700666 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100667}
668
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700669static int iwl_rx_missed_beacon_notif(struct iwl_priv *priv,
670 struct iwl_rx_mem_buffer *rxb,
671 struct iwl_device_cmd *cmd)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100672
673{
674 struct iwl_rx_packet *pkt = rxb_addr(rxb);
675 struct iwl_missed_beacon_notif *missed_beacon;
676
677 missed_beacon = &pkt->u.missed_beacon;
678 if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
679 priv->missed_beacon_threshold) {
680 IWL_DEBUG_CALIB(priv,
681 "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
682 le32_to_cpu(missed_beacon->consecutive_missed_beacons),
683 le32_to_cpu(missed_beacon->total_missed_becons),
684 le32_to_cpu(missed_beacon->num_recvd_beacons),
685 le32_to_cpu(missed_beacon->num_expected_beacons));
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700686 if (!test_bit(STATUS_SCANNING, &priv->shrd->status))
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100687 iwl_init_sensitivity(priv);
688 }
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700689 return 0;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100690}
691
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100692/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
693 * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700694static int iwl_rx_reply_rx_phy(struct iwl_priv *priv,
695 struct iwl_rx_mem_buffer *rxb,
696 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100697{
698 struct iwl_rx_packet *pkt = rxb_addr(rxb);
699
Wey-Yi Guy898ed672011-07-13 08:38:57 -0700700 priv->last_phy_res_valid = true;
701 memcpy(&priv->last_phy_res, pkt->u.raw,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100702 sizeof(struct iwl_rx_phy_res));
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700703 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100704}
705
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800706/*
707 * returns non-zero if packet should be dropped
708 */
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100709static int iwl_set_decrypted_flag(struct iwl_priv *priv,
710 struct ieee80211_hdr *hdr,
711 u32 decrypt_res,
712 struct ieee80211_rx_status *stats)
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800713{
714 u16 fc = le16_to_cpu(hdr->frame_control);
715
Johannes Berg246ed352010-08-23 10:46:32 +0200716 /*
717 * All contexts have the same setting here due to it being
718 * a module parameter, so OK to check any context.
719 */
720 if (priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags &
721 RXON_FILTER_DIS_DECRYPT_MSK)
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800722 return 0;
723
724 if (!(fc & IEEE80211_FCTL_PROTECTED))
725 return 0;
726
Tomas Winklere1623442009-01-27 14:27:56 -0800727 IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800728 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
729 case RX_RES_STATUS_SEC_TYPE_TKIP:
730 /* The uCode has got a bad phase 1 Key, pushes the packet.
731 * Decryption will be done in SW. */
732 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
733 RX_RES_STATUS_BAD_KEY_TTAK)
734 break;
735
736 case RX_RES_STATUS_SEC_TYPE_WEP:
737 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
738 RX_RES_STATUS_BAD_ICV_MIC) {
739 /* bad ICV, the packet is destroyed since the
740 * decryption is inplace, drop it */
Tomas Winklere1623442009-01-27 14:27:56 -0800741 IWL_DEBUG_RX(priv, "Packet destroyed\n");
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800742 return -1;
743 }
744 case RX_RES_STATUS_SEC_TYPE_CCMP:
745 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
746 RX_RES_STATUS_DECRYPT_OK) {
Tomas Winklere1623442009-01-27 14:27:56 -0800747 IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800748 stats->flag |= RX_FLAG_DECRYPTED;
749 }
750 break;
751
752 default:
753 break;
754 }
755 return 0;
756}
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100757
758static void iwl_pass_packet_to_mac80211(struct iwl_priv *priv,
759 struct ieee80211_hdr *hdr,
760 u16 len,
761 u32 ampdu_status,
762 struct iwl_rx_mem_buffer *rxb,
763 struct ieee80211_rx_status *stats)
764{
765 struct sk_buff *skb;
766 __le16 fc = hdr->frame_control;
Garen Tamrazian68b99312011-03-30 02:29:32 -0700767 struct iwl_rxon_context *ctx;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100768
769 /* We only process data packets if the interface is open */
770 if (unlikely(!priv->is_open)) {
771 IWL_DEBUG_DROP_LIMIT(priv,
772 "Dropping packet while interface is not open.\n");
773 return;
774 }
775
776 /* In case of HW accelerated crypto and bad decryption, drop */
Don Fry9d143e92011-04-20 15:23:57 -0700777 if (!iwlagn_mod_params.sw_crypto &&
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100778 iwl_set_decrypted_flag(priv, hdr, ampdu_status, stats))
779 return;
780
781 skb = dev_alloc_skb(128);
782 if (!skb) {
783 IWL_ERR(priv, "dev_alloc_skb failed\n");
784 return;
785 }
786
787 skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
788
789 iwl_update_stats(priv, false, fc, len);
Garen Tamrazian68b99312011-03-30 02:29:32 -0700790
791 /*
792 * Wake any queues that were stopped due to a passive channel tx
793 * failure. This can happen because the regulatory enforcement in
794 * the device waits for a beacon before allowing transmission,
795 * sometimes even after already having transmitted frames for the
796 * association because the new RXON may reset the information.
797 */
798 if (unlikely(ieee80211_is_beacon(fc))) {
799 for_each_context(priv, ctx) {
800 if (!ctx->last_tx_rejected)
801 continue;
802 if (compare_ether_addr(hdr->addr3,
803 ctx->active.bssid_addr))
804 continue;
805 ctx->last_tx_rejected = false;
Emmanuel Grumbache13c0c52011-08-25 23:11:24 -0700806 iwl_trans_wake_any_queue(trans(priv), ctx->ctxid);
Garen Tamrazian68b99312011-03-30 02:29:32 -0700807 }
808 }
809
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100810 memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
811
812 ieee80211_rx(priv->hw, skb);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100813 rxb->page = NULL;
814}
815
816static u32 iwl_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
817{
818 u32 decrypt_out = 0;
819
820 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
821 RX_RES_STATUS_STATION_FOUND)
822 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
823 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
824
825 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
826
827 /* packet was not encrypted */
828 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
829 RX_RES_STATUS_SEC_TYPE_NONE)
830 return decrypt_out;
831
832 /* packet was encrypted with unknown alg */
833 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
834 RX_RES_STATUS_SEC_TYPE_ERR)
835 return decrypt_out;
836
837 /* decryption was not done in HW */
838 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
839 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
840 return decrypt_out;
841
842 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
843
844 case RX_RES_STATUS_SEC_TYPE_CCMP:
845 /* alg is CCM: check MIC only */
846 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
847 /* Bad MIC */
848 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
849 else
850 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
851
852 break;
853
854 case RX_RES_STATUS_SEC_TYPE_TKIP:
855 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
856 /* Bad TTAK */
857 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
858 break;
859 }
860 /* fall through if TTAK OK */
861 default:
862 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
863 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
864 else
865 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
866 break;
867 }
868
869 IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
870 decrypt_in, decrypt_out);
871
872 return decrypt_out;
873}
874
Don Fry5c3d29f2011-07-08 08:46:29 -0700875/* Calc max signal level (dBm) among 3 possible receivers */
876static int iwlagn_calc_rssi(struct iwl_priv *priv,
877 struct iwl_rx_phy_res *rx_resp)
878{
879 /* data from PHY/DSP regarding signal strength, etc.,
880 * contents are always there, not configurable by host
881 */
882 struct iwlagn_non_cfg_phy *ncphy =
883 (struct iwlagn_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
884 u32 val, rssi_a, rssi_b, rssi_c, max_rssi;
885 u8 agc;
886
887 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_AGC_IDX]);
888 agc = (val & IWLAGN_OFDM_AGC_MSK) >> IWLAGN_OFDM_AGC_BIT_POS;
889
890 /* Find max rssi among 3 possible receivers.
891 * These values are measured by the digital signal processor (DSP).
892 * They should stay fairly constant even as the signal strength varies,
893 * if the radio's automatic gain control (AGC) is working right.
894 * AGC value (see below) will provide the "interesting" info.
895 */
896 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_AB_IDX]);
897 rssi_a = (val & IWLAGN_OFDM_RSSI_INBAND_A_BITMSK) >>
898 IWLAGN_OFDM_RSSI_A_BIT_POS;
899 rssi_b = (val & IWLAGN_OFDM_RSSI_INBAND_B_BITMSK) >>
900 IWLAGN_OFDM_RSSI_B_BIT_POS;
901 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_C_IDX]);
902 rssi_c = (val & IWLAGN_OFDM_RSSI_INBAND_C_BITMSK) >>
903 IWLAGN_OFDM_RSSI_C_BIT_POS;
904
905 max_rssi = max_t(u32, rssi_a, rssi_b);
906 max_rssi = max_t(u32, max_rssi, rssi_c);
907
908 IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
909 rssi_a, rssi_b, rssi_c, max_rssi, agc);
910
911 /* dBm = max_rssi dB - agc dB - constant.
912 * Higher AGC (higher radio gain) means lower signal. */
913 return max_rssi - agc - IWLAGN_RSSI_OFFSET;
914}
915
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100916/* Called for REPLY_RX (legacy ABG frames), or
917 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700918static int iwl_rx_reply_rx(struct iwl_priv *priv,
919 struct iwl_rx_mem_buffer *rxb,
920 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100921{
922 struct ieee80211_hdr *header;
923 struct ieee80211_rx_status rx_status;
924 struct iwl_rx_packet *pkt = rxb_addr(rxb);
925 struct iwl_rx_phy_res *phy_res;
926 __le32 rx_pkt_status;
927 struct iwl_rx_mpdu_res_start *amsdu;
928 u32 len;
929 u32 ampdu_status;
930 u32 rate_n_flags;
931
932 /**
933 * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
934 * REPLY_RX: physical layer info is in this buffer
935 * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
936 * command and cached in priv->last_phy_res
937 *
938 * Here we set up local variables depending on which command is
939 * received.
940 */
941 if (pkt->hdr.cmd == REPLY_RX) {
942 phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
943 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
944 + phy_res->cfg_phy_cnt);
945
946 len = le16_to_cpu(phy_res->byte_count);
947 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
948 phy_res->cfg_phy_cnt + len);
949 ampdu_status = le32_to_cpu(rx_pkt_status);
950 } else {
Wey-Yi Guy898ed672011-07-13 08:38:57 -0700951 if (!priv->last_phy_res_valid) {
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100952 IWL_ERR(priv, "MPDU frame without cached PHY data\n");
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700953 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100954 }
Wey-Yi Guy898ed672011-07-13 08:38:57 -0700955 phy_res = &priv->last_phy_res;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100956 amsdu = (struct iwl_rx_mpdu_res_start *)pkt->u.raw;
957 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
958 len = le16_to_cpu(amsdu->byte_count);
959 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
960 ampdu_status = iwl_translate_rx_status(priv,
961 le32_to_cpu(rx_pkt_status));
962 }
963
964 if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
965 IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
966 phy_res->cfg_phy_cnt);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700967 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100968 }
969
970 if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
971 !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
972 IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
973 le32_to_cpu(rx_pkt_status));
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700974 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100975 }
976
977 /* This will be used in several places later */
978 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
979
980 /* rx_status carries information about the packet to mac80211 */
981 rx_status.mactime = le64_to_cpu(phy_res->timestamp);
982 rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
983 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
984 rx_status.freq =
985 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
986 rx_status.band);
987 rx_status.rate_idx =
988 iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
989 rx_status.flag = 0;
990
991 /* TSF isn't reliable. In order to allow smooth user experience,
992 * this W/A doesn't propagate it to the mac80211 */
993 /*rx_status.flag |= RX_FLAG_MACTIME_MPDU;*/
994
995 priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
996
997 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
Don Fry5c3d29f2011-07-08 08:46:29 -0700998 rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100999
1000 iwl_dbg_log_rx_data_frame(priv, len, header);
1001 IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
1002 rx_status.signal, (unsigned long long)rx_status.mactime);
1003
1004 /*
1005 * "antenna number"
1006 *
1007 * It seems that the antenna field in the phy flags value
1008 * is actually a bit field. This is undefined by radiotap,
1009 * it wants an actual antenna number but I always get "7"
1010 * for most legacy frames I receive indicating that the
1011 * same frame was received on all three RX chains.
1012 *
1013 * I think this field should be removed in favor of a
1014 * new 802.11n radiotap field "RX chains" that is defined
1015 * as a bitmask.
1016 */
1017 rx_status.antenna =
1018 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
1019 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
1020
1021 /* set the preamble flag if appropriate */
1022 if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
1023 rx_status.flag |= RX_FLAG_SHORTPRE;
1024
1025 /* Set up the HT phy flags */
1026 if (rate_n_flags & RATE_MCS_HT_MSK)
1027 rx_status.flag |= RX_FLAG_HT;
1028 if (rate_n_flags & RATE_MCS_HT40_MSK)
1029 rx_status.flag |= RX_FLAG_40MHZ;
1030 if (rate_n_flags & RATE_MCS_SGI_MSK)
1031 rx_status.flag |= RX_FLAG_SHORT_GI;
1032
1033 iwl_pass_packet_to_mac80211(priv, header, len, ampdu_status,
1034 rxb, &rx_status);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001035 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001036}
1037
1038/**
1039 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
1040 *
1041 * Setup the RX handlers for each of the reply types sent from the uCode
1042 * to the host.
1043 */
1044void iwl_setup_rx_handlers(struct iwl_priv *priv)
1045{
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001046 int (**handlers)(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb,
1047 struct iwl_device_cmd *cmd);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001048
1049 handlers = priv->rx_handlers;
1050
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001051 handlers[REPLY_ERROR] = iwl_rx_reply_error;
1052 handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
1053 handlers[SPECTRUM_MEASURE_NOTIFICATION] = iwl_rx_spectrum_measure_notif;
1054 handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
1055 handlers[PM_DEBUG_STATISTIC_NOTIFIC] = iwl_rx_pm_debug_statistics_notif;
1056 handlers[BEACON_NOTIFICATION] = iwl_rx_beacon_notif;
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001057 handlers[REPLY_ADD_STA] = iwl_add_sta_callback;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001058
1059 /*
1060 * The same handler is used for both the REPLY to a discrete
1061 * statistics request from the host as well as for the periodic
1062 * statistics notifications (after received beacons) from the uCode.
1063 */
1064 handlers[REPLY_STATISTICS_CMD] = iwl_rx_reply_statistics;
1065 handlers[STATISTICS_NOTIFICATION] = iwl_rx_statistics;
1066
1067 iwl_setup_rx_scan_handlers(priv);
1068
1069 handlers[CARD_STATE_NOTIFICATION] = iwl_rx_card_state_notif;
1070 handlers[MISSED_BEACONS_NOTIFICATION] = iwl_rx_missed_beacon_notif;
1071
1072 /* Rx handlers */
1073 handlers[REPLY_RX_PHY_CMD] = iwl_rx_reply_rx_phy;
1074 handlers[REPLY_RX_MPDU_CMD] = iwl_rx_reply_rx;
1075
1076 /* block ack */
1077 handlers[REPLY_COMPRESSED_BA] = iwlagn_rx_reply_compressed_ba;
1078
Emmanuel Grumbach8d301192011-07-03 15:45:34 +03001079 /* init calibration handlers */
1080 priv->rx_handlers[CALIBRATION_RES_NOTIFICATION] =
1081 iwlagn_rx_calib_result;
1082 priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
1083
1084 /* set up notification wait support */
Wey-Yi Guy898ed672011-07-13 08:38:57 -07001085 spin_lock_init(&priv->notif_wait_lock);
1086 INIT_LIST_HEAD(&priv->notif_waits);
1087 init_waitqueue_head(&priv->notif_waitq);
Emmanuel Grumbach8d301192011-07-03 15:45:34 +03001088
1089 /* Set up BT Rx handlers */
1090 if (priv->cfg->lib->bt_rx_handler_setup)
1091 priv->cfg->lib->bt_rx_handler_setup(priv);
1092
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001093}
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001094
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001095int iwl_rx_dispatch(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb,
1096 struct iwl_device_cmd *cmd)
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001097{
1098 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001099 int err = 0;
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001100
1101 /*
1102 * Do the notification wait before RX handlers so
1103 * even if the RX handler consumes the RXB we have
1104 * access to it in the notification wait entry.
1105 */
Wey-Yi Guy898ed672011-07-13 08:38:57 -07001106 if (!list_empty(&priv->notif_waits)) {
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001107 struct iwl_notification_wait *w;
1108
Wey-Yi Guy898ed672011-07-13 08:38:57 -07001109 spin_lock(&priv->notif_wait_lock);
1110 list_for_each_entry(w, &priv->notif_waits, list) {
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001111 if (w->cmd != pkt->hdr.cmd)
1112 continue;
1113 IWL_DEBUG_RX(priv,
1114 "Notif: %s, 0x%02x - wake the callers up\n",
1115 get_cmd_string(pkt->hdr.cmd),
1116 pkt->hdr.cmd);
1117 w->triggered = true;
1118 if (w->fn)
1119 w->fn(priv, pkt, w->fn_data);
1120 }
Wey-Yi Guy898ed672011-07-13 08:38:57 -07001121 spin_unlock(&priv->notif_wait_lock);
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001122
Wey-Yi Guy898ed672011-07-13 08:38:57 -07001123 wake_up_all(&priv->notif_waitq);
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001124 }
1125
1126 if (priv->pre_rx_handler)
1127 priv->pre_rx_handler(priv, rxb);
1128
1129 /* Based on type of command response or notification,
1130 * handle those that need handling via function in
1131 * rx_handlers table. See iwl_setup_rx_handlers() */
1132 if (priv->rx_handlers[pkt->hdr.cmd]) {
Emmanuel Grumbach1f7b6172011-08-25 23:10:59 -07001133 priv->rx_handlers_stats[pkt->hdr.cmd]++;
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001134 err = priv->rx_handlers[pkt->hdr.cmd] (priv, rxb, cmd);
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001135 } else {
1136 /* No handling needed */
1137 IWL_DEBUG_RX(priv,
1138 "No handler needed for %s, 0x%02x\n",
1139 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
1140 }
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001141 return err;
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001142}